2 * Copyright 2000, International Business Machines Corporation and others.
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
9 * Portions Copyright (c) 2005-2008 Sine Nomine Associates
12 /* 1/1/89: NB: this stuff is all going to be replaced. Don't take it too seriously */
17 Institution: The Information Technology Center, Carnegie-Mellon University
21 #include <afsconfig.h>
22 #include <afs/param.h>
28 #include <afs/afsint.h>
31 #include <sys/param.h>
32 #if !defined(AFS_SGI_ENV)
35 #else /* AFS_OSF_ENV */
36 #ifdef AFS_VFSINCL_ENV
39 #include <sys/fs/ufs_fs.h>
41 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
42 #include <ufs/ufs/dinode.h>
43 #include <ufs/ffs/fs.h>
48 #else /* AFS_VFSINCL_ENV */
49 #if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_XBSD_ENV)
52 #endif /* AFS_VFSINCL_ENV */
53 #endif /* AFS_OSF_ENV */
54 #endif /* AFS_SGI_ENV */
55 #endif /* AFS_NT40_ENV */
73 #if defined(AFS_SUN_ENV) || defined(AFS_SUN5_ENV)
75 #include <sys/mnttab.h>
76 #include <sys/mntent.h>
82 #if defined(AFS_SGI_ENV)
87 #ifndef AFS_LINUX20_ENV
88 #include <fstab.h> /* Need to find in libc 5, present in libc 6 */
91 #endif /* AFS_SGI_ENV */
93 #endif /* AFS_HPUX_ENV */
97 #include <netinet/in.h>
101 #include <sys/time.h>
102 #endif /* ITIMER_REAL */
103 #endif /* AFS_NT40_ENV */
104 #if defined(AFS_SUN5_ENV) || defined(AFS_NT40_ENV) || defined(AFS_LINUX20_ENV)
111 #include <afs/errors.h>
114 #include <afs/afssyscalls.h>
116 #include <afs/afsutil.h>
120 #include "daemon_com.h"
122 #include "salvsync.h"
125 #include "partition.h"
126 #include "volume_inline.h"
127 #ifdef AFS_PTHREAD_ENV
129 #else /* AFS_PTHREAD_ENV */
130 #include "afs/assert.h"
131 #endif /* AFS_PTHREAD_ENV */
138 #if !defined(offsetof)
143 #define afs_stat stat64
144 #define afs_fstat fstat64
145 #define afs_open open64
146 #else /* !O_LARGEFILE */
147 #define afs_stat stat
148 #define afs_fstat fstat
149 #define afs_open open
150 #endif /* !O_LARGEFILE */
152 #ifdef AFS_PTHREAD_ENV
153 pthread_mutex_t vol_glock_mutex;
154 pthread_mutex_t vol_trans_mutex;
155 pthread_cond_t vol_put_volume_cond;
156 pthread_cond_t vol_sleep_cond;
157 int vol_attach_threads = 1;
158 #endif /* AFS_PTHREAD_ENV */
160 #ifdef AFS_DEMAND_ATTACH_FS
161 pthread_mutex_t vol_salvsync_mutex;
162 #endif /* AFS_DEMAND_ATTACH_FS */
165 extern void *calloc(), *realloc();
168 /*@printflike@*/ extern void Log(const char *format, ...);
170 /* Forward declarations */
171 static Volume *attach2(Error * ec, VolId vid, char *path,
172 register struct VolumeHeader *header,
173 struct DiskPartition *partp, Volume * vp,
174 int isbusy, int mode);
175 static void ReallyFreeVolume(Volume * vp);
176 #ifdef AFS_DEMAND_ATTACH_FS
177 static void FreeVolume(Volume * vp);
178 #else /* !AFS_DEMAND_ATTACH_FS */
179 #define FreeVolume(vp) ReallyFreeVolume(vp)
180 static void VScanUpdateList(void);
181 #endif /* !AFS_DEMAND_ATTACH_FS */
182 static void VInitVolumeHeaderCache(afs_uint32 howMany);
183 static int GetVolumeHeader(register Volume * vp);
184 static void ReleaseVolumeHeader(register struct volHeader *hd);
185 static void FreeVolumeHeader(register Volume * vp);
186 static void AddVolumeToHashTable(register Volume * vp, int hashid);
187 static void DeleteVolumeFromHashTable(register Volume * vp);
188 static int VHold(Volume * vp);
189 static int VHold_r(Volume * vp);
190 static void VGetBitmap_r(Error * ec, Volume * vp, VnodeClass class);
191 static void GetVolumePath(Error * ec, VolId volumeId, char **partitionp,
193 static void VReleaseVolumeHandles_r(Volume * vp);
194 static void VCloseVolumeHandles_r(Volume * vp);
195 static void LoadVolumeHeader(Error * ec, Volume * vp);
196 static int VCheckOffline(register Volume * vp);
197 static int VCheckDetach(register Volume * vp);
198 static Volume * GetVolume(Error * ec, Error * client_ec, VolId volumeId, Volume * hint, int flags);
199 static int VolumeExternalName_r(VolumeId volumeId, char * name, size_t len);
201 int LogLevel; /* Vice loglevel--not defined as extern so that it will be
202 * defined when not linked with vice, XXXX */
203 ProgramType programType; /* The type of program using the package */
205 /* extended volume package statistics */
208 #ifdef VOL_LOCK_DEBUG
209 pthread_t vol_glock_holder = 0;
213 #define VOLUME_BITMAP_GROWSIZE 16 /* bytes, => 128vnodes */
214 /* Must be a multiple of 4 (1 word) !! */
216 /* this parameter needs to be tunable at runtime.
217 * 128 was really inadequate for largish servers -- at 16384 volumes this
218 * puts average chain length at 128, thus an average 65 deref's to find a volptr.
219 * talk about bad spatial locality...
221 * an AVL or splay tree might work a lot better, but we'll just increase
222 * the default hash table size for now
224 #define DEFAULT_VOLUME_HASH_SIZE 256 /* Must be a power of 2!! */
225 #define DEFAULT_VOLUME_HASH_MASK (DEFAULT_VOLUME_HASH_SIZE-1)
226 #define VOLUME_HASH(volumeId) (volumeId&(VolumeHashTable.Mask))
229 * turn volume hash chains into partially ordered lists.
230 * when the threshold is exceeded between two adjacent elements,
231 * perform a chain rebalancing operation.
233 * keep the threshold high in order to keep cache line invalidates
234 * low "enough" on SMPs
236 #define VOLUME_HASH_REORDER_THRESHOLD 200
239 * when possible, don't just reorder single elements, but reorder
240 * entire chains of elements at once. a chain of elements that
241 * exceed the element previous to the pivot by at least CHAIN_THRESH
242 * accesses are moved in front of the chain whose elements have at
243 * least CHAIN_THRESH less accesses than the pivot element
245 #define VOLUME_HASH_REORDER_CHAIN_THRESH (VOLUME_HASH_REORDER_THRESHOLD / 2)
247 #include "rx/rx_queue.h"
250 VolumeHashTable_t VolumeHashTable = {
251 DEFAULT_VOLUME_HASH_SIZE,
252 DEFAULT_VOLUME_HASH_MASK,
257 static void VInitVolumeHash(void);
261 /* This macro is used where an ffs() call does not exist. Was in util/ffs.c */
265 afs_int32 ffs_tmp = x;
269 for (ffs_i = 1;; ffs_i++) {
276 #endif /* !AFS_HAVE_FFS */
278 #ifdef AFS_PTHREAD_ENV
279 typedef struct diskpartition_queue_t {
280 struct rx_queue queue;
281 struct DiskPartition * diskP;
282 } diskpartition_queue_t;
283 typedef struct vinitvolumepackage_thread_t {
284 struct rx_queue queue;
285 pthread_cond_t thread_done_cv;
286 int n_threads_complete;
287 } vinitvolumepackage_thread_t;
288 static void * VInitVolumePackageThread(void * args);
289 #endif /* AFS_PTHREAD_ENV */
291 static int VAttachVolumesByPartition(struct DiskPartition *diskP,
292 int * nAttached, int * nUnattached);
295 #ifdef AFS_DEMAND_ATTACH_FS
296 /* demand attach fileserver extensions */
299 * in the future we will support serialization of VLRU state into the fs_state
302 * these structures are the beginning of that effort
304 struct VLRU_DiskHeader {
305 struct versionStamp stamp; /* magic and structure version number */
306 afs_uint32 mtime; /* time of dump to disk */
307 afs_uint32 num_records; /* number of VLRU_DiskEntry records */
310 struct VLRU_DiskEntry {
311 afs_uint32 vid; /* volume ID */
312 afs_uint32 idx; /* generation */
313 afs_uint32 last_get; /* timestamp of last get */
316 struct VLRU_StartupQueue {
317 struct VLRU_DiskEntry * entry;
322 typedef struct vshutdown_thread_t {
324 pthread_mutex_t lock;
326 pthread_cond_t master_cv;
328 int n_threads_complete;
330 int schedule_version;
333 byte n_parts_done_pass;
334 byte part_thread_target[VOLMAXPARTS+1];
335 byte part_done_pass[VOLMAXPARTS+1];
336 struct rx_queue * part_pass_head[VOLMAXPARTS+1];
337 int stats[4][VOLMAXPARTS+1];
338 } vshutdown_thread_t;
339 static void * VShutdownThread(void * args);
342 static Volume * VAttachVolumeByVp_r(Error * ec, Volume * vp, int mode);
343 static int VCheckFree(Volume * vp);
346 static void AddVolumeToVByPList_r(Volume * vp);
347 static void DeleteVolumeFromVByPList_r(Volume * vp);
348 static void VVByPListBeginExclusive_r(struct DiskPartition * dp);
349 static void VVByPListEndExclusive_r(struct DiskPartition * dp);
350 static void VVByPListWait_r(struct DiskPartition * dp);
352 /* online salvager */
353 static int VCheckSalvage(register Volume * vp);
354 static int VUpdateSalvagePriority_r(Volume * vp);
355 static int VScheduleSalvage_r(Volume * vp);
356 static int VCancelSalvage_r(Volume * vp, int reason);
358 /* Volume hash table */
359 static void VReorderHash_r(VolumeHashChainHead * head, Volume * pp, Volume * vp);
360 static void VHashBeginExclusive_r(VolumeHashChainHead * head);
361 static void VHashEndExclusive_r(VolumeHashChainHead * head);
362 static void VHashWait_r(VolumeHashChainHead * head);
365 static int ShutdownVByPForPass_r(struct DiskPartition * dp, int pass);
366 static int ShutdownVolumeWalk_r(struct DiskPartition * dp, int pass,
367 struct rx_queue ** idx);
368 static void ShutdownController(vshutdown_thread_t * params);
369 static void ShutdownCreateSchedule(vshutdown_thread_t * params);
372 static void VLRU_ComputeConstants(void);
373 static void VInitVLRU(void);
374 static void VLRU_Init_Node_r(volatile Volume * vp);
375 static void VLRU_Add_r(volatile Volume * vp);
376 static void VLRU_Delete_r(volatile Volume * vp);
377 static void VLRU_UpdateAccess_r(volatile Volume * vp);
378 static void * VLRU_ScannerThread(void * args);
379 static void VLRU_Scan_r(int idx);
380 static void VLRU_Promote_r(int idx);
381 static void VLRU_Demote_r(int idx);
382 static void VLRU_SwitchQueues(volatile Volume * vp, int new_idx, int append);
385 static int VCheckSoftDetach(volatile Volume * vp, afs_uint32 thresh);
386 static int VCheckSoftDetachCandidate(volatile Volume * vp, afs_uint32 thresh);
387 static int VSoftDetachVolume_r(volatile Volume * vp, afs_uint32 thresh);
388 #endif /* AFS_DEMAND_ATTACH_FS */
391 struct Lock vol_listLock; /* Lock obtained when listing volumes:
392 * prevents a volume from being missed
393 * if the volume is attached during a
397 static int TimeZoneCorrection; /* Number of seconds west of GMT */
399 /* Common message used when the volume goes off line */
400 char *VSalvageMessage =
401 "Files in this volume are currently unavailable; call operations";
403 int VInit; /* 0 - uninitialized,
404 * 1 - initialized but not all volumes have been attached,
405 * 2 - initialized and all volumes have been attached,
406 * 3 - initialized, all volumes have been attached, and
407 * VConnectFS() has completed. */
410 bit32 VolumeCacheCheck; /* Incremented everytime a volume goes on line--
411 * used to stamp volume headers and in-core
412 * vnodes. When the volume goes on-line the
413 * vnode will be invalidated
414 * access only with VOL_LOCK held */
419 /***************************************************/
420 /* Startup routines */
421 /***************************************************/
424 VInitVolumePackage(ProgramType pt, afs_uint32 nLargeVnodes, afs_uint32 nSmallVnodes,
425 int connect, afs_uint32 volcache)
427 int errors = 0; /* Number of errors while finding vice partitions. */
433 memset(&VStats, 0, sizeof(VStats));
434 VStats.hdr_cache_size = 200;
436 VInitPartitionPackage();
438 #ifdef AFS_DEMAND_ATTACH_FS
439 if (programType == fileServer) {
442 VLRU_SetOptions(VLRU_SET_ENABLED, 0);
446 #ifdef AFS_PTHREAD_ENV
447 assert(pthread_mutex_init(&vol_glock_mutex, NULL) == 0);
448 assert(pthread_mutex_init(&vol_trans_mutex, NULL) == 0);
449 assert(pthread_cond_init(&vol_put_volume_cond, NULL) == 0);
450 assert(pthread_cond_init(&vol_sleep_cond, NULL) == 0);
451 #else /* AFS_PTHREAD_ENV */
453 #endif /* AFS_PTHREAD_ENV */
454 Lock_Init(&vol_listLock);
456 srandom(time(0)); /* For VGetVolumeInfo */
457 gettimeofday(&tv, &tz);
458 TimeZoneCorrection = tz.tz_minuteswest * 60;
460 #ifdef AFS_DEMAND_ATTACH_FS
461 assert(pthread_mutex_init(&vol_salvsync_mutex, NULL) == 0);
462 #endif /* AFS_DEMAND_ATTACH_FS */
464 /* Ok, we have done enough initialization that fileserver can
465 * start accepting calls, even though the volumes may not be
466 * available just yet.
470 #if defined(AFS_DEMAND_ATTACH_FS) && defined(SALVSYNC_BUILD_SERVER)
471 if (programType == salvageServer) {
474 #endif /* AFS_DEMAND_ATTACH_FS */
475 #ifdef FSSYNC_BUILD_SERVER
476 if (programType == fileServer) {
480 #if defined(AFS_DEMAND_ATTACH_FS) && defined(SALVSYNC_BUILD_CLIENT)
481 if (programType == fileServer) {
482 /* establish a connection to the salvager at this point */
483 assert(VConnectSALV() != 0);
485 #endif /* AFS_DEMAND_ATTACH_FS */
487 if (volcache > VStats.hdr_cache_size)
488 VStats.hdr_cache_size = volcache;
489 VInitVolumeHeaderCache(VStats.hdr_cache_size);
491 VInitVnodes(vLarge, nLargeVnodes);
492 VInitVnodes(vSmall, nSmallVnodes);
495 errors = VAttachPartitions();
499 if (programType == fileServer) {
500 struct DiskPartition *diskP;
501 #ifdef AFS_PTHREAD_ENV
502 struct vinitvolumepackage_thread_t params;
503 struct diskpartition_queue_t * dpq;
504 int i, threads, parts;
506 pthread_attr_t attrs;
508 assert(pthread_cond_init(¶ms.thread_done_cv,NULL) == 0);
510 params.n_threads_complete = 0;
512 /* create partition work queue */
513 for (parts=0, diskP = DiskPartitionList; diskP; diskP = diskP->next, parts++) {
514 dpq = (diskpartition_queue_t *) malloc(sizeof(struct diskpartition_queue_t));
517 queue_Append(¶ms,dpq);
520 threads = MIN(parts, vol_attach_threads);
523 /* spawn off a bunch of initialization threads */
524 assert(pthread_attr_init(&attrs) == 0);
525 assert(pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED) == 0);
527 Log("VInitVolumePackage: beginning parallel fileserver startup\n");
528 #ifdef AFS_DEMAND_ATTACH_FS
529 Log("VInitVolumePackage: using %d threads to pre-attach volumes on %d partitions\n",
531 #else /* AFS_DEMAND_ATTACH_FS */
532 Log("VInitVolumePackage: using %d threads to attach volumes on %d partitions\n",
534 #endif /* AFS_DEMAND_ATTACH_FS */
537 for (i=0; i < threads; i++) {
538 assert(pthread_create
539 (&tid, &attrs, &VInitVolumePackageThread,
543 while(params.n_threads_complete < threads) {
544 VOL_CV_WAIT(¶ms.thread_done_cv);
548 assert(pthread_attr_destroy(&attrs) == 0);
550 /* if we're only going to run one init thread, don't bother creating
552 Log("VInitVolumePackage: beginning single-threaded fileserver startup\n");
553 #ifdef AFS_DEMAND_ATTACH_FS
554 Log("VInitVolumePackage: using 1 thread to pre-attach volumes on %d partition(s)\n",
556 #else /* AFS_DEMAND_ATTACH_FS */
557 Log("VInitVolumePackage: using 1 thread to attach volumes on %d partition(s)\n",
559 #endif /* AFS_DEMAND_ATTACH_FS */
561 VInitVolumePackageThread(¶ms);
564 assert(pthread_cond_destroy(¶ms.thread_done_cv) == 0);
566 #else /* AFS_PTHREAD_ENV */
570 /* Attach all the volumes in this partition */
571 for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
572 int nAttached = 0, nUnattached = 0;
573 assert(VAttachVolumesByPartition(diskP, &nAttached, &nUnattached) == 0);
575 #endif /* AFS_PTHREAD_ENV */
578 VInit = 2; /* Initialized, and all volumes have been attached */
579 #ifdef FSSYNC_BUILD_CLIENT
580 if (programType == volumeUtility && connect) {
582 Log("Unable to connect to file server; aborted\n");
586 #ifdef AFS_DEMAND_ATTACH_FS
587 else if (programType == salvageServer) {
589 Log("Unable to connect to file server; aborted\n");
593 #endif /* AFS_DEMAND_ATTACH_FS */
594 #endif /* FSSYNC_BUILD_CLIENT */
598 #ifdef AFS_PTHREAD_ENV
600 VInitVolumePackageThread(void * args) {
601 int errors = 0; /* Number of errors while finding vice partitions. */
605 struct DiskPartition *diskP;
606 struct vinitvolumepackage_thread_t * params;
607 struct diskpartition_queue_t * dpq;
609 params = (vinitvolumepackage_thread_t *) args;
613 /* Attach all the volumes in this partition */
614 while (queue_IsNotEmpty(params)) {
615 int nAttached = 0, nUnattached = 0;
617 dpq = queue_First(params,diskpartition_queue_t);
623 assert(VAttachVolumesByPartition(diskP, &nAttached, &nUnattached) == 0);
628 params->n_threads_complete++;
629 pthread_cond_signal(¶ms->thread_done_cv);
633 #endif /* AFS_PTHREAD_ENV */
636 * attach all volumes on a given disk partition
639 VAttachVolumesByPartition(struct DiskPartition *diskP, int * nAttached, int * nUnattached)
645 Log("Partition %s: attaching volumes\n", diskP->name);
646 dirp = opendir(VPartitionPath(diskP));
648 Log("opendir on Partition %s failed!\n", diskP->name);
652 while ((dp = readdir(dirp))) {
654 p = strrchr(dp->d_name, '.');
655 if (p != NULL && strcmp(p, VHDREXT) == 0) {
658 #ifdef AFS_DEMAND_ATTACH_FS
659 vp = VPreAttachVolumeByName(&error, diskP->name, dp->d_name);
660 #else /* AFS_DEMAND_ATTACH_FS */
661 vp = VAttachVolumeByName(&error, diskP->name, dp->d_name,
663 #endif /* AFS_DEMAND_ATTACH_FS */
664 (*(vp ? nAttached : nUnattached))++;
665 if (error == VOFFLINE)
666 Log("Volume %d stays offline (/vice/offline/%s exists)\n", VolumeNumber(dp->d_name), dp->d_name);
667 else if (LogLevel >= 5) {
668 Log("Partition %s: attached volume %d (%s)\n",
669 diskP->name, VolumeNumber(dp->d_name),
672 #if !defined(AFS_DEMAND_ATTACH_FS)
676 #endif /* AFS_DEMAND_ATTACH_FS */
680 Log("Partition %s: attached %d volumes; %d volumes not attached\n", diskP->name, *nAttached, *nUnattached);
686 /***************************************************/
687 /* Shutdown routines */
688 /***************************************************/
692 * highly multithreaded volume package shutdown
694 * with the demand attach fileserver extensions,
695 * VShutdown has been modified to be multithreaded.
696 * In order to achieve optimal use of many threads,
697 * the shutdown code involves one control thread and
698 * n shutdown worker threads. The control thread
699 * periodically examines the number of volumes available
700 * for shutdown on each partition, and produces a worker
701 * thread allocation schedule. The idea is to eliminate
702 * redundant scheduling computation on the workers by
703 * having a single master scheduler.
705 * The scheduler's objectives are:
707 * each partition with volumes remaining gets allocated
708 * at least 1 thread (assuming sufficient threads)
710 * threads are allocated proportional to the number of
711 * volumes remaining to be offlined. This ensures that
712 * the OS I/O scheduler has many requests to elevator
713 * seek on partitions that will (presumably) take the
714 * longest amount of time (from now) to finish shutdown
715 * (3) keep threads busy
716 * when there are extra threads, they are assigned to
717 * partitions using a simple round-robin algorithm
719 * In the future, we may wish to add the ability to adapt
720 * to the relative performance patterns of each disk
725 * multi-step shutdown process
727 * demand attach shutdown is a four-step process. Each
728 * shutdown "pass" shuts down increasingly more difficult
729 * volumes. The main purpose is to achieve better cache
730 * utilization during shutdown.
733 * shutdown volumes in the unattached, pre-attached
736 * shutdown attached volumes with cached volume headers
738 * shutdown all volumes in non-exclusive states
740 * shutdown all remaining volumes
747 register Volume *vp, *np;
748 register afs_int32 code;
749 #ifdef AFS_DEMAND_ATTACH_FS
750 struct DiskPartition * diskP;
751 struct diskpartition_queue_t * dpq;
752 vshutdown_thread_t params;
754 pthread_attr_t attrs;
756 memset(¶ms, 0, sizeof(vshutdown_thread_t));
758 for (params.n_parts=0, diskP = DiskPartitionList;
759 diskP; diskP = diskP->next, params.n_parts++);
761 Log("VShutdown: shutting down on-line volumes on %d partition%s...\n",
762 params.n_parts, params.n_parts > 1 ? "s" : "");
764 if (vol_attach_threads > 1) {
765 /* prepare for parallel shutdown */
766 params.n_threads = vol_attach_threads;
767 assert(pthread_mutex_init(¶ms.lock, NULL) == 0);
768 assert(pthread_cond_init(¶ms.cv, NULL) == 0);
769 assert(pthread_cond_init(¶ms.master_cv, NULL) == 0);
770 assert(pthread_attr_init(&attrs) == 0);
771 assert(pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED) == 0);
774 /* setup the basic partition information structures for
775 * parallel shutdown */
776 for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
778 struct rx_queue * qp, * nqp;
782 VVByPListWait_r(diskP);
783 VVByPListBeginExclusive_r(diskP);
786 for (queue_Scan(&diskP->vol_list, qp, nqp, rx_queue)) {
787 vp = (Volume *)((char *)qp - offsetof(Volume, vol_list));
791 Log("VShutdown: partition %s has %d volumes with attached headers\n",
792 VPartitionPath(diskP), count);
795 /* build up the pass 0 shutdown work queue */
796 dpq = (struct diskpartition_queue_t *) malloc(sizeof(struct diskpartition_queue_t));
799 queue_Prepend(¶ms, dpq);
801 params.part_pass_head[diskP->device] = queue_First(&diskP->vol_list, rx_queue);
804 Log("VShutdown: beginning parallel fileserver shutdown\n");
805 Log("VShutdown: using %d threads to offline volumes on %d partition%s\n",
806 vol_attach_threads, params.n_parts, params.n_parts > 1 ? "s" : "" );
808 /* do pass 0 shutdown */
809 assert(pthread_mutex_lock(¶ms.lock) == 0);
810 for (i=0; i < params.n_threads; i++) {
811 assert(pthread_create
812 (&tid, &attrs, &VShutdownThread,
816 /* wait for all the pass 0 shutdowns to complete */
817 while (params.n_threads_complete < params.n_threads) {
818 assert(pthread_cond_wait(¶ms.master_cv, ¶ms.lock) == 0);
820 params.n_threads_complete = 0;
822 assert(pthread_cond_broadcast(¶ms.cv) == 0);
823 assert(pthread_mutex_unlock(¶ms.lock) == 0);
825 Log("VShutdown: pass 0 completed using the 1 thread per partition algorithm\n");
826 Log("VShutdown: starting passes 1 through 3 using finely-granular mp-fast algorithm\n");
828 /* run the parallel shutdown scheduler. it will drop the glock internally */
829 ShutdownController(¶ms);
831 /* wait for all the workers to finish pass 3 and terminate */
832 while (params.pass < 4) {
833 VOL_CV_WAIT(¶ms.cv);
836 assert(pthread_attr_destroy(&attrs) == 0);
837 assert(pthread_cond_destroy(¶ms.cv) == 0);
838 assert(pthread_cond_destroy(¶ms.master_cv) == 0);
839 assert(pthread_mutex_destroy(¶ms.lock) == 0);
841 /* drop the VByPList exclusive reservations */
842 for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
843 VVByPListEndExclusive_r(diskP);
844 Log("VShutdown: %s stats : (pass[0]=%d, pass[1]=%d, pass[2]=%d, pass[3]=%d)\n",
845 VPartitionPath(diskP),
846 params.stats[0][diskP->device],
847 params.stats[1][diskP->device],
848 params.stats[2][diskP->device],
849 params.stats[3][diskP->device]);
852 Log("VShutdown: shutdown finished using %d threads\n", params.n_threads);
854 /* if we're only going to run one shutdown thread, don't bother creating
856 Log("VShutdown: beginning single-threaded fileserver shutdown\n");
858 for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
859 VShutdownByPartition_r(diskP);
863 Log("VShutdown: complete.\n");
864 #else /* AFS_DEMAND_ATTACH_FS */
865 Log("VShutdown: shutting down on-line volumes...\n");
866 for (i = 0; i < VolumeHashTable.Size; i++) {
867 /* try to hold first volume in the hash table */
868 for (queue_Scan(&VolumeHashTable.Table[i],vp,np,Volume)) {
872 Log("VShutdown: Attempting to take volume %u offline.\n",
875 /* next, take the volume offline (drops reference count) */
876 VOffline_r(vp, "File server was shut down");
880 Log("VShutdown: complete.\n");
881 #endif /* AFS_DEMAND_ATTACH_FS */
892 #ifdef AFS_DEMAND_ATTACH_FS
895 * shutdown control thread
898 ShutdownController(vshutdown_thread_t * params)
901 struct DiskPartition * diskP;
903 vshutdown_thread_t shadow;
905 ShutdownCreateSchedule(params);
907 while ((params->pass < 4) &&
908 (params->n_threads_complete < params->n_threads)) {
909 /* recompute schedule once per second */
911 memcpy(&shadow, params, sizeof(vshutdown_thread_t));
915 Log("ShutdownController: schedule version=%d, vol_remaining=%d, pass=%d\n",
916 shadow.schedule_version, shadow.vol_remaining, shadow.pass);
917 Log("ShutdownController: n_threads_complete=%d, n_parts_done_pass=%d\n",
918 shadow.n_threads_complete, shadow.n_parts_done_pass);
919 for (diskP = DiskPartitionList; diskP; diskP=diskP->next) {
921 Log("ShutdownController: part[%d] : (len=%d, thread_target=%d, done_pass=%d, pass_head=%p)\n",
924 shadow.part_thread_target[id],
925 shadow.part_done_pass[id],
926 shadow.part_pass_head[id]);
932 ShutdownCreateSchedule(params);
936 /* create the shutdown thread work schedule.
937 * this scheduler tries to implement fairness
938 * by allocating at least 1 thread to each
939 * partition with volumes to be shutdown,
940 * and then it attempts to allocate remaining
941 * threads based upon the amount of work left
944 ShutdownCreateSchedule(vshutdown_thread_t * params)
946 struct DiskPartition * diskP;
947 int sum, thr_workload, thr_left;
948 int part_residue[VOLMAXPARTS+1];
951 /* compute the total number of outstanding volumes */
953 for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
954 sum += diskP->vol_list.len;
957 params->schedule_version++;
958 params->vol_remaining = sum;
963 /* compute average per-thread workload */
964 thr_workload = sum / params->n_threads;
965 if (sum % params->n_threads)
968 thr_left = params->n_threads;
969 memset(&part_residue, 0, sizeof(part_residue));
971 /* for fairness, give every partition with volumes remaining
972 * at least one thread */
973 for (diskP = DiskPartitionList; diskP && thr_left; diskP = diskP->next) {
975 if (diskP->vol_list.len) {
976 params->part_thread_target[id] = 1;
979 params->part_thread_target[id] = 0;
983 if (thr_left && thr_workload) {
984 /* compute length-weighted workloads */
987 for (diskP = DiskPartitionList; diskP && thr_left; diskP = diskP->next) {
989 delta = (diskP->vol_list.len / thr_workload) -
990 params->part_thread_target[id];
994 if (delta < thr_left) {
995 params->part_thread_target[id] += delta;
998 params->part_thread_target[id] += thr_left;
1006 /* try to assign any leftover threads to partitions that
1007 * had volume lengths closer to needing thread_target+1 */
1008 int max_residue, max_id;
1010 /* compute the residues */
1011 for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
1013 part_residue[id] = diskP->vol_list.len -
1014 (params->part_thread_target[id] * thr_workload);
1017 /* now try to allocate remaining threads to partitions with the
1018 * highest residues */
1021 for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
1023 if (part_residue[id] > max_residue) {
1024 max_residue = part_residue[id];
1033 params->part_thread_target[max_id]++;
1035 part_residue[max_id] = 0;
1040 /* punt and give any remaining threads equally to each partition */
1042 if (thr_left >= params->n_parts) {
1043 alloc = thr_left / params->n_parts;
1044 for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
1046 params->part_thread_target[id] += alloc;
1051 /* finish off the last of the threads */
1052 for (diskP = DiskPartitionList; thr_left && diskP; diskP = diskP->next) {
1054 params->part_thread_target[id]++;
1060 /* worker thread for parallel shutdown */
1062 VShutdownThread(void * args)
1064 struct rx_queue *qp;
1066 vshutdown_thread_t * params;
1067 int part, code, found, pass, schedule_version_save, count;
1068 struct DiskPartition *diskP;
1069 struct diskpartition_queue_t * dpq;
1072 params = (vshutdown_thread_t *) args;
1074 /* acquire the shutdown pass 0 lock */
1075 assert(pthread_mutex_lock(¶ms->lock) == 0);
1077 /* if there's still pass 0 work to be done,
1078 * get a work entry, and do a pass 0 shutdown */
1079 if (queue_IsNotEmpty(params)) {
1080 dpq = queue_First(params, diskpartition_queue_t);
1082 assert(pthread_mutex_unlock(¶ms->lock) == 0);
1088 while (ShutdownVolumeWalk_r(diskP, 0, ¶ms->part_pass_head[id]))
1090 params->stats[0][diskP->device] = count;
1091 assert(pthread_mutex_lock(¶ms->lock) == 0);
1094 params->n_threads_complete++;
1095 if (params->n_threads_complete == params->n_threads) {
1096 /* notify control thread that all workers have completed pass 0 */
1097 assert(pthread_cond_signal(¶ms->master_cv) == 0);
1099 while (params->pass == 0) {
1100 assert(pthread_cond_wait(¶ms->cv, ¶ms->lock) == 0);
1104 assert(pthread_mutex_unlock(¶ms->lock) == 0);
1107 pass = params->pass;
1110 /* now escalate through the more complicated shutdowns */
1112 schedule_version_save = params->schedule_version;
1114 /* find a disk partition to work on */
1115 for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
1117 if (params->part_thread_target[id] && !params->part_done_pass[id]) {
1118 params->part_thread_target[id]--;
1125 /* hmm. for some reason the controller thread couldn't find anything for
1126 * us to do. let's see if there's anything we can do */
1127 for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
1129 if (diskP->vol_list.len && !params->part_done_pass[id]) {
1132 } else if (!params->part_done_pass[id]) {
1133 params->part_done_pass[id] = 1;
1134 params->n_parts_done_pass++;
1136 Log("VShutdown: done shutting down volumes on partition %s.\n",
1137 VPartitionPath(diskP));
1143 /* do work on this partition until either the controller
1144 * creates a new schedule, or we run out of things to do
1145 * on this partition */
1148 while (!params->part_done_pass[id] &&
1149 (schedule_version_save == params->schedule_version)) {
1150 /* ShutdownVolumeWalk_r will drop the glock internally */
1151 if (!ShutdownVolumeWalk_r(diskP, pass, ¶ms->part_pass_head[id])) {
1152 if (!params->part_done_pass[id]) {
1153 params->part_done_pass[id] = 1;
1154 params->n_parts_done_pass++;
1156 Log("VShutdown: done shutting down volumes on partition %s.\n",
1157 VPartitionPath(diskP));
1165 params->stats[pass][id] += count;
1167 /* ok, everyone is done this pass, proceed */
1170 params->n_threads_complete++;
1171 while (params->pass == pass) {
1172 if (params->n_threads_complete == params->n_threads) {
1173 /* we are the last thread to complete, so we will
1174 * reinitialize worker pool state for the next pass */
1175 params->n_threads_complete = 0;
1176 params->n_parts_done_pass = 0;
1178 for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
1180 params->part_done_pass[id] = 0;
1181 params->part_pass_head[id] = queue_First(&diskP->vol_list, rx_queue);
1184 /* compute a new thread schedule before releasing all the workers */
1185 ShutdownCreateSchedule(params);
1187 /* wake up all the workers */
1188 assert(pthread_cond_broadcast(¶ms->cv) == 0);
1191 Log("VShutdown: pass %d completed using %d threads on %d partitions\n",
1192 pass, params->n_threads, params->n_parts);
1195 VOL_CV_WAIT(¶ms->cv);
1198 pass = params->pass;
1212 /* shut down all volumes on a given disk partition
1214 * note that this function will not allow mp-fast
1215 * shutdown of a partition */
1217 VShutdownByPartition_r(struct DiskPartition * dp)
1223 /* wait for other exclusive ops to finish */
1224 VVByPListWait_r(dp);
1226 /* begin exclusive access */
1227 VVByPListBeginExclusive_r(dp);
1229 /* pick the low-hanging fruit first,
1230 * then do the complicated ones last
1231 * (has the advantage of keeping
1232 * in-use volumes up until the bitter end) */
1233 for (pass = 0, total=0; pass < 4; pass++) {
1234 pass_stats[pass] = ShutdownVByPForPass_r(dp, pass);
1235 total += pass_stats[pass];
1238 /* end exclusive access */
1239 VVByPListEndExclusive_r(dp);
1241 Log("VShutdownByPartition: shut down %d volumes on %s (pass[0]=%d, pass[1]=%d, pass[2]=%d, pass[3]=%d)\n",
1242 total, VPartitionPath(dp), pass_stats[0], pass_stats[1], pass_stats[2], pass_stats[3]);
1247 /* internal shutdown functionality
1249 * for multi-pass shutdown:
1250 * 0 to only "shutdown" {pre,un}attached and error state volumes
1251 * 1 to also shutdown attached volumes w/ volume header loaded
1252 * 2 to also shutdown attached volumes w/o volume header loaded
1253 * 3 to also shutdown exclusive state volumes
1255 * caller MUST hold exclusive access on the hash chain
1256 * because we drop vol_glock_mutex internally
1258 * this function is reentrant for passes 1--3
1259 * (e.g. multiple threads can cooperate to
1260 * shutdown a partition mp-fast)
1262 * pass 0 is not scaleable because the volume state data is
1263 * synchronized by vol_glock mutex, and the locking overhead
1264 * is too high to drop the lock long enough to do linked list
1268 ShutdownVByPForPass_r(struct DiskPartition * dp, int pass)
1270 struct rx_queue * q = queue_First(&dp->vol_list, rx_queue);
1273 while (ShutdownVolumeWalk_r(dp, pass, &q))
1279 /* conditionally shutdown one volume on partition dp
1280 * returns 1 if a volume was shutdown in this pass,
1283 ShutdownVolumeWalk_r(struct DiskPartition * dp, int pass,
1284 struct rx_queue ** idx)
1286 struct rx_queue *qp, *nqp;
1291 for (queue_ScanFrom(&dp->vol_list, qp, qp, nqp, rx_queue)) {
1292 vp = (Volume *) (((char *)qp) - offsetof(Volume, vol_list));
1296 if ((V_attachState(vp) != VOL_STATE_UNATTACHED) &&
1297 (V_attachState(vp) != VOL_STATE_ERROR) &&
1298 (V_attachState(vp) != VOL_STATE_PREATTACHED)) {
1302 if ((V_attachState(vp) == VOL_STATE_ATTACHED) &&
1303 (vp->header == NULL)) {
1307 if (VIsExclusiveState(V_attachState(vp))) {
1312 DeleteVolumeFromVByPList_r(vp);
1313 VShutdownVolume_r(vp);
1323 * shutdown a specific volume
1325 /* caller MUST NOT hold a heavyweight ref on vp */
1327 VShutdownVolume_r(Volume * vp)
1331 VCreateReservation_r(vp);
1333 if (LogLevel >= 5) {
1334 Log("VShutdownVolume_r: vid=%u, device=%d, state=%hu\n",
1335 vp->hashid, vp->partition->device, V_attachState(vp));
1338 /* wait for other blocking ops to finish */
1339 VWaitExclusiveState_r(vp);
1341 assert(VIsValidState(V_attachState(vp)));
1343 switch(V_attachState(vp)) {
1344 case VOL_STATE_SALVAGING:
1345 /* make sure salvager knows we don't want
1346 * the volume back */
1347 VCancelSalvage_r(vp, SALVSYNC_SHUTDOWN);
1348 case VOL_STATE_PREATTACHED:
1349 case VOL_STATE_ERROR:
1350 VChangeState_r(vp, VOL_STATE_UNATTACHED);
1351 case VOL_STATE_UNATTACHED:
1353 case VOL_STATE_GOING_OFFLINE:
1354 case VOL_STATE_SHUTTING_DOWN:
1355 case VOL_STATE_ATTACHED:
1359 Log("VShutdown: Attempting to take volume %u offline.\n",
1362 /* take the volume offline (drops reference count) */
1363 VOffline_r(vp, "File server was shut down");
1368 VCancelReservation_r(vp);
1372 #endif /* AFS_DEMAND_ATTACH_FS */
1375 /***************************************************/
1376 /* Header I/O routines */
1377 /***************************************************/
1379 /* open a descriptor for the inode (h),
1380 * read in an on-disk structure into buffer (to) of size (size),
1381 * verify versionstamp in structure has magic (magic) and
1382 * optionally verify version (version) if (version) is nonzero
1385 ReadHeader(Error * ec, IHandle_t * h, char *to, int size, bit32 magic,
1388 struct versionStamp *vsn;
1403 if (FDH_SEEK(fdP, 0, SEEK_SET) < 0) {
1405 FDH_REALLYCLOSE(fdP);
1408 vsn = (struct versionStamp *)to;
1409 if (FDH_READ(fdP, to, size) != size || vsn->magic != magic) {
1411 FDH_REALLYCLOSE(fdP);
1416 /* Check is conditional, in case caller wants to inspect version himself */
1417 if (version && vsn->version != version) {
1423 WriteVolumeHeader_r(Error * ec, Volume * vp)
1425 IHandle_t *h = V_diskDataHandle(vp);
1435 if (FDH_SEEK(fdP, 0, SEEK_SET) < 0) {
1437 FDH_REALLYCLOSE(fdP);
1440 if (FDH_WRITE(fdP, (char *)&V_disk(vp), sizeof(V_disk(vp)))
1441 != sizeof(V_disk(vp))) {
1443 FDH_REALLYCLOSE(fdP);
1449 /* VolumeHeaderToDisk
1450 * Allows for storing 64 bit inode numbers in on-disk volume header
1453 /* convert in-memory representation of a volume header to the
1454 * on-disk representation of a volume header */
1456 VolumeHeaderToDisk(VolumeDiskHeader_t * dh, VolumeHeader_t * h)
1459 memset((char *)dh, 0, sizeof(VolumeDiskHeader_t));
1460 dh->stamp = h->stamp;
1462 dh->parent = h->parent;
1464 #ifdef AFS_64BIT_IOPS_ENV
1465 dh->volumeInfo_lo = (afs_int32) h->volumeInfo & 0xffffffff;
1466 dh->volumeInfo_hi = (afs_int32) (h->volumeInfo >> 32) & 0xffffffff;
1467 dh->smallVnodeIndex_lo = (afs_int32) h->smallVnodeIndex & 0xffffffff;
1468 dh->smallVnodeIndex_hi =
1469 (afs_int32) (h->smallVnodeIndex >> 32) & 0xffffffff;
1470 dh->largeVnodeIndex_lo = (afs_int32) h->largeVnodeIndex & 0xffffffff;
1471 dh->largeVnodeIndex_hi =
1472 (afs_int32) (h->largeVnodeIndex >> 32) & 0xffffffff;
1473 dh->linkTable_lo = (afs_int32) h->linkTable & 0xffffffff;
1474 dh->linkTable_hi = (afs_int32) (h->linkTable >> 32) & 0xffffffff;
1476 dh->volumeInfo_lo = h->volumeInfo;
1477 dh->smallVnodeIndex_lo = h->smallVnodeIndex;
1478 dh->largeVnodeIndex_lo = h->largeVnodeIndex;
1479 dh->linkTable_lo = h->linkTable;
1483 /* DiskToVolumeHeader
1484 * Converts an on-disk representation of a volume header to
1485 * the in-memory representation of a volume header.
1487 * Makes the assumption that AFS has *always*
1488 * zero'd the volume header file so that high parts of inode
1489 * numbers are 0 in older (SGI EFS) volume header files.
1492 DiskToVolumeHeader(VolumeHeader_t * h, VolumeDiskHeader_t * dh)
1494 memset((char *)h, 0, sizeof(VolumeHeader_t));
1495 h->stamp = dh->stamp;
1497 h->parent = dh->parent;
1499 #ifdef AFS_64BIT_IOPS_ENV
1501 (Inode) dh->volumeInfo_lo | ((Inode) dh->volumeInfo_hi << 32);
1503 h->smallVnodeIndex =
1504 (Inode) dh->smallVnodeIndex_lo | ((Inode) dh->
1505 smallVnodeIndex_hi << 32);
1507 h->largeVnodeIndex =
1508 (Inode) dh->largeVnodeIndex_lo | ((Inode) dh->
1509 largeVnodeIndex_hi << 32);
1511 (Inode) dh->linkTable_lo | ((Inode) dh->linkTable_hi << 32);
1513 h->volumeInfo = dh->volumeInfo_lo;
1514 h->smallVnodeIndex = dh->smallVnodeIndex_lo;
1515 h->largeVnodeIndex = dh->largeVnodeIndex_lo;
1516 h->linkTable = dh->linkTable_lo;
1521 /***************************************************/
1522 /* Volume Attachment routines */
1523 /***************************************************/
1525 #ifdef AFS_DEMAND_ATTACH_FS
1527 * pre-attach a volume given its path.
1529 * @param[out] ec outbound error code
1530 * @param[in] partition partition path string
1531 * @param[in] name volume id string
1533 * @return volume object pointer
1535 * @note A pre-attached volume will only have its partition
1536 * and hashid fields initialized. At first call to
1537 * VGetVolume, the volume will be fully attached.
1541 VPreAttachVolumeByName(Error * ec, char *partition, char *name)
1545 vp = VPreAttachVolumeByName_r(ec, partition, name);
1551 * pre-attach a volume given its path.
1553 * @param[out] ec outbound error code
1554 * @param[in] partition path to vice partition
1555 * @param[in] name volume id string
1557 * @return volume object pointer
1559 * @pre VOL_LOCK held
1561 * @internal volume package internal use only.
1564 VPreAttachVolumeByName_r(Error * ec, char *partition, char *name)
1566 return VPreAttachVolumeById_r(ec,
1568 VolumeNumber(name));
1572 * pre-attach a volume given its path and numeric volume id.
1574 * @param[out] ec error code return
1575 * @param[in] partition path to vice partition
1576 * @param[in] volumeId numeric volume id
1578 * @return volume object pointer
1580 * @pre VOL_LOCK held
1582 * @internal volume package internal use only.
1585 VPreAttachVolumeById_r(Error * ec,
1590 struct DiskPartition *partp;
1594 assert(programType == fileServer);
1596 if (!(partp = VGetPartition_r(partition, 0))) {
1598 Log("VPreAttachVolumeById_r: Error getting partition (%s)\n", partition);
1602 vp = VLookupVolume_r(ec, volumeId, NULL);
1607 return VPreAttachVolumeByVp_r(ec, partp, vp, volumeId);
1611 * preattach a volume.
1613 * @param[out] ec outbound error code
1614 * @param[in] partp pointer to partition object
1615 * @param[in] vp pointer to volume object
1616 * @param[in] vid volume id
1618 * @return volume object pointer
1620 * @pre VOL_LOCK is held.
1622 * @warning Returned volume object pointer does not have to
1623 * equal the pointer passed in as argument vp. There
1624 * are potential race conditions which can result in
1625 * the pointers having different values. It is up to
1626 * the caller to make sure that references are handled
1627 * properly in this case.
1629 * @note If there is already a volume object registered with
1630 * the same volume id, its pointer MUST be passed as
1631 * argument vp. Failure to do so will result in a silent
1632 * failure to preattach.
1634 * @internal volume package internal use only.
1637 VPreAttachVolumeByVp_r(Error * ec,
1638 struct DiskPartition * partp,
1646 /* check to see if pre-attach already happened */
1648 (V_attachState(vp) != VOL_STATE_UNATTACHED) &&
1649 !VIsErrorState(V_attachState(vp)) &&
1650 ((V_attachState(vp) != VOL_STATE_PREATTACHED) ||
1651 vp->pending_vol_op == NULL)) {
1653 * pre-attach is a no-op in all but the following cases:
1655 * - volume is unattached
1656 * - volume is in an error state
1657 * - volume is pre-attached with a pending volume operation
1658 * (e.g. vos move between two partitions on same server)
1662 /* we're re-attaching a volume; clear out some old state */
1663 memset(&vp->salvage, 0, sizeof(struct VolumeOnlineSalvage));
1665 if (V_partition(vp) != partp) {
1666 /* XXX potential race */
1667 DeleteVolumeFromVByPList_r(vp);
1670 /* if we need to allocate a new Volume struct,
1671 * go ahead and drop the vol glock, otherwise
1672 * do the basic setup synchronised, as it's
1673 * probably not worth dropping the lock */
1676 /* allocate the volume structure */
1677 vp = nvp = (Volume *) malloc(sizeof(Volume));
1679 memset(vp, 0, sizeof(Volume));
1680 queue_Init(&vp->vnode_list);
1681 assert(pthread_cond_init(&V_attachCV(vp), NULL) == 0);
1684 /* link the volume with its associated vice partition */
1685 vp->device = partp->device;
1686 vp->partition = partp;
1689 /* if we dropped the lock, reacquire the lock,
1690 * check for pre-attach races, and then add
1691 * the volume to the hash table */
1694 nvp = VLookupVolume_r(ec, vid, NULL);
1699 } else if (nvp) { /* race detected */
1704 /* hack to make up for VChangeState_r() decrementing
1705 * the old state counter */
1706 VStats.state_levels[0]++;
1710 /* put pre-attached volume onto the hash table
1711 * and bring it up to the pre-attached state */
1712 AddVolumeToHashTable(vp, vp->hashid);
1713 AddVolumeToVByPList_r(vp);
1714 VLRU_Init_Node_r(vp);
1715 VChangeState_r(vp, VOL_STATE_PREATTACHED);
1718 Log("VPreAttachVolumeByVp_r: volume %u pre-attached\n", vp->hashid);
1726 #endif /* AFS_DEMAND_ATTACH_FS */
1728 /* Attach an existing volume, given its pathname, and return a
1729 pointer to the volume header information. The volume also
1730 normally goes online at this time. An offline volume
1731 must be reattached to make it go online */
1733 VAttachVolumeByName(Error * ec, char *partition, char *name, int mode)
1737 retVal = VAttachVolumeByName_r(ec, partition, name, mode);
1743 VAttachVolumeByName_r(Error * ec, char *partition, char *name, int mode)
1745 register Volume *vp = NULL, *svp = NULL;
1747 struct afs_stat status;
1748 struct VolumeDiskHeader diskHeader;
1749 struct VolumeHeader iheader;
1750 struct DiskPartition *partp;
1754 #ifdef AFS_DEMAND_ATTACH_FS
1755 VolumeStats stats_save;
1756 #endif /* AFS_DEMAND_ATTACH_FS */
1760 volumeId = VolumeNumber(name);
1762 if (!(partp = VGetPartition_r(partition, 0))) {
1764 Log("VAttachVolume: Error getting partition (%s)\n", partition);
1768 if (programType == volumeUtility) {
1770 VLockPartition_r(partition);
1771 } else if (programType == fileServer) {
1772 #ifdef AFS_DEMAND_ATTACH_FS
1773 /* lookup the volume in the hash table */
1774 vp = VLookupVolume_r(ec, volumeId, NULL);
1780 /* save any counters that are supposed to
1781 * be monotonically increasing over the
1782 * lifetime of the fileserver */
1783 memcpy(&stats_save, &vp->stats, sizeof(VolumeStats));
1785 memset(&stats_save, 0, sizeof(VolumeStats));
1788 /* if there's something in the hash table, and it's not
1789 * in the pre-attach state, then we may need to detach
1790 * it before proceeding */
1791 if (vp && (V_attachState(vp) != VOL_STATE_PREATTACHED)) {
1792 VCreateReservation_r(vp);
1793 VWaitExclusiveState_r(vp);
1795 /* at this point state must be one of:
1804 if (vp->specialStatus == VBUSY)
1807 /* if it's already attached, see if we can return it */
1808 if (V_attachState(vp) == VOL_STATE_ATTACHED) {
1809 VGetVolumeByVp_r(ec, vp);
1811 VCancelReservation_r(vp);
1815 /* otherwise, we need to detach, and attempt to re-attach */
1816 VDetachVolume_r(ec, vp);
1818 Log("VAttachVolume: Error detaching old volume instance (%s)\n", name);
1821 /* if it isn't fully attached, delete from the hash tables,
1822 and let the refcounter handle the rest */
1823 DeleteVolumeFromHashTable(vp);
1824 DeleteVolumeFromVByPList_r(vp);
1827 VCancelReservation_r(vp);
1831 /* pre-attach volume if it hasn't been done yet */
1833 (V_attachState(vp) == VOL_STATE_UNATTACHED) ||
1834 (V_attachState(vp) == VOL_STATE_ERROR)) {
1836 vp = VPreAttachVolumeByVp_r(ec, partp, vp, volumeId);
1844 /* handle pre-attach races
1846 * multiple threads can race to pre-attach a volume,
1847 * but we can't let them race beyond that
1849 * our solution is to let the first thread to bring
1850 * the volume into an exclusive state win; the other
1851 * threads just wait until it finishes bringing the
1852 * volume online, and then they do a vgetvolumebyvp
1854 if (svp && (svp != vp)) {
1855 /* wait for other exclusive ops to finish */
1856 VCreateReservation_r(vp);
1857 VWaitExclusiveState_r(vp);
1859 /* get a heavyweight ref, kill the lightweight ref, and return */
1860 VGetVolumeByVp_r(ec, vp);
1861 VCancelReservation_r(vp);
1865 /* at this point, we are chosen as the thread to do
1866 * demand attachment for this volume. all other threads
1867 * doing a getvolume on vp->hashid will block until we finish */
1869 /* make sure any old header cache entries are invalidated
1870 * before proceeding */
1871 FreeVolumeHeader(vp);
1873 VChangeState_r(vp, VOL_STATE_ATTACHING);
1875 /* restore any saved counters */
1876 memcpy(&vp->stats, &stats_save, sizeof(VolumeStats));
1877 #else /* AFS_DEMAND_ATTACH_FS */
1878 vp = VGetVolume_r(ec, volumeId);
1882 if (vp->specialStatus == VBUSY)
1884 VDetachVolume_r(ec, vp);
1886 Log("VAttachVolume: Error detaching volume (%s)\n", name);
1890 #endif /* AFS_DEMAND_ATTACH_FS */
1894 strcpy(path, VPartitionPath(partp));
1900 if ((fd = afs_open(path, O_RDONLY)) == -1 || afs_fstat(fd, &status) == -1) {
1901 Log("VAttachVolume: Failed to open %s (errno %d)\n", path, errno);
1908 n = read(fd, &diskHeader, sizeof(diskHeader));
1910 if (n != sizeof(diskHeader)
1911 || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
1912 Log("VAttachVolume: Error reading volume header %s\n", path);
1917 if (diskHeader.stamp.version != VOLUMEHEADERVERSION) {
1918 Log("VAttachVolume: Volume %s, version number is incorrect; volume needs salvaged\n", path);
1924 DiskToVolumeHeader(&iheader, &diskHeader);
1925 #ifdef FSSYNC_BUILD_CLIENT
1926 if (programType == volumeUtility && mode != V_SECRETLY && mode != V_PEEK) {
1928 if (FSYNC_VolOp(iheader.id, partition, FSYNC_VOL_NEEDVOLUME, mode, NULL)
1930 Log("VAttachVolume: attach of volume %u apparently denied by file server\n", iheader.id);
1931 *ec = VNOVOL; /* XXXX */
1939 vp = (Volume *) calloc(1, sizeof(Volume));
1941 vp->device = partp->device;
1942 vp->partition = partp;
1943 queue_Init(&vp->vnode_list);
1944 #ifdef AFS_DEMAND_ATTACH_FS
1945 assert(pthread_cond_init(&V_attachCV(vp), NULL) == 0);
1946 #endif /* AFS_DEMAND_ATTACH_FS */
1949 /* attach2 is entered without any locks, and returns
1950 * with vol_glock_mutex held */
1951 vp = attach2(ec, volumeId, path, &iheader, partp, vp, isbusy, mode);
1953 if (programType == volumeUtility && vp) {
1954 #ifdef AFS_DEMAND_ATTACH_FS
1955 /* for dafs, we should tell the fileserver, except for V_PEEK
1956 * where we know it is not necessary */
1957 if (mode == V_PEEK) {
1958 vp->needsPutBack = 0;
1960 vp->needsPutBack = 1;
1962 #else /* !AFS_DEMAND_ATTACH_FS */
1963 /* duplicate computation in fssync.c about whether the server
1964 * takes the volume offline or not. If the volume isn't
1965 * offline, we must not return it when we detach the volume,
1966 * or the server will abort */
1967 if (mode == V_READONLY || mode == V_PEEK
1968 || (!VolumeWriteable(vp) && (mode == V_CLONE || mode == V_DUMP)))
1969 vp->needsPutBack = 0;
1971 vp->needsPutBack = 1;
1972 #endif /* !AFS_DEMAND_ATTACH_FS */
1974 /* OK, there's a problem here, but one that I don't know how to
1975 * fix right now, and that I don't think should arise often.
1976 * Basically, we should only put back this volume to the server if
1977 * it was given to us by the server, but since we don't have a vp,
1978 * we can't run the VolumeWriteable function to find out as we do
1979 * above when computing vp->needsPutBack. So we send it back, but
1980 * there's a path in VAttachVolume on the server which may abort
1981 * if this volume doesn't have a header. Should be pretty rare
1982 * for all of that to happen, but if it does, probably the right
1983 * fix is for the server to allow the return of readonly volumes
1984 * that it doesn't think are really checked out. */
1985 #ifdef FSSYNC_BUILD_CLIENT
1986 if (programType == volumeUtility && vp == NULL &&
1987 mode != V_SECRETLY && mode != V_PEEK) {
1988 FSYNC_VolOp(iheader.id, partition, FSYNC_VOL_ON, 0, NULL);
1991 if (programType == fileServer && vp) {
1992 #ifdef AFS_DEMAND_ATTACH_FS
1994 * we can get here in cases where we don't "own"
1995 * the volume (e.g. volume owned by a utility).
1996 * short circuit around potential disk header races.
1998 if (V_attachState(vp) != VOL_STATE_ATTACHED) {
2002 V_needsCallback(vp) = 0;
2004 if (VInit >= 2 && V_BreakVolumeCallbacks) {
2005 Log("VAttachVolume: Volume %u was changed externally; breaking callbacks\n", V_id(vp));
2006 (*V_BreakVolumeCallbacks) (V_id(vp));
2009 VUpdateVolume_r(ec, vp, 0);
2011 Log("VAttachVolume: Error updating volume\n");
2016 if (VolumeWriteable(vp) && V_dontSalvage(vp) == 0) {
2017 #ifndef AFS_DEMAND_ATTACH_FS
2018 /* This is a hack: by temporarily setting the incore
2019 * dontSalvage flag ON, the volume will be put back on the
2020 * Update list (with dontSalvage OFF again). It will then
2021 * come back in N minutes with DONT_SALVAGE eventually
2022 * set. This is the way that volumes that have never had
2023 * it set get it set; or that volumes that have been
2024 * offline without DONT SALVAGE having been set also
2025 * eventually get it set */
2026 V_dontSalvage(vp) = DONT_SALVAGE;
2027 #endif /* !AFS_DEMAND_ATTACH_FS */
2028 VAddToVolumeUpdateList_r(ec, vp);
2030 Log("VAttachVolume: Error adding volume to update list\n");
2037 Log("VOnline: volume %u (%s) attached and online\n", V_id(vp),
2042 if (programType == volumeUtility) {
2043 VUnlockPartition_r(partition);
2046 #ifdef AFS_DEMAND_ATTACH_FS
2047 /* attach failed; make sure we're in error state */
2048 if (vp && !VIsErrorState(V_attachState(vp))) {
2049 VChangeState_r(vp, VOL_STATE_ERROR);
2051 #endif /* AFS_DEMAND_ATTACH_FS */
2058 #ifdef AFS_DEMAND_ATTACH_FS
2059 /* VAttachVolumeByVp_r
2061 * finish attaching a volume that is
2062 * in a less than fully attached state
2064 /* caller MUST hold a ref count on vp */
2066 VAttachVolumeByVp_r(Error * ec, Volume * vp, int mode)
2068 char name[VMAXPATHLEN];
2069 int fd, n, reserve = 0;
2070 struct afs_stat status;
2071 struct VolumeDiskHeader diskHeader;
2072 struct VolumeHeader iheader;
2073 struct DiskPartition *partp;
2078 VolumeStats stats_save;
2081 /* volume utility should never call AttachByVp */
2082 assert(programType == fileServer);
2084 volumeId = vp->hashid;
2085 partp = vp->partition;
2086 VolumeExternalName_r(volumeId, name, sizeof(name));
2089 /* if another thread is performing a blocking op, wait */
2090 VWaitExclusiveState_r(vp);
2092 memcpy(&stats_save, &vp->stats, sizeof(VolumeStats));
2094 /* if it's already attached, see if we can return it */
2095 if (V_attachState(vp) == VOL_STATE_ATTACHED) {
2096 VGetVolumeByVp_r(ec, vp);
2100 if (vp->specialStatus == VBUSY)
2102 VDetachVolume_r(ec, vp);
2104 Log("VAttachVolume: Error detaching volume (%s)\n", name);
2110 /* pre-attach volume if it hasn't been done yet */
2112 (V_attachState(vp) == VOL_STATE_UNATTACHED) ||
2113 (V_attachState(vp) == VOL_STATE_ERROR)) {
2114 nvp = VPreAttachVolumeByVp_r(ec, partp, vp, volumeId);
2120 VCreateReservation_r(nvp);
2126 VChangeState_r(vp, VOL_STATE_ATTACHING);
2128 /* restore monotonically increasing stats */
2129 memcpy(&vp->stats, &stats_save, sizeof(VolumeStats));
2134 /* compute path to disk header,
2136 * and verify magic and version stamps */
2137 strcpy(path, VPartitionPath(partp));
2143 if ((fd = afs_open(path, O_RDONLY)) == -1 || afs_fstat(fd, &status) == -1) {
2144 Log("VAttachVolume: Failed to open %s (errno %d)\n", path, errno);
2151 n = read(fd, &diskHeader, sizeof(diskHeader));
2153 if (n != sizeof(diskHeader)
2154 || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
2155 Log("VAttachVolume: Error reading volume header %s\n", path);
2160 if (diskHeader.stamp.version != VOLUMEHEADERVERSION) {
2161 Log("VAttachVolume: Volume %s, version number is incorrect; volume needs salvaged\n", path);
2167 /* convert on-disk header format to in-memory header format */
2168 DiskToVolumeHeader(&iheader, &diskHeader);
2172 * NOTE: attach2 is entered without any locks, and returns
2173 * with vol_glock_mutex held */
2174 vp = attach2(ec, volumeId, path, &iheader, partp, vp, isbusy, mode);
2177 * the event that an error was encountered, or
2178 * the volume was not brought to an attached state
2179 * for any reason, skip to the end. We cannot
2180 * safely call VUpdateVolume unless we "own" it.
2184 (V_attachState(vp) != VOL_STATE_ATTACHED)) {
2188 V_needsCallback(vp) = 0;
2189 VUpdateVolume_r(ec, vp, 0);
2191 Log("VAttachVolume: Error updating volume %u\n", vp->hashid);
2195 if (VolumeWriteable(vp) && V_dontSalvage(vp) == 0) {
2196 #ifndef AFS_DEMAND_ATTACH_FS
2197 /* This is a hack: by temporarily setting the incore
2198 * dontSalvage flag ON, the volume will be put back on the
2199 * Update list (with dontSalvage OFF again). It will then
2200 * come back in N minutes with DONT_SALVAGE eventually
2201 * set. This is the way that volumes that have never had
2202 * it set get it set; or that volumes that have been
2203 * offline without DONT SALVAGE having been set also
2204 * eventually get it set */
2205 V_dontSalvage(vp) = DONT_SALVAGE;
2206 #endif /* !AFS_DEMAND_ATTACH_FS */
2207 VAddToVolumeUpdateList_r(ec, vp);
2209 Log("VAttachVolume: Error adding volume %u to update list\n", vp->hashid);
2216 Log("VOnline: volume %u (%s) attached and online\n", V_id(vp),
2220 VCancelReservation_r(nvp);
2223 if (*ec && (*ec != VOFFLINE) && (*ec != VSALVAGE)) {
2224 if (vp && !VIsErrorState(V_attachState(vp))) {
2225 VChangeState_r(vp, VOL_STATE_ERROR);
2232 #endif /* AFS_DEMAND_ATTACH_FS */
2235 * called without any locks held
2236 * returns with vol_glock_mutex held
2239 attach2(Error * ec, VolId volumeId, char *path, register struct VolumeHeader * header,
2240 struct DiskPartition * partp, register Volume * vp, int isbusy, int mode)
2242 vp->specialStatus = (byte) (isbusy ? VBUSY : 0);
2243 IH_INIT(vp->vnodeIndex[vLarge].handle, partp->device, header->parent,
2244 header->largeVnodeIndex);
2245 IH_INIT(vp->vnodeIndex[vSmall].handle, partp->device, header->parent,
2246 header->smallVnodeIndex);
2247 IH_INIT(vp->diskDataHandle, partp->device, header->parent,
2248 header->volumeInfo);
2249 IH_INIT(vp->linkHandle, partp->device, header->parent, header->linkTable);
2250 vp->shuttingDown = 0;
2251 vp->goingOffline = 0;
2253 #ifdef AFS_DEMAND_ATTACH_FS
2254 vp->stats.last_attach = FT_ApproxTime();
2255 vp->stats.attaches++;
2259 IncUInt64(&VStats.attaches);
2260 vp->cacheCheck = ++VolumeCacheCheck;
2261 /* just in case this ever rolls over */
2262 if (!vp->cacheCheck)
2263 vp->cacheCheck = ++VolumeCacheCheck;
2264 GetVolumeHeader(vp);
2267 #if defined(AFS_DEMAND_ATTACH_FS) && defined(FSSYNC_BUILD_CLIENT)
2268 /* demand attach changes the V_PEEK mechanism
2270 * we can now suck the current disk data structure over
2271 * the fssync interface without going to disk
2273 * (technically, we don't need to restrict this feature
2274 * to demand attach fileservers. However, I'm trying
2275 * to limit the number of common code changes)
2277 if (programType != fileServer && mode == V_PEEK) {
2279 res.payload.len = sizeof(VolumeDiskData);
2280 res.payload.buf = &vp->header->diskstuff;
2282 if (FSYNC_VolOp(volumeId,
2283 VPartitionPath(partp),
2284 FSYNC_VOL_QUERY_HDR,
2287 goto disk_header_loaded;
2290 #endif /* AFS_DEMAND_ATTACH_FS && FSSYNC_BUILD_CLIENT */
2291 (void)ReadHeader(ec, V_diskDataHandle(vp), (char *)&V_disk(vp),
2292 sizeof(V_disk(vp)), VOLUMEINFOMAGIC, VOLUMEINFOVERSION);
2294 #ifdef AFS_DEMAND_ATTACH_FS
2297 IncUInt64(&VStats.hdr_loads);
2298 IncUInt64(&vp->stats.hdr_loads);
2300 #endif /* AFS_DEMAND_ATTACH_FS */
2303 Log("VAttachVolume: Error reading diskDataHandle vol header %s; error=%u\n", path, *ec);
2308 #ifdef AFS_DEMAND_ATTACH_FS
2311 /* check for pending volume operations */
2312 if (vp->pending_vol_op) {
2313 /* see if the pending volume op requires exclusive access */
2314 if (!VVolOpLeaveOnline_r(vp, vp->pending_vol_op)) {
2315 /* mark the volume down */
2317 VChangeState_r(vp, VOL_STATE_UNATTACHED);
2318 if (V_offlineMessage(vp)[0] == '\0')
2319 strlcpy(V_offlineMessage(vp),
2320 "A volume utility is running.",
2321 sizeof(V_offlineMessage(vp)));
2322 V_offlineMessage(vp)[sizeof(V_offlineMessage(vp)) - 1] = '\0';
2324 /* check to see if we should set the specialStatus flag */
2325 if (VVolOpSetVBusy_r(vp, vp->pending_vol_op)) {
2326 vp->specialStatus = VBUSY;
2331 V_attachFlags(vp) |= VOL_HDR_LOADED;
2332 vp->stats.last_hdr_load = vp->stats.last_attach;
2334 #endif /* AFS_DEMAND_ATTACH_FS */
2337 struct IndexFileHeader iHead;
2339 #if OPENAFS_VOL_STATS
2341 * We just read in the diskstuff part of the header. If the detailed
2342 * volume stats area has not yet been initialized, we should bzero the
2343 * area and mark it as initialized.
2345 if (!(V_stat_initialized(vp))) {
2346 memset((char *)(V_stat_area(vp)), 0, VOL_STATS_BYTES);
2347 V_stat_initialized(vp) = 1;
2349 #endif /* OPENAFS_VOL_STATS */
2351 (void)ReadHeader(ec, vp->vnodeIndex[vSmall].handle,
2352 (char *)&iHead, sizeof(iHead),
2353 SMALLINDEXMAGIC, SMALLINDEXVERSION);
2356 Log("VAttachVolume: Error reading smallVnode vol header %s; error=%u\n", path, *ec);
2361 struct IndexFileHeader iHead;
2363 (void)ReadHeader(ec, vp->vnodeIndex[vLarge].handle,
2364 (char *)&iHead, sizeof(iHead),
2365 LARGEINDEXMAGIC, LARGEINDEXVERSION);
2368 Log("VAttachVolume: Error reading largeVnode vol header %s; error=%u\n", path, *ec);
2372 #ifdef AFS_NAMEI_ENV
2374 struct versionStamp stamp;
2376 (void)ReadHeader(ec, V_linkHandle(vp), (char *)&stamp,
2377 sizeof(stamp), LINKTABLEMAGIC, LINKTABLEVERSION);
2380 Log("VAttachVolume: Error reading namei vol header %s; error=%u\n", path, *ec);
2383 #endif /* AFS_NAMEI_ENV */
2385 #if defined(AFS_DEMAND_ATTACH_FS)
2386 if (*ec && ((*ec != VOFFLINE) || (V_attachState(vp) != VOL_STATE_UNATTACHED))) {
2388 if (programType == fileServer) {
2389 VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, VOL_SALVAGE_INVALIDATE_HEADER);
2392 Log("VAttachVolume: Error attaching volume %s; volume needs salvage; error=%u\n", path, *ec);
2398 /* volume operation in progress */
2402 #else /* AFS_DEMAND_ATTACH_FS */
2404 Log("VAttachVolume: Error attaching volume %s; volume needs salvage; error=%u\n", path, *ec);
2409 #endif /* AFS_DEMAND_ATTACH_FS */
2411 if (V_needsSalvaged(vp)) {
2412 if (vp->specialStatus)
2413 vp->specialStatus = 0;
2415 #if defined(AFS_DEMAND_ATTACH_FS)
2416 if (programType == fileServer) {
2417 VRequestSalvage_r(ec, vp, SALVSYNC_NEEDED, VOL_SALVAGE_INVALIDATE_HEADER);
2420 Log("VAttachVolume: volume salvage flag is ON for %s; volume needs salvage\n", path);
2424 #else /* AFS_DEMAND_ATTACH_FS */
2427 #endif /* AFS_DEMAND_ATTACH_FS */
2432 if (programType == fileServer) {
2433 #ifndef FAST_RESTART
2434 if (V_inUse(vp) && VolumeWriteable(vp)) {
2435 if (!V_needsSalvaged(vp)) {
2436 V_needsSalvaged(vp) = 1;
2437 VUpdateVolume_r(ec, vp, 0);
2439 #if defined(AFS_DEMAND_ATTACH_FS)
2440 VRequestSalvage_r(ec, vp, SALVSYNC_NEEDED, VOL_SALVAGE_INVALIDATE_HEADER);
2442 #else /* AFS_DEMAND_ATTACH_FS */
2443 Log("VAttachVolume: volume %s needs to be salvaged; not attached.\n", path);
2446 #endif /* AFS_DEMAND_ATTACH_FS */
2449 #endif /* FAST_RESTART */
2451 if (V_destroyMe(vp) == DESTROY_ME) {
2452 #if defined(AFS_DEMAND_ATTACH_FS)
2453 /* schedule a salvage so the volume goes away on disk */
2454 VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, VOL_SALVAGE_INVALIDATE_HEADER);
2455 VChangeState_r(vp, VOL_STATE_ERROR);
2457 #endif /* AFS_DEMAND_ATTACH_FS */
2459 Log("VAttachVolume: volume %s is junk; it should be destroyed at next salvage\n", path);
2465 vp->nextVnodeUnique = V_uniquifier(vp);
2466 vp->vnodeIndex[vSmall].bitmap = vp->vnodeIndex[vLarge].bitmap = NULL;
2467 #ifndef BITMAP_LATER
2468 if (programType == fileServer && VolumeWriteable(vp)) {
2470 for (i = 0; i < nVNODECLASSES; i++) {
2471 VGetBitmap_r(ec, vp, i);
2473 #ifdef AFS_DEMAND_ATTACH_FS
2474 VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, VOL_SALVAGE_INVALIDATE_HEADER);
2476 #else /* AFS_DEMAND_ATTACH_FS */
2478 #endif /* AFS_DEMAND_ATTACH_FS */
2479 Log("VAttachVolume: error getting bitmap for volume (%s)\n",
2485 #endif /* BITMAP_LATER */
2487 if (programType == fileServer) {
2488 if (vp->specialStatus)
2489 vp->specialStatus = 0;
2490 if (V_blessed(vp) && V_inService(vp) && !V_needsSalvaged(vp)) {
2492 V_offlineMessage(vp)[0] = '\0';
2496 AddVolumeToHashTable(vp, V_id(vp));
2497 #ifdef AFS_DEMAND_ATTACH_FS
2498 AddVolumeToVByPList_r(vp);
2500 if ((programType != fileServer) ||
2502 VChangeState_r(vp, VOL_STATE_ATTACHED);
2504 VChangeState_r(vp, VOL_STATE_UNATTACHED);
2510 /* Attach an existing volume.
2511 The volume also normally goes online at this time.
2512 An offline volume must be reattached to make it go online.
2516 VAttachVolume(Error * ec, VolumeId volumeId, int mode)
2520 retVal = VAttachVolume_r(ec, volumeId, mode);
2526 VAttachVolume_r(Error * ec, VolumeId volumeId, int mode)
2529 GetVolumePath(ec, volumeId, &part, &name);
2531 register Volume *vp;
2533 vp = VGetVolume_r(&error, volumeId);
2535 assert(V_inUse(vp) == 0);
2536 VDetachVolume_r(ec, vp);
2540 return VAttachVolumeByName_r(ec, part, name, mode);
2543 /* Increment a reference count to a volume, sans context swaps. Requires
2544 * possibly reading the volume header in from the disk, since there's
2545 * an invariant in the volume package that nUsers>0 ==> vp->header is valid.
2547 * N.B. This call can fail if we can't read in the header!! In this case
2548 * we still guarantee we won't context swap, but the ref count won't be
2549 * incremented (otherwise we'd violate the invariant).
2551 /* NOTE: with the demand attach fileserver extensions, the global lock
2552 * is dropped within VHold */
2553 #ifdef AFS_DEMAND_ATTACH_FS
2555 VHold_r(register Volume * vp)
2559 VCreateReservation_r(vp);
2560 VWaitExclusiveState_r(vp);
2562 LoadVolumeHeader(&error, vp);
2564 VCancelReservation_r(vp);
2568 VCancelReservation_r(vp);
2571 #else /* AFS_DEMAND_ATTACH_FS */
2573 VHold_r(register Volume * vp)
2577 LoadVolumeHeader(&error, vp);
2583 #endif /* AFS_DEMAND_ATTACH_FS */
2586 VHold(register Volume * vp)
2590 retVal = VHold_r(vp);
2596 /***************************************************/
2597 /* get and put volume routines */
2598 /***************************************************/
2601 * put back a heavyweight reference to a volume object.
2603 * @param[in] vp volume object pointer
2605 * @pre VOL_LOCK held
2607 * @post heavyweight volume reference put back.
2608 * depending on state, volume may have been taken offline,
2609 * detached, salvaged, freed, etc.
2611 * @internal volume package internal use only
2614 VPutVolume_r(register Volume * vp)
2616 assert(--vp->nUsers >= 0);
2617 if (vp->nUsers == 0) {
2619 ReleaseVolumeHeader(vp->header);
2620 #ifdef AFS_DEMAND_ATTACH_FS
2621 if (!VCheckDetach(vp)) {
2625 #else /* AFS_DEMAND_ATTACH_FS */
2627 #endif /* AFS_DEMAND_ATTACH_FS */
2632 VPutVolume(register Volume * vp)
2640 /* Get a pointer to an attached volume. The pointer is returned regardless
2641 of whether or not the volume is in service or on/off line. An error
2642 code, however, is returned with an indication of the volume's status */
2644 VGetVolume(Error * ec, Error * client_ec, VolId volumeId)
2648 retVal = GetVolume(ec, client_ec, volumeId, NULL, 0);
2654 VGetVolume_r(Error * ec, VolId volumeId)
2656 return GetVolume(ec, NULL, volumeId, NULL, 0);
2659 /* try to get a volume we've previously looked up */
2660 /* for demand attach fs, caller MUST NOT hold a ref count on vp */
2662 VGetVolumeByVp_r(Error * ec, Volume * vp)
2664 return GetVolume(ec, NULL, vp->hashid, vp, 0);
2667 /* private interface for getting a volume handle
2668 * volumeId must be provided.
2669 * hint is an optional parameter to speed up hash lookups
2670 * flags is not used at this time
2672 /* for demand attach fs, caller MUST NOT hold a ref count on hint */
2674 GetVolume(Error * ec, Error * client_ec, VolId volumeId, Volume * hint, int flags)
2677 /* pull this profiling/debugging code out of regular builds */
2679 #define VGET_CTR_INC(x) x++
2680 unsigned short V0 = 0, V1 = 0, V2 = 0, V3 = 0, V5 = 0, V6 =
2681 0, V7 = 0, V8 = 0, V9 = 0;
2682 unsigned short V10 = 0, V11 = 0, V12 = 0, V13 = 0, V14 = 0, V15 = 0;
2684 #define VGET_CTR_INC(x)
2686 #ifdef AFS_DEMAND_ATTACH_FS
2687 Volume *avp, * rvp = hint;
2690 #ifdef AFS_DEMAND_ATTACH_FS
2692 VCreateReservation_r(rvp);
2694 #endif /* AFS_DEMAND_ATTACH_FS */
2702 vp = VLookupVolume_r(ec, volumeId, vp);
2708 #ifdef AFS_DEMAND_ATTACH_FS
2709 if (rvp && (rvp != vp)) {
2710 /* break reservation on old vp */
2711 VCancelReservation_r(rvp);
2714 #endif /* AFS_DEMAND_ATTACH_FS */
2720 /* Until we have reached an initialization level of 2
2721 * we don't know whether this volume exists or not.
2722 * We can't sleep and retry later because before a volume
2723 * is attached, the caller tries to get it first. Just
2724 * return VOFFLINE and the caller can choose whether to
2725 * retry the command or not. */
2735 IncUInt64(&VStats.hdr_gets);
2737 #ifdef AFS_DEMAND_ATTACH_FS
2738 /* block if someone else is performing an exclusive op on this volume */
2741 VCreateReservation_r(rvp);
2743 VWaitExclusiveState_r(vp);
2745 /* short circuit with VNOVOL in the following circumstances:
2748 * VOL_STATE_SHUTTING_DOWN
2750 if ((V_attachState(vp) == VOL_STATE_ERROR) ||
2751 (V_attachState(vp) == VOL_STATE_SHUTTING_DOWN)) {
2758 * short circuit with VOFFLINE in the following circumstances:
2760 * VOL_STATE_UNATTACHED
2762 if (V_attachState(vp) == VOL_STATE_UNATTACHED) {
2768 /* allowable states:
2776 if (vp->salvage.requested) {
2777 VUpdateSalvagePriority_r(vp);
2780 if (V_attachState(vp) == VOL_STATE_PREATTACHED) {
2781 avp = VAttachVolumeByVp_r(ec, vp, 0);
2784 /* VAttachVolumeByVp_r can return a pointer
2785 * != the vp passed to it under certain
2786 * conditions; make sure we don't leak
2787 * reservations if that happens */
2789 VCancelReservation_r(rvp);
2791 VCreateReservation_r(rvp);
2801 if (!vp->pending_vol_op) {
2816 if ((V_attachState(vp) == VOL_STATE_SALVAGING) ||
2817 (*ec == VSALVAGING)) {
2819 /* see CheckVnode() in afsfileprocs.c for an explanation
2820 * of this error code logic */
2821 afs_uint32 now = FT_ApproxTime();
2822 if ((vp->stats.last_salvage + (10 * 60)) >= now) {
2825 *client_ec = VRESTARTING;
2834 LoadVolumeHeader(ec, vp);
2837 /* Only log the error if it was a totally unexpected error. Simply
2838 * a missing inode is likely to be caused by the volume being deleted */
2839 if (errno != ENXIO || LogLevel)
2840 Log("Volume %u: couldn't reread volume header\n",
2842 #ifdef AFS_DEMAND_ATTACH_FS
2843 if (programType == fileServer) {
2844 VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, VOL_SALVAGE_INVALIDATE_HEADER);
2849 #else /* AFS_DEMAND_ATTACH_FS */
2852 #endif /* AFS_DEMAND_ATTACH_FS */
2856 #ifdef AFS_DEMAND_ATTACH_FS
2858 * this test MUST happen after the volume header is loaded
2860 if (vp->pending_vol_op && !VVolOpLeaveOnline_r(vp, vp->pending_vol_op)) {
2862 /* see CheckVnode() in afsfileprocs.c for an explanation
2863 * of this error code logic */
2864 afs_uint32 now = FT_ApproxTime();
2865 if ((vp->stats.last_vol_op + (10 * 60)) >= now) {
2868 *client_ec = VRESTARTING;
2872 ReleaseVolumeHeader(vp->header);
2876 #endif /* AFS_DEMAND_ATTACH_FS */
2879 if (vp->shuttingDown) {
2886 if (programType == fileServer) {
2888 if (vp->goingOffline) {
2890 #ifdef AFS_DEMAND_ATTACH_FS
2891 /* wait for the volume to go offline */
2892 if (V_attachState(vp) == VOL_STATE_GOING_OFFLINE) {
2893 VWaitStateChange_r(vp);
2895 #elif defined(AFS_PTHREAD_ENV)
2896 VOL_CV_WAIT(&vol_put_volume_cond);
2897 #else /* AFS_PTHREAD_ENV */
2898 LWP_WaitProcess(VPutVolume);
2899 #endif /* AFS_PTHREAD_ENV */
2902 if (vp->specialStatus) {
2904 *ec = vp->specialStatus;
2905 } else if (V_inService(vp) == 0 || V_blessed(vp) == 0) {
2908 } else if (V_inUse(vp) == 0) {
2919 #ifdef AFS_DEMAND_ATTACH_FS
2920 /* if no error, bump nUsers */
2923 VLRU_UpdateAccess_r(vp);
2926 VCancelReservation_r(rvp);
2929 if (client_ec && !*client_ec) {
2932 #else /* AFS_DEMAND_ATTACH_FS */
2933 /* if no error, bump nUsers */
2940 #endif /* AFS_DEMAND_ATTACH_FS */
2947 /***************************************************/
2948 /* Volume offline/detach routines */
2949 /***************************************************/
2951 /* caller MUST hold a heavyweight ref on vp */
2952 #ifdef AFS_DEMAND_ATTACH_FS
2954 VTakeOffline_r(register Volume * vp)
2958 assert(vp->nUsers > 0);
2959 assert(programType == fileServer);
2961 VCreateReservation_r(vp);
2962 VWaitExclusiveState_r(vp);
2964 vp->goingOffline = 1;
2965 V_needsSalvaged(vp) = 1;
2967 VRequestSalvage_r(&error, vp, SALVSYNC_ERROR, 0);
2968 VCancelReservation_r(vp);
2970 #else /* AFS_DEMAND_ATTACH_FS */
2972 VTakeOffline_r(register Volume * vp)
2974 assert(vp->nUsers > 0);
2975 assert(programType == fileServer);
2977 vp->goingOffline = 1;
2978 V_needsSalvaged(vp) = 1;
2980 #endif /* AFS_DEMAND_ATTACH_FS */
2983 VTakeOffline(register Volume * vp)
2991 * force a volume offline.
2993 * @param[in] vp volume object pointer
2994 * @param[in] flags flags (see note below)
2996 * @note the flag VOL_FORCEOFF_NOUPDATE is a recursion control flag
2997 * used when VUpdateVolume_r needs to call VForceOffline_r
2998 * (which in turn would normally call VUpdateVolume_r)
3000 * @see VUpdateVolume_r
3002 * @pre VOL_LOCK must be held.
3003 * for DAFS, caller must hold ref.
3005 * @note for DAFS, it _is safe_ to call this function from an
3008 * @post needsSalvaged flag is set.
3009 * for DAFS, salvage is requested.
3010 * no further references to the volume through the volume
3011 * package will be honored.
3012 * all file descriptor and vnode caches are invalidated.
3014 * @warning this is a heavy-handed interface. it results in
3015 * a volume going offline regardless of the current
3016 * reference count state.
3018 * @internal volume package internal use only
3021 VForceOffline_r(Volume * vp, int flags)
3025 #ifdef AFS_DEMAND_ATTACH_FS
3026 VChangeState_r(vp, VOL_STATE_ERROR);
3031 strcpy(V_offlineMessage(vp),
3032 "Forced offline due to internal error: volume needs to be salvaged");
3033 Log("Volume %u forced offline: it needs salvaging!\n", V_id(vp));
3036 vp->goingOffline = 0;
3037 V_needsSalvaged(vp) = 1;
3038 if (!(flags & VOL_FORCEOFF_NOUPDATE)) {
3039 VUpdateVolume_r(&error, vp, VOL_UPDATE_NOFORCEOFF);
3042 #ifdef AFS_DEMAND_ATTACH_FS
3043 VRequestSalvage_r(&error, vp, SALVSYNC_ERROR, VOL_SALVAGE_INVALIDATE_HEADER);
3044 #endif /* AFS_DEMAND_ATTACH_FS */
3046 #ifdef AFS_PTHREAD_ENV
3047 assert(pthread_cond_broadcast(&vol_put_volume_cond) == 0);
3048 #else /* AFS_PTHREAD_ENV */
3049 LWP_NoYieldSignal(VPutVolume);
3050 #endif /* AFS_PTHREAD_ENV */
3052 VReleaseVolumeHandles_r(vp);
3056 * force a volume offline.
3058 * @param[in] vp volume object pointer
3060 * @see VForceOffline_r
3063 VForceOffline(Volume * vp)
3066 VForceOffline_r(vp, 0);
3070 /* The opposite of VAttachVolume. The volume header is written to disk, with
3071 the inUse bit turned off. A copy of the header is maintained in memory,
3072 however (which is why this is VOffline, not VDetach).
3075 VOffline_r(Volume * vp, char *message)
3078 VolumeId vid = V_id(vp);
3080 assert(programType != volumeUtility);
3085 if (V_offlineMessage(vp)[0] == '\0')
3086 strncpy(V_offlineMessage(vp), message, sizeof(V_offlineMessage(vp)));
3087 V_offlineMessage(vp)[sizeof(V_offlineMessage(vp)) - 1] = '\0';
3089 vp->goingOffline = 1;
3090 #ifdef AFS_DEMAND_ATTACH_FS
3091 VChangeState_r(vp, VOL_STATE_GOING_OFFLINE);
3092 VCreateReservation_r(vp);
3095 /* wait for the volume to go offline */
3096 if (V_attachState(vp) == VOL_STATE_GOING_OFFLINE) {
3097 VWaitStateChange_r(vp);
3099 VCancelReservation_r(vp);
3100 #else /* AFS_DEMAND_ATTACH_FS */
3102 vp = VGetVolume_r(&error, vid); /* Wait for it to go offline */
3103 if (vp) /* In case it was reattached... */
3105 #endif /* AFS_DEMAND_ATTACH_FS */
3109 VOffline(Volume * vp, char *message)
3112 VOffline_r(vp, message);
3116 /* This gets used for the most part by utility routines that don't want
3117 * to keep all the volume headers around. Generally, the file server won't
3118 * call this routine, because then the offline message in the volume header
3119 * (or other information) won't be available to clients. For NAMEI, also
3120 * close the file handles. However, the fileserver does call this during
3121 * an attach following a volume operation.
3124 VDetachVolume_r(Error * ec, Volume * vp)
3127 struct DiskPartition *tpartp;
3128 int notifyServer, useDone = FSYNC_VOL_ON;
3130 *ec = 0; /* always "succeeds" */
3131 if (programType == volumeUtility) {
3132 notifyServer = vp->needsPutBack;
3133 if (V_destroyMe(vp) == DESTROY_ME)
3134 useDone = FSYNC_VOL_DONE;
3135 #ifdef AFS_DEMAND_ATTACH_FS
3136 else if (!V_blessed(vp) || !V_inService(vp))
3137 useDone = FSYNC_VOL_LEAVE_OFF;
3140 tpartp = vp->partition;
3142 DeleteVolumeFromHashTable(vp);
3143 vp->shuttingDown = 1;
3144 #ifdef AFS_DEMAND_ATTACH_FS
3145 DeleteVolumeFromVByPList_r(vp);
3147 VChangeState_r(vp, VOL_STATE_SHUTTING_DOWN);
3148 #endif /* AFS_DEMAND_ATTACH_FS */
3150 /* Will be detached sometime in the future--this is OK since volume is offline */
3152 /* XXX the following code should really be moved to VCheckDetach() since the volume
3153 * is not technically detached until the refcounts reach zero
3155 #ifdef FSSYNC_BUILD_CLIENT
3156 if (programType == volumeUtility && notifyServer) {
3158 * Note: The server is not notified in the case of a bogus volume
3159 * explicitly to make it possible to create a volume, do a partial
3160 * restore, then abort the operation without ever putting the volume
3161 * online. This is essential in the case of a volume move operation
3162 * between two partitions on the same server. In that case, there
3163 * would be two instances of the same volume, one of them bogus,
3164 * which the file server would attempt to put on line
3166 FSYNC_VolOp(volume, tpartp->name, useDone, 0, NULL);
3167 /* XXX this code path is only hit by volume utilities, thus
3168 * V_BreakVolumeCallbacks will always be NULL. if we really
3169 * want to break callbacks in this path we need to use FSYNC_VolOp() */
3171 /* Dettaching it so break all callbacks on it */
3172 if (V_BreakVolumeCallbacks) {
3173 Log("volume %u detached; breaking all call backs\n", volume);
3174 (*V_BreakVolumeCallbacks) (volume);
3178 #endif /* FSSYNC_BUILD_CLIENT */
3182 VDetachVolume(Error * ec, Volume * vp)
3185 VDetachVolume_r(ec, vp);
3190 /***************************************************/
3191 /* Volume fd/inode handle closing routines */
3192 /***************************************************/
3194 /* For VDetachVolume, we close all cached file descriptors, but keep
3195 * the Inode handles in case we need to read from a busy volume.
3197 /* for demand attach, caller MUST hold ref count on vp */
3199 VCloseVolumeHandles_r(Volume * vp)
3201 #ifdef AFS_DEMAND_ATTACH_FS
3202 VolState state_save;
3204 state_save = VChangeState_r(vp, VOL_STATE_OFFLINING);
3209 * XXX need to investigate whether we can perform
3210 * DFlushVolume outside of vol_glock_mutex...
3212 * VCloseVnodeFiles_r drops the glock internally */
3213 DFlushVolume(V_id(vp));
3214 VCloseVnodeFiles_r(vp);
3216 #ifdef AFS_DEMAND_ATTACH_FS
3220 /* Too time consuming and unnecessary for the volserver */
3221 if (programType != volumeUtility) {
3222 IH_CONDSYNC(vp->vnodeIndex[vLarge].handle);
3223 IH_CONDSYNC(vp->vnodeIndex[vSmall].handle);
3224 IH_CONDSYNC(vp->diskDataHandle);
3226 IH_CONDSYNC(vp->linkHandle);
3227 #endif /* AFS_NT40_ENV */
3230 IH_REALLYCLOSE(vp->vnodeIndex[vLarge].handle);
3231 IH_REALLYCLOSE(vp->vnodeIndex[vSmall].handle);
3232 IH_REALLYCLOSE(vp->diskDataHandle);
3233 IH_REALLYCLOSE(vp->linkHandle);
3235 #ifdef AFS_DEMAND_ATTACH_FS
3237 VChangeState_r(vp, state_save);
3241 /* For both VForceOffline and VOffline, we close all relevant handles.
3242 * For VOffline, if we re-attach the volume, the files may possible be
3243 * different than before.
3245 /* for demand attach, caller MUST hold a ref count on vp */
3247 VReleaseVolumeHandles_r(Volume * vp)
3249 #ifdef AFS_DEMAND_ATTACH_FS
3250 VolState state_save;
3252 state_save = VChangeState_r(vp, VOL_STATE_DETACHING);
3255 /* XXX need to investigate whether we can perform
3256 * DFlushVolume outside of vol_glock_mutex... */
3257 DFlushVolume(V_id(vp));
3259 VReleaseVnodeFiles_r(vp); /* releases the glock internally */
3261 #ifdef AFS_DEMAND_ATTACH_FS
3265 /* Too time consuming and unnecessary for the volserver */
3266 if (programType != volumeUtility) {
3267 IH_CONDSYNC(vp->vnodeIndex[vLarge].handle);
3268 IH_CONDSYNC(vp->vnodeIndex[vSmall].handle);
3269 IH_CONDSYNC(vp->diskDataHandle);
3271 IH_CONDSYNC(vp->linkHandle);
3272 #endif /* AFS_NT40_ENV */
3275 IH_RELEASE(vp->vnodeIndex[vLarge].handle);
3276 IH_RELEASE(vp->vnodeIndex[vSmall].handle);
3277 IH_RELEASE(vp->diskDataHandle);
3278 IH_RELEASE(vp->linkHandle);
3280 #ifdef AFS_DEMAND_ATTACH_FS
3282 VChangeState_r(vp, state_save);
3287 /***************************************************/
3288 /* Volume write and fsync routines */
3289 /***************************************************/
3292 VUpdateVolume_r(Error * ec, Volume * vp, int flags)
3294 #ifdef AFS_DEMAND_ATTACH_FS
3295 VolState state_save;
3297 if (flags & VOL_UPDATE_WAIT) {
3298 VCreateReservation_r(vp);
3299 VWaitExclusiveState_r(vp);
3304 if (programType == fileServer)
3306 (V_inUse(vp) ? V_nextVnodeUnique(vp) +
3307 200 : V_nextVnodeUnique(vp));
3309 #ifdef AFS_DEMAND_ATTACH_FS
3310 state_save = VChangeState_r(vp, VOL_STATE_UPDATING);
3314 WriteVolumeHeader_r(ec, vp);
3316 #ifdef AFS_DEMAND_ATTACH_FS
3318 VChangeState_r(vp, state_save);
3319 if (flags & VOL_UPDATE_WAIT) {
3320 VCancelReservation_r(vp);
3325 Log("VUpdateVolume: error updating volume header, volume %u (%s)\n",
3326 V_id(vp), V_name(vp));
3327 /* try to update on-disk header,
3328 * while preventing infinite recursion */
3329 if (!(flags & VOL_UPDATE_NOFORCEOFF)) {
3330 VForceOffline_r(vp, VOL_FORCEOFF_NOUPDATE);
3336 VUpdateVolume(Error * ec, Volume * vp)
3339 VUpdateVolume_r(ec, vp, VOL_UPDATE_WAIT);
3344 VSyncVolume_r(Error * ec, Volume * vp, int flags)
3348 #ifdef AFS_DEMAND_ATTACH_FS
3349 VolState state_save;
3352 if (flags & VOL_SYNC_WAIT) {
3353 VUpdateVolume_r(ec, vp, VOL_UPDATE_WAIT);
3355 VUpdateVolume_r(ec, vp, 0);
3358 #ifdef AFS_DEMAND_ATTACH_FS
3359 state_save = VChangeState_r(vp, VOL_STATE_UPDATING);
3362 fdP = IH_OPEN(V_diskDataHandle(vp));
3363 assert(fdP != NULL);
3364 code = FDH_SYNC(fdP);
3367 #ifdef AFS_DEMAND_ATTACH_FS
3369 VChangeState_r(vp, state_save);
3375 VSyncVolume(Error * ec, Volume * vp)
3378 VSyncVolume_r(ec, vp, VOL_SYNC_WAIT);
3383 /***************************************************/
3384 /* Volume dealloaction routines */
3385 /***************************************************/
3387 #ifdef AFS_DEMAND_ATTACH_FS
3389 FreeVolume(Volume * vp)
3391 /* free the heap space, iff it's safe.
3392 * otherwise, pull it out of the hash table, so it
3393 * will get deallocated when all refs to it go away */
3394 if (!VCheckFree(vp)) {
3395 DeleteVolumeFromHashTable(vp);
3396 DeleteVolumeFromVByPList_r(vp);
3398 /* make sure we invalidate the header cache entry */
3399 FreeVolumeHeader(vp);
3402 #endif /* AFS_DEMAND_ATTACH_FS */
3405 ReallyFreeVolume(Volume * vp)
3410 #ifdef AFS_DEMAND_ATTACH_FS
3412 VChangeState_r(vp, VOL_STATE_FREED);
3413 if (vp->pending_vol_op)
3414 free(vp->pending_vol_op);
3415 #endif /* AFS_DEMAND_ATTACH_FS */
3416 for (i = 0; i < nVNODECLASSES; i++)
3417 if (vp->vnodeIndex[i].bitmap)
3418 free(vp->vnodeIndex[i].bitmap);
3419 FreeVolumeHeader(vp);
3420 #ifndef AFS_DEMAND_ATTACH_FS
3421 DeleteVolumeFromHashTable(vp);
3422 #endif /* AFS_DEMAND_ATTACH_FS */
3426 /* check to see if we should shutdown this volume
3427 * returns 1 if volume was freed, 0 otherwise */
3428 #ifdef AFS_DEMAND_ATTACH_FS
3430 VCheckDetach(register Volume * vp)
3434 if (vp->nUsers || vp->nWaiters)
3437 if (vp->shuttingDown) {
3439 VReleaseVolumeHandles_r(vp);
3441 ReallyFreeVolume(vp);
3442 if (programType == fileServer) {
3443 assert(pthread_cond_broadcast(&vol_put_volume_cond) == 0);
3448 #else /* AFS_DEMAND_ATTACH_FS */
3450 VCheckDetach(register Volume * vp)
3457 if (vp->shuttingDown) {
3459 VReleaseVolumeHandles_r(vp);
3460 ReallyFreeVolume(vp);
3461 if (programType == fileServer) {
3462 #if defined(AFS_PTHREAD_ENV)
3463 assert(pthread_cond_broadcast(&vol_put_volume_cond) == 0);
3464 #else /* AFS_PTHREAD_ENV */
3465 LWP_NoYieldSignal(VPutVolume);
3466 #endif /* AFS_PTHREAD_ENV */
3471 #endif /* AFS_DEMAND_ATTACH_FS */
3473 /* check to see if we should offline this volume
3474 * return 1 if volume went offline, 0 otherwise */
3475 #ifdef AFS_DEMAND_ATTACH_FS
3477 VCheckOffline(register Volume * vp)
3479 Volume * rvp = NULL;
3482 if (vp->goingOffline && !vp->nUsers) {
3484 assert(programType == fileServer);
3485 assert((V_attachState(vp) != VOL_STATE_ATTACHED) &&
3486 (V_attachState(vp) != VOL_STATE_FREED) &&
3487 (V_attachState(vp) != VOL_STATE_PREATTACHED) &&
3488 (V_attachState(vp) != VOL_STATE_UNATTACHED));
3492 * VOL_STATE_GOING_OFFLINE
3493 * VOL_STATE_SHUTTING_DOWN
3494 * VIsErrorState(V_attachState(vp))
3495 * VIsExclusiveState(V_attachState(vp))
3498 VCreateReservation_r(vp);
3499 VChangeState_r(vp, VOL_STATE_OFFLINING);
3502 /* must clear the goingOffline flag before we drop the glock */
3503 vp->goingOffline = 0;
3508 /* perform async operations */
3509 VUpdateVolume_r(&error, vp, 0);
3510 VCloseVolumeHandles_r(vp);
3513 Log("VOffline: Volume %u (%s) is now offline", V_id(vp),
3515 if (V_offlineMessage(vp)[0])
3516 Log(" (%s)", V_offlineMessage(vp));
3520 /* invalidate the volume header cache entry */
3521 FreeVolumeHeader(vp);
3523 /* if nothing changed state to error or salvaging,
3524 * drop state to unattached */
3525 if (!VIsErrorState(V_attachState(vp))) {
3526 VChangeState_r(vp, VOL_STATE_UNATTACHED);
3528 VCancelReservation_r(vp);
3529 /* no usage of vp is safe beyond this point */
3533 #else /* AFS_DEMAND_ATTACH_FS */
3535 VCheckOffline(register Volume * vp)
3537 Volume * rvp = NULL;
3540 if (vp->goingOffline && !vp->nUsers) {
3542 assert(programType == fileServer);
3545 vp->goingOffline = 0;
3547 VUpdateVolume_r(&error, vp, 0);
3548 VCloseVolumeHandles_r(vp);
3550 Log("VOffline: Volume %u (%s) is now offline", V_id(vp),
3552 if (V_offlineMessage(vp)[0])
3553 Log(" (%s)", V_offlineMessage(vp));
3556 FreeVolumeHeader(vp);
3557 #ifdef AFS_PTHREAD_ENV
3558 assert(pthread_cond_broadcast(&vol_put_volume_cond) == 0);
3559 #else /* AFS_PTHREAD_ENV */
3560 LWP_NoYieldSignal(VPutVolume);
3561 #endif /* AFS_PTHREAD_ENV */
3565 #endif /* AFS_DEMAND_ATTACH_FS */
3567 /***************************************************/
3568 /* demand attach fs ref counting routines */
3569 /***************************************************/
3571 #ifdef AFS_DEMAND_ATTACH_FS
3572 /* the following two functions handle reference counting for
3573 * asynchronous operations on volume structs.
3575 * their purpose is to prevent a VDetachVolume or VShutdown
3576 * from free()ing the Volume struct during an async i/o op */
3578 /* register with the async volume op ref counter */
3579 /* VCreateReservation_r moved into inline code header because it
3580 * is now needed in vnode.c -- tkeiser 11/20/2007
3584 * decrement volume-package internal refcount.
3586 * @param vp volume object pointer
3588 * @internal volume package internal use only
3591 * @arg VOL_LOCK is held
3592 * @arg lightweight refcount held
3594 * @post volume waiters refcount is decremented; volume may
3595 * have been deallocated/shutdown/offlined/salvaged/
3596 * whatever during the process
3598 * @warning once you have tossed your last reference (you can acquire
3599 * lightweight refs recursively) it is NOT SAFE to reference
3600 * a volume object pointer ever again
3602 * @see VCreateReservation_r
3604 * @note DEMAND_ATTACH_FS only
3607 VCancelReservation_r(Volume * vp)
3609 assert(--vp->nWaiters >= 0);
3610 if (vp->nWaiters == 0) {
3612 if (!VCheckDetach(vp)) {
3619 /* check to see if we should free this volume now
3620 * return 1 if volume was freed, 0 otherwise */
3622 VCheckFree(Volume * vp)
3625 if ((vp->nUsers == 0) &&
3626 (vp->nWaiters == 0) &&
3627 !(V_attachFlags(vp) & (VOL_IN_HASH |
3631 ReallyFreeVolume(vp);
3636 #endif /* AFS_DEMAND_ATTACH_FS */
3639 /***************************************************/
3640 /* online volume operations routines */
3641 /***************************************************/
3643 #ifdef AFS_DEMAND_ATTACH_FS
3645 * register a volume operation on a given volume.
3647 * @param[in] vp volume object
3648 * @param[in] vopinfo volume operation info object
3650 * @pre VOL_LOCK is held
3652 * @post volume operation info object attached to volume object.
3653 * volume operation statistics updated.
3655 * @note by "attached" we mean a copy of the passed in object is made
3657 * @internal volume package internal use only
3660 VRegisterVolOp_r(Volume * vp, FSSYNC_VolOp_info * vopinfo)
3662 FSSYNC_VolOp_info * info;
3664 /* attach a vol op info node to the volume struct */
3665 info = (FSSYNC_VolOp_info *) malloc(sizeof(FSSYNC_VolOp_info));
3666 assert(info != NULL);
3667 memcpy(info, vopinfo, sizeof(FSSYNC_VolOp_info));
3668 vp->pending_vol_op = info;
3671 vp->stats.last_vol_op = FT_ApproxTime();
3672 vp->stats.vol_ops++;
3673 IncUInt64(&VStats.vol_ops);
3679 * deregister the volume operation attached to this volume.
3681 * @param[in] vp volume object pointer
3683 * @pre VOL_LOCK is held
3685 * @post the volume operation info object is detached from the volume object
3687 * @internal volume package internal use only
3690 VDeregisterVolOp_r(Volume * vp)
3692 if (vp->pending_vol_op) {
3693 free(vp->pending_vol_op);
3694 vp->pending_vol_op = NULL;
3698 #endif /* AFS_DEMAND_ATTACH_FS */
3701 * determine whether it is safe to leave a volume online during
3702 * the volume operation described by the vopinfo object.
3704 * @param[in] vp volume object
3705 * @param[in] vopinfo volume operation info object
3707 * @return whether it is safe to leave volume online
3708 * @retval 0 it is NOT SAFE to leave the volume online
3709 * @retval 1 it is safe to leave the volume online during the operation
3712 * @arg VOL_LOCK is held
3713 * @arg disk header attached to vp (heavyweight ref on vp will guarantee
3714 * this condition is met)
3716 * @internal volume package internal use only
3719 VVolOpLeaveOnline_r(Volume * vp, FSSYNC_VolOp_info * vopinfo)
3721 return (vopinfo->com.command == FSYNC_VOL_NEEDVOLUME &&
3722 (vopinfo->com.reason == V_READONLY ||
3723 (!VolumeWriteable(vp) &&
3724 (vopinfo->com.reason == V_CLONE ||
3725 vopinfo->com.reason == V_DUMP))));
3729 * determine whether VBUSY should be set during this volume operation.
3731 * @param[in] vp volume object
3732 * @param[in] vopinfo volume operation info object
3734 * @return whether VBUSY should be set
3735 * @retval 0 VBUSY does NOT need to be set
3736 * @retval 1 VBUSY SHOULD be set
3738 * @pre VOL_LOCK is held
3740 * @internal volume package internal use only
3743 VVolOpSetVBusy_r(Volume * vp, FSSYNC_VolOp_info * vopinfo)
3745 return (vopinfo->com.command == FSYNC_VOL_NEEDVOLUME &&
3746 (vopinfo->com.reason == V_CLONE ||
3747 vopinfo->com.reason == V_DUMP));
3751 /***************************************************/
3752 /* online salvager routines */
3753 /***************************************************/
3754 #if defined(AFS_DEMAND_ATTACH_FS)
3755 #define SALVAGE_PRIO_UPDATE_INTERVAL 3 /**< number of seconds between prio updates */
3756 #define SALVAGE_COUNT_MAX 16 /**< number of online salvages we
3757 * allow before moving the volume
3758 * into a permanent error state
3760 * once this threshold is reached,
3761 * the operator will have to manually
3762 * issue a 'bos salvage' to bring
3763 * the volume back online
3767 * check whether a salvage needs to be performed on this volume.
3769 * @param[in] vp pointer to volume object
3771 * @return status code
3772 * @retval 0 no salvage scheduled
3773 * @retval 1 a salvage has been scheduled with the salvageserver
3775 * @pre VOL_LOCK is held
3777 * @post if salvage request flag is set and nUsers and nWaiters are zero,
3778 * then a salvage will be requested
3780 * @note this is one of the event handlers called by VCancelReservation_r
3782 * @see VCancelReservation_r
3784 * @internal volume package internal use only.
3787 VCheckSalvage(register Volume * vp)
3790 #ifdef SALVSYNC_BUILD_CLIENT
3791 if (vp->nUsers || vp->nWaiters)
3793 if (vp->salvage.requested) {
3794 VScheduleSalvage_r(vp);
3797 #endif /* SALVSYNC_BUILD_CLIENT */
3802 * request volume salvage.
3804 * @param[out] ec computed client error code
3805 * @param[in] vp volume object pointer
3806 * @param[in] reason reason code (passed to salvageserver via SALVSYNC)
3807 * @param[in] flags see flags note below
3810 * VOL_SALVAGE_INVALIDATE_HEADER causes volume header cache entry
3811 * to be invalidated.
3813 * @pre VOL_LOCK is held.
3815 * @post volume state is changed.
3816 * for fileserver, salvage will be requested once refcount reaches zero.
3818 * @return operation status code
3819 * @retval 0 volume salvage will occur
3820 * @retval 1 volume salvage could not be scheduled
3822 * @note DAFS fileserver only
3824 * @note this call does not synchronously schedule a volume salvage. rather,
3825 * it sets volume state so that when volume refcounts reach zero, a
3826 * volume salvage will occur. by "refcounts", we mean both nUsers and
3827 * nWaiters must be zero.
3829 * @internal volume package internal use only.
3832 VRequestSalvage_r(Error * ec, Volume * vp, int reason, int flags)
3836 * for DAFS volume utilities, transition to error state
3837 * (at some point in the future, we should consider
3838 * making volser talk to salsrv)
3840 if (programType != fileServer) {
3841 VChangeState_r(vp, VOL_STATE_ERROR);
3846 if (!vp->salvage.requested) {
3847 vp->salvage.requested = 1;
3848 vp->salvage.reason = reason;
3849 vp->stats.last_salvage = FT_ApproxTime();
3850 if (flags & VOL_SALVAGE_INVALIDATE_HEADER) {
3851 /* XXX this should likely be changed to FreeVolumeHeader() */
3852 ReleaseVolumeHeader(vp->header);
3854 if (vp->stats.salvages < SALVAGE_COUNT_MAX) {
3855 VChangeState_r(vp, VOL_STATE_SALVAGING);
3858 Log("VRequestSalvage: volume %u online salvaged too many times; forced offline.\n", vp->hashid);
3859 VChangeState_r(vp, VOL_STATE_ERROR);
3868 * update salvageserver scheduling priority for a volume.
3870 * @param[in] vp pointer to volume object
3872 * @return operation status
3874 * @retval 1 request denied, or SALVSYNC communications failure
3876 * @pre VOL_LOCK is held.
3878 * @post in-core salvage priority counter is incremented. if at least
3879 * SALVAGE_PRIO_UPDATE_INTERVAL seconds have elapsed since the
3880 * last SALVSYNC_RAISEPRIO request, we contact the salvageserver
3881 * to update its priority queue. if no salvage is scheduled,
3882 * this function is a no-op.
3884 * @note DAFS fileserver only
3886 * @note this should be called whenever a VGetVolume fails due to a
3887 * pending salvage request
3889 * @todo should set exclusive state and drop glock around salvsync call
3891 * @internal volume package internal use only.
3894 VUpdateSalvagePriority_r(Volume * vp)
3899 #ifdef SALVSYNC_BUILD_CLIENT
3901 now = FT_ApproxTime();
3903 /* update the salvageserver priority queue occasionally so that
3904 * frequently requested volumes get moved to the head of the queue
3906 if ((vp->salvage.scheduled) &&
3907 (vp->stats.last_salvage_req < (now-SALVAGE_PRIO_UPDATE_INTERVAL))) {
3908 code = SALVSYNC_SalvageVolume(vp->hashid,
3909 VPartitionPath(vp->partition),
3914 vp->stats.last_salvage_req = now;
3915 if (code != SYNC_OK) {
3919 #endif /* SALVSYNC_BUILD_CLIENT */
3925 * schedule a salvage with the salvage server.
3927 * @param[in] vp pointer to volume object
3929 * @return operation status
3930 * @retval 0 salvage scheduled successfully
3931 * @retval 1 salvage not scheduled, or SALVSYNC com error
3934 * @arg VOL_LOCK is held.
3935 * @arg nUsers and nWaiters should be zero.
3937 * @post salvageserver is sent a salvage request
3939 * @note DAFS fileserver only
3941 * @internal volume package internal use only.
3944 VScheduleSalvage_r(Volume * vp)
3947 #ifdef SALVSYNC_BUILD_CLIENT
3948 VolState state_save;
3951 if (vp->nWaiters || vp->nUsers) {
3955 /* prevent endless salvage,attach,salvage,attach,... loops */
3956 if (vp->stats.salvages >= SALVAGE_COUNT_MAX)
3959 if (!vp->salvage.scheduled) {
3960 /* if we haven't previously scheduled a salvage, do so now
3962 * set the volume to an exclusive state and drop the lock
3963 * around the SALVSYNC call
3965 * note that we do NOT acquire a reservation here -- doing so
3966 * could result in unbounded recursion
3968 strlcpy(partName, VPartitionPath(vp->partition), sizeof(partName));
3969 state_save = VChangeState_r(vp, VOL_STATE_SALVSYNC_REQ);
3970 V_attachFlags(vp) |= VOL_IS_BUSY;
3973 /* can't use V_id() since there's no guarantee
3974 * we have the disk data header at this point */
3975 code = SALVSYNC_SalvageVolume(vp->hashid,
3982 VChangeState_r(vp, state_save);
3983 V_attachFlags(vp) &= ~(VOL_IS_BUSY);
3985 if (code == SYNC_OK) {
3986 vp->salvage.scheduled = 1;
3987 vp->stats.salvages++;
3988 vp->stats.last_salvage_req = FT_ApproxTime();
3989 IncUInt64(&VStats.salvages);
3993 case SYNC_BAD_COMMAND:
3994 case SYNC_COM_ERROR:
3997 Log("VScheduleSalvage_r: SALVSYNC request denied\n");
4000 Log("VScheduleSalvage_r: SALVSYNC unknown protocol error\n");
4005 #endif /* SALVSYNC_BUILD_CLIENT */
4010 * ask salvageserver to cancel a scheduled salvage operation.
4012 * @param[in] vp pointer to volume object
4013 * @param[in] reason SALVSYNC protocol reason code
4015 * @return operation status
4017 * @retval 1 request failed
4019 * @pre VOL_LOCK is held.
4021 * @post salvageserver is sent a request to cancel the volume salvage
4023 * @todo should set exclusive state and drop glock around salvsync call
4025 * @internal volume package internal use only.
4028 VCancelSalvage_r(Volume * vp, int reason)
4032 #ifdef SALVSYNC_BUILD_CLIENT
4033 if (vp->salvage.scheduled) {
4034 code = SALVSYNC_SalvageVolume(vp->hashid,
4035 VPartitionPath(vp->partition),
4040 if (code == SYNC_OK) {
4041 vp->salvage.scheduled = 0;
4046 #endif /* SALVSYNC_BUILD_CLIENT */
4051 #ifdef SALVSYNC_BUILD_CLIENT
4053 * connect to the salvageserver SYNC service.
4055 * @return operation status
4059 * @post connection to salvageserver SYNC service established
4061 * @see VConnectSALV_r
4062 * @see VDisconnectSALV
4063 * @see VReconnectSALV
4070 retVal = VConnectSALV_r();
4076 * connect to the salvageserver SYNC service.
4078 * @return operation status
4082 * @pre VOL_LOCK is held.
4084 * @post connection to salvageserver SYNC service established
4087 * @see VDisconnectSALV_r
4088 * @see VReconnectSALV_r
4089 * @see SALVSYNC_clientInit
4091 * @internal volume package internal use only.
4094 VConnectSALV_r(void)
4096 return SALVSYNC_clientInit();
4100 * disconnect from the salvageserver SYNC service.
4102 * @return operation status
4105 * @pre client should have a live connection to the salvageserver
4107 * @post connection to salvageserver SYNC service destroyed
4109 * @see VDisconnectSALV_r
4111 * @see VReconnectSALV
4114 VDisconnectSALV(void)
4118 VDisconnectSALV_r();
4124 * disconnect from the salvageserver SYNC service.
4126 * @return operation status
4130 * @arg VOL_LOCK is held.
4131 * @arg client should have a live connection to the salvageserver.
4133 * @post connection to salvageserver SYNC service destroyed
4135 * @see VDisconnectSALV
4136 * @see VConnectSALV_r
4137 * @see VReconnectSALV_r
4138 * @see SALVSYNC_clientFinis
4140 * @internal volume package internal use only.
4143 VDisconnectSALV_r(void)
4145 return SALVSYNC_clientFinis();
4149 * disconnect and then re-connect to the salvageserver SYNC service.
4151 * @return operation status
4155 * @pre client should have a live connection to the salvageserver
4157 * @post old connection is dropped, and a new one is established
4160 * @see VDisconnectSALV
4161 * @see VReconnectSALV_r
4164 VReconnectSALV(void)
4168 retVal = VReconnectSALV_r();
4174 * disconnect and then re-connect to the salvageserver SYNC service.
4176 * @return operation status
4181 * @arg VOL_LOCK is held.
4182 * @arg client should have a live connection to the salvageserver.
4184 * @post old connection is dropped, and a new one is established
4186 * @see VConnectSALV_r
4187 * @see VDisconnectSALV
4188 * @see VReconnectSALV
4189 * @see SALVSYNC_clientReconnect
4191 * @internal volume package internal use only.
4194 VReconnectSALV_r(void)
4196 return SALVSYNC_clientReconnect();
4198 #endif /* SALVSYNC_BUILD_CLIENT */
4199 #endif /* AFS_DEMAND_ATTACH_FS */
4202 /***************************************************/
4203 /* FSSYNC routines */
4204 /***************************************************/
4206 /* This must be called by any volume utility which needs to run while the
4207 file server is also running. This is separated from VInitVolumePackage so
4208 that a utility can fork--and each of the children can independently
4209 initialize communication with the file server */
4210 #ifdef FSSYNC_BUILD_CLIENT
4212 * connect to the fileserver SYNC service.
4214 * @return operation status
4219 * @arg VInit must equal 2.
4220 * @arg Program Type must not be fileserver or salvager.
4222 * @post connection to fileserver SYNC service established
4225 * @see VDisconnectFS
4226 * @see VChildProcReconnectFS
4233 retVal = VConnectFS_r();
4239 * connect to the fileserver SYNC service.
4241 * @return operation status
4246 * @arg VInit must equal 2.
4247 * @arg Program Type must not be fileserver or salvager.
4248 * @arg VOL_LOCK is held.
4250 * @post connection to fileserver SYNC service established
4253 * @see VDisconnectFS_r
4254 * @see VChildProcReconnectFS_r
4256 * @internal volume package internal use only.
4262 assert((VInit == 2) &&
4263 (programType != fileServer) &&
4264 (programType != salvager));
4265 rc = FSYNC_clientInit();
4272 * disconnect from the fileserver SYNC service.
4275 * @arg client should have a live connection to the fileserver.
4276 * @arg VOL_LOCK is held.
4277 * @arg Program Type must not be fileserver or salvager.
4279 * @post connection to fileserver SYNC service destroyed
4281 * @see VDisconnectFS
4283 * @see VChildProcReconnectFS_r
4285 * @internal volume package internal use only.
4288 VDisconnectFS_r(void)
4290 assert((programType != fileServer) &&
4291 (programType != salvager));
4292 FSYNC_clientFinis();
4297 * disconnect from the fileserver SYNC service.
4300 * @arg client should have a live connection to the fileserver.
4301 * @arg Program Type must not be fileserver or salvager.
4303 * @post connection to fileserver SYNC service destroyed
4305 * @see VDisconnectFS_r
4307 * @see VChildProcReconnectFS
4318 * connect to the fileserver SYNC service from a child process following a fork.
4320 * @return operation status
4325 * @arg VOL_LOCK is held.
4326 * @arg current FSYNC handle is shared with a parent process
4328 * @post current FSYNC handle is discarded and a new connection to the
4329 * fileserver SYNC service is established
4331 * @see VChildProcReconnectFS
4333 * @see VDisconnectFS_r
4335 * @internal volume package internal use only.
4338 VChildProcReconnectFS_r(void)
4340 return FSYNC_clientChildProcReconnect();
4344 * connect to the fileserver SYNC service from a child process following a fork.
4346 * @return operation status
4350 * @pre current FSYNC handle is shared with a parent process
4352 * @post current FSYNC handle is discarded and a new connection to the
4353 * fileserver SYNC service is established
4355 * @see VChildProcReconnectFS_r
4357 * @see VDisconnectFS
4360 VChildProcReconnectFS(void)
4364 ret = VChildProcReconnectFS_r();
4368 #endif /* FSSYNC_BUILD_CLIENT */
4371 /***************************************************/
4372 /* volume bitmap routines */
4373 /***************************************************/
4376 * For demand attach fs, flags parameter controls
4377 * locking behavior. If (flags & VOL_ALLOC_BITMAP_WAIT)
4378 * is set, then this function will create a reservation
4379 * and block on any other exclusive operations. Otherwise,
4380 * this function assumes the caller already has exclusive
4381 * access to vp, and we just change the volume state.
4384 VAllocBitmapEntry_r(Error * ec, Volume * vp,
4385 struct vnodeIndex *index, int flags)
4388 register byte *bp, *ep;
4389 #ifdef AFS_DEMAND_ATTACH_FS
4390 VolState state_save;
4391 #endif /* AFS_DEMAND_ATTACH_FS */
4395 /* This test is probably redundant */
4396 if (!VolumeWriteable(vp)) {
4397 *ec = (bit32) VREADONLY;
4401 #ifdef AFS_DEMAND_ATTACH_FS
4402 if (flags & VOL_ALLOC_BITMAP_WAIT) {
4403 VCreateReservation_r(vp);
4404 VWaitExclusiveState_r(vp);
4406 state_save = VChangeState_r(vp, VOL_STATE_GET_BITMAP);
4407 #endif /* AFS_DEMAND_ATTACH_FS */
4410 if ((programType == fileServer) && !index->bitmap) {
4412 #ifndef AFS_DEMAND_ATTACH_FS
4413 /* demand attach fs uses the volume state to avoid races.
4414 * specialStatus field is not used at all */
4416 if (vp->specialStatus == VBUSY) {
4417 if (vp->goingOffline) { /* vos dump waiting for the volume to
4418 * go offline. We probably come here
4419 * from AddNewReadableResidency */
4422 while (vp->specialStatus == VBUSY) {
4423 #ifdef AFS_PTHREAD_ENV
4427 #else /* !AFS_PTHREAD_ENV */
4429 #endif /* !AFS_PTHREAD_ENV */
4433 #endif /* !AFS_DEMAND_ATTACH_FS */
4435 if (!index->bitmap) {
4436 #ifndef AFS_DEMAND_ATTACH_FS
4437 vp->specialStatus = VBUSY; /* Stop anyone else from using it. */
4438 #endif /* AFS_DEMAND_ATTACH_FS */
4439 for (i = 0; i < nVNODECLASSES; i++) {
4440 VGetBitmap_r(ec, vp, i);
4442 #ifdef AFS_DEMAND_ATTACH_FS
4443 VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, VOL_SALVAGE_INVALIDATE_HEADER);
4444 #else /* AFS_DEMAND_ATTACH_FS */
4445 DeleteVolumeFromHashTable(vp);
4446 vp->shuttingDown = 1; /* Let who has it free it. */
4447 vp->specialStatus = 0;
4448 #endif /* AFS_DEMAND_ATTACH_FS */
4453 #ifndef AFS_DEMAND_ATTACH_FS
4455 vp->specialStatus = 0; /* Allow others to have access. */
4456 #endif /* AFS_DEMAND_ATTACH_FS */
4459 #endif /* BITMAP_LATER */
4461 #ifdef AFS_DEMAND_ATTACH_FS
4463 #endif /* AFS_DEMAND_ATTACH_FS */
4464 bp = index->bitmap + index->bitmapOffset;
4465 ep = index->bitmap + index->bitmapSize;
4467 if ((*(bit32 *) bp) != (bit32) 0xffffffff) {
4469 index->bitmapOffset = (afs_uint32) (bp - index->bitmap);
4472 o = ffs(~*bp) - 1; /* ffs is documented in BSTRING(3) */
4474 ret = (VnodeId) ((bp - index->bitmap) * 8 + o);
4475 #ifdef AFS_DEMAND_ATTACH_FS
4477 #endif /* AFS_DEMAND_ATTACH_FS */
4480 bp += sizeof(bit32) /* i.e. 4 */ ;
4482 /* No bit map entry--must grow bitmap */
4484 realloc(index->bitmap, index->bitmapSize + VOLUME_BITMAP_GROWSIZE);
4487 bp += index->bitmapSize;
4488 memset(bp, 0, VOLUME_BITMAP_GROWSIZE);
4489 index->bitmapOffset = index->bitmapSize;
4490 index->bitmapSize += VOLUME_BITMAP_GROWSIZE;
4492 ret = index->bitmapOffset * 8;
4493 #ifdef AFS_DEMAND_ATTACH_FS
4495 #endif /* AFS_DEMAND_ATTACH_FS */
4498 #ifdef AFS_DEMAND_ATTACH_FS
4499 VChangeState_r(vp, state_save);
4500 if (flags & VOL_ALLOC_BITMAP_WAIT) {
4501 VCancelReservation_r(vp);
4503 #endif /* AFS_DEMAND_ATTACH_FS */
4508 VAllocBitmapEntry(Error * ec, Volume * vp, register struct vnodeIndex * index)
4512 retVal = VAllocBitmapEntry_r(ec, vp, index, VOL_ALLOC_BITMAP_WAIT);
4518 VFreeBitMapEntry_r(Error * ec, register struct vnodeIndex *index,
4521 unsigned int offset;
4527 #endif /* BITMAP_LATER */
4528 offset = bitNumber >> 3;
4529 if (offset >= index->bitmapSize) {
4533 if (offset < index->bitmapOffset)
4534 index->bitmapOffset = offset & ~3; /* Truncate to nearest bit32 */
4535 *(index->bitmap + offset) &= ~(1 << (bitNumber & 0x7));
4539 VFreeBitMapEntry(Error * ec, register struct vnodeIndex *index,
4543 VFreeBitMapEntry_r(ec, index, bitNumber);
4547 /* this function will drop the glock internally.
4548 * for old pthread fileservers, this is safe thanks to vbusy.
4550 * for demand attach fs, caller must have already called
4551 * VCreateReservation_r and VWaitExclusiveState_r */
4553 VGetBitmap_r(Error * ec, Volume * vp, VnodeClass class)
4555 StreamHandle_t *file;
4558 struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
4559 struct vnodeIndex *vip = &vp->vnodeIndex[class];
4560 struct VnodeDiskObject *vnode;
4561 unsigned int unique = 0;
4565 #endif /* BITMAP_LATER */
4566 #ifdef AFS_DEMAND_ATTACH_FS
4567 VolState state_save;
4568 #endif /* AFS_DEMAND_ATTACH_FS */
4572 #ifdef AFS_DEMAND_ATTACH_FS
4573 state_save = VChangeState_r(vp, VOL_STATE_GET_BITMAP);
4574 #endif /* AFS_DEMAND_ATTACH_FS */
4577 fdP = IH_OPEN(vip->handle);
4578 assert(fdP != NULL);
4579 file = FDH_FDOPEN(fdP, "r");
4580 assert(file != NULL);
4581 vnode = (VnodeDiskObject *) malloc(vcp->diskSize);
4582 assert(vnode != NULL);
4583 size = OS_SIZE(fdP->fd_fd);
4585 nVnodes = (size <= vcp->diskSize ? 0 : size - vcp->diskSize)
4587 vip->bitmapSize = ((nVnodes / 8) + 10) / 4 * 4; /* The 10 is a little extra so
4588 * a few files can be created in this volume,
4589 * the whole thing is rounded up to nearest 4
4590 * bytes, because the bit map allocator likes
4593 BitMap = (byte *) calloc(1, vip->bitmapSize);
4594 assert(BitMap != NULL);
4595 #else /* BITMAP_LATER */
4596 vip->bitmap = (byte *) calloc(1, vip->bitmapSize);
4597 assert(vip->bitmap != NULL);
4598 vip->bitmapOffset = 0;
4599 #endif /* BITMAP_LATER */
4600 if (STREAM_SEEK(file, vcp->diskSize, 0) != -1) {
4602 for (bitNumber = 0; bitNumber < nVnodes + 100; bitNumber++) {
4603 if (STREAM_READ(vnode, vcp->diskSize, 1, file) != 1)
4605 if (vnode->type != vNull) {
4606 if (vnode->vnodeMagic != vcp->magic) {
4607 Log("GetBitmap: addled vnode index in volume %s; volume needs salvage\n", V_name(vp));
4612 *(BitMap + (bitNumber >> 3)) |= (1 << (bitNumber & 0x7));
4613 #else /* BITMAP_LATER */
4614 *(vip->bitmap + (bitNumber >> 3)) |= (1 << (bitNumber & 0x7));
4615 #endif /* BITMAP_LATER */
4616 if (unique <= vnode->uniquifier)
4617 unique = vnode->uniquifier + 1;
4619 #ifndef AFS_PTHREAD_ENV
4620 if ((bitNumber & 0x00ff) == 0x0ff) { /* every 256 iterations */
4623 #endif /* !AFS_PTHREAD_ENV */
4626 if (vp->nextVnodeUnique < unique) {
4627 Log("GetBitmap: bad volume uniquifier for volume %s; volume needs salvage\n", V_name(vp));
4630 /* Paranoia, partly justified--I think fclose after fdopen
4631 * doesn't seem to close fd. In any event, the documentation
4632 * doesn't specify, so it's safer to close it twice.
4640 /* There may have been a racing condition with some other thread, both
4641 * creating the bitmaps for this volume. If the other thread was faster
4642 * the pointer to bitmap should already be filled and we can free ours.
4644 if (vip->bitmap == NULL) {
4645 vip->bitmap = BitMap;
4646 vip->bitmapOffset = 0;
4648 free((byte *) BitMap);
4649 #endif /* BITMAP_LATER */
4650 #ifdef AFS_DEMAND_ATTACH_FS
4651 VChangeState_r(vp, state_save);
4652 #endif /* AFS_DEMAND_ATTACH_FS */
4656 /***************************************************/
4657 /* Volume Path and Volume Number utility routines */
4658 /***************************************************/
4661 * find the first occurrence of a volume header file and return the path.
4663 * @param[out] ec outbound error code
4664 * @param[in] volumeId volume id to find
4665 * @param[out] partitionp pointer to disk partition path string
4666 * @param[out] namep pointer to volume header file name string
4668 * @post path to first occurrence of volume header is returned in partitionp
4669 * and namep, or ec is set accordingly.
4671 * @warning this function is NOT re-entrant -- partitionp and namep point to
4672 * static data segments
4674 * @note if a volume utility inadvertently leaves behind a stale volume header
4675 * on a vice partition, it is possible for callers to get the wrong one,
4676 * depending on the order of the disk partition linked list.
4678 * @internal volume package internal use only.
4681 GetVolumePath(Error * ec, VolId volumeId, char **partitionp, char **namep)
4683 static char partition[VMAXPATHLEN], name[VMAXPATHLEN];
4684 char path[VMAXPATHLEN];
4686 struct DiskPartition *dp;
4690 (void)afs_snprintf(&name[1], (sizeof name) - 1, VFORMAT, volumeId);
4691 for (dp = DiskPartitionList; dp; dp = dp->next) {
4692 struct afs_stat status;
4693 strcpy(path, VPartitionPath(dp));
4695 if (afs_stat(path, &status) == 0) {
4696 strcpy(partition, dp->name);
4703 *partitionp = *namep = NULL;
4705 *partitionp = partition;
4711 * extract a volume number from a volume header filename string.
4713 * @param[in] name volume header filename string
4715 * @return volume number
4717 * @note the string must be of the form VFORMAT. the only permissible
4718 * deviation is a leading '/' character.
4723 VolumeNumber(char *name)
4727 return atoi(name + 1);
4731 * compute the volume header filename.
4733 * @param[in] volumeId
4735 * @return volume header filename
4737 * @post volume header filename string is constructed
4739 * @warning this function is NOT re-entrant -- the returned string is
4740 * stored in a static char array. see VolumeExternalName_r
4741 * for a re-entrant equivalent.
4743 * @see VolumeExternalName_r
4745 * @deprecated due to the above re-entrancy warning, this interface should
4746 * be considered deprecated. Please use VolumeExternalName_r
4750 VolumeExternalName(VolumeId volumeId)
4752 static char name[VMAXPATHLEN];
4753 (void)afs_snprintf(name, sizeof name, VFORMAT, volumeId);
4758 * compute the volume header filename.
4760 * @param[in] volumeId
4761 * @param[inout] name array in which to store filename
4762 * @param[in] len length of name array
4764 * @return result code from afs_snprintf
4766 * @see VolumeExternalName
4769 * @note re-entrant equivalent of VolumeExternalName
4771 * @internal volume package internal use only.
4774 VolumeExternalName_r(VolumeId volumeId, char * name, size_t len)
4776 return afs_snprintf(name, len, VFORMAT, volumeId);
4780 /***************************************************/
4781 /* Volume Usage Statistics routines */
4782 /***************************************************/
4784 #if OPENAFS_VOL_STATS
4785 #define OneDay (86400) /* 24 hours' worth of seconds */
4787 #define OneDay (24*60*60) /* 24 hours */
4788 #endif /* OPENAFS_VOL_STATS */
4790 #define Midnight(date) ((date-TimeZoneCorrection)/OneDay*OneDay+TimeZoneCorrection)
4792 /*------------------------------------------------------------------------
4793 * [export] VAdjustVolumeStatistics
4796 * If we've passed midnight, we need to update all the day use
4797 * statistics as well as zeroing the detailed volume statistics
4798 * (if we are implementing them).
4801 * vp : Pointer to the volume structure describing the lucky
4802 * volume being considered for update.
4808 * Nothing interesting.
4812 *------------------------------------------------------------------------*/
4815 VAdjustVolumeStatistics_r(register Volume * vp)
4817 unsigned int now = FT_ApproxTime();
4819 if (now - V_dayUseDate(vp) > OneDay) {
4820 register int ndays, i;
4822 ndays = (now - V_dayUseDate(vp)) / OneDay;
4823 for (i = 6; i > ndays - 1; i--)
4824 V_weekUse(vp)[i] = V_weekUse(vp)[i - ndays];
4825 for (i = 0; i < ndays - 1 && i < 7; i++)
4826 V_weekUse(vp)[i] = 0;
4828 V_weekUse(vp)[ndays - 1] = V_dayUse(vp);
4830 V_dayUseDate(vp) = Midnight(now);
4832 #if OPENAFS_VOL_STATS
4834 * All we need to do is bzero the entire VOL_STATS_BYTES of
4835 * the detailed volume statistics area.
4837 memset((char *)(V_stat_area(vp)), 0, VOL_STATS_BYTES);
4838 #endif /* OPENAFS_VOL_STATS */
4841 /*It's been more than a day of collection */
4843 * Always return happily.
4846 } /*VAdjustVolumeStatistics */
4849 VAdjustVolumeStatistics(register Volume * vp)
4853 retVal = VAdjustVolumeStatistics_r(vp);
4859 VBumpVolumeUsage_r(register Volume * vp)
4861 unsigned int now = FT_ApproxTime();
4862 if (now - V_dayUseDate(vp) > OneDay)
4863 VAdjustVolumeStatistics_r(vp);
4865 * Save the volume header image to disk after every 128 bumps to dayUse.
4867 if ((V_dayUse(vp)++ & 127) == 0) {
4869 VUpdateVolume_r(&error, vp, VOL_UPDATE_WAIT);
4874 VBumpVolumeUsage(register Volume * vp)
4877 VBumpVolumeUsage_r(vp);
4882 VSetDiskUsage_r(void)
4884 #ifndef AFS_DEMAND_ATTACH_FS
4885 static int FifteenMinuteCounter = 0;
4889 /* NOTE: Don't attempt to access the partitions list until the
4890 * initialization level indicates that all volumes are attached,
4891 * which implies that all partitions are initialized. */
4892 #ifdef AFS_PTHREAD_ENV
4894 #else /* AFS_PTHREAD_ENV */
4896 #endif /* AFS_PTHREAD_ENV */
4899 VResetDiskUsage_r();
4901 #ifndef AFS_DEMAND_ATTACH_FS
4902 if (++FifteenMinuteCounter == 3) {
4903 FifteenMinuteCounter = 0;
4906 #endif /* !AFS_DEMAND_ATTACH_FS */
4918 /***************************************************/
4919 /* Volume Update List routines */
4920 /***************************************************/
4922 /* The number of minutes that a volume hasn't been updated before the
4923 * "Dont salvage" flag in the volume header will be turned on */
4924 #define SALVAGE_INTERVAL (10*60)
4929 * volume update list functionality has been moved into the VLRU
4930 * the DONT_SALVAGE flag is now set during VLRU demotion
4933 #ifndef AFS_DEMAND_ATTACH_FS
4934 static VolumeId *UpdateList = NULL; /* Pointer to array of Volume ID's */
4935 static int nUpdatedVolumes = 0; /* Updated with entry in UpdateList, salvage after crash flag on */
4936 static int updateSize = 0; /* number of entries possible */
4937 #define UPDATE_LIST_SIZE 128 /* initial size increment (must be a power of 2!) */
4938 #endif /* !AFS_DEMAND_ATTACH_FS */
4941 VAddToVolumeUpdateList_r(Error * ec, Volume * vp)
4944 vp->updateTime = FT_ApproxTime();
4945 if (V_dontSalvage(vp) == 0)
4947 V_dontSalvage(vp) = 0;
4948 VSyncVolume_r(ec, vp, 0);
4949 #ifdef AFS_DEMAND_ATTACH_FS
4950 V_attachFlags(vp) &= ~(VOL_HDR_DONTSALV);
4951 #else /* !AFS_DEMAND_ATTACH_FS */
4954 if (UpdateList == NULL) {
4955 updateSize = UPDATE_LIST_SIZE;
4956 UpdateList = (VolumeId *) malloc(sizeof(VolumeId) * updateSize);
4958 if (nUpdatedVolumes == updateSize) {
4960 if (updateSize > 524288) {
4961 Log("warning: there is likely a bug in the volume update scanner\n");
4965 (VolumeId *) realloc(UpdateList,
4966 sizeof(VolumeId) * updateSize);
4969 assert(UpdateList != NULL);
4970 UpdateList[nUpdatedVolumes++] = V_id(vp);
4971 #endif /* !AFS_DEMAND_ATTACH_FS */
4974 #ifndef AFS_DEMAND_ATTACH_FS
4976 VScanUpdateList(void)
4978 register int i, gap;
4979 register Volume *vp;
4981 afs_uint32 now = FT_ApproxTime();
4982 /* Be careful with this code, since it works with interleaved calls to AddToVolumeUpdateList */
4983 for (i = gap = 0; i < nUpdatedVolumes; i++) {
4985 UpdateList[i - gap] = UpdateList[i];
4987 /* XXX this routine needlessly messes up the Volume LRU by
4988 * breaking the LRU temporal-locality assumptions.....
4989 * we should use a special volume header allocator here */
4990 vp = VGetVolume_r(&error, UpdateList[i - gap] = UpdateList[i]);
4993 } else if (vp->nUsers == 1 && now - vp->updateTime > SALVAGE_INTERVAL) {
4994 V_dontSalvage(vp) = DONT_SALVAGE;
4995 VUpdateVolume_r(&error, vp, 0); /* No need to fsync--not critical */
5003 #ifndef AFS_PTHREAD_ENV
5005 #endif /* !AFS_PTHREAD_ENV */
5007 nUpdatedVolumes -= gap;
5009 #endif /* !AFS_DEMAND_ATTACH_FS */
5012 /***************************************************/
5013 /* Volume LRU routines */
5014 /***************************************************/
5019 * with demand attach fs, we attempt to soft detach(1)
5020 * volumes which have not been accessed in a long time
5021 * in order to speed up fileserver shutdown
5023 * (1) by soft detach we mean a process very similar
5024 * to VOffline, except the final state of the
5025 * Volume will be VOL_STATE_PREATTACHED, instead
5026 * of the usual VOL_STATE_UNATTACHED
5028 #ifdef AFS_DEMAND_ATTACH_FS
5030 /* implementation is reminiscent of a generational GC
5032 * queue 0 is newly attached volumes. this queue is
5033 * sorted by attach timestamp
5035 * queue 1 is volumes that have been around a bit
5036 * longer than queue 0. this queue is sorted by
5039 * queue 2 is volumes tha have been around the longest.
5040 * this queue is unsorted
5042 * queue 3 is volumes that have been marked as
5043 * candidates for soft detachment. this queue is
5046 #define VLRU_GENERATIONS 3 /**< number of generations in VLRU */
5047 #define VLRU_QUEUES 5 /**< total number of VLRU queues */
5050 * definition of a VLRU queue.
5053 volatile struct rx_queue q;
5060 * main VLRU data structure.
5063 struct VLRU_q q[VLRU_QUEUES]; /**< VLRU queues */
5066 /** time interval (in seconds) between promotion passes for
5067 * each young generation queue. */
5068 afs_uint32 promotion_interval[VLRU_GENERATIONS-1];
5070 /** time interval (in seconds) between soft detach candidate
5071 * scans for each generation queue.
5073 * scan_interval[VLRU_QUEUE_CANDIDATE] defines how frequently
5074 * we perform a soft detach pass. */
5075 afs_uint32 scan_interval[VLRU_GENERATIONS+1];
5077 /* scheduler state */
5078 int next_idx; /**< next queue to receive attention */
5079 afs_uint32 last_promotion[VLRU_GENERATIONS-1]; /**< timestamp of last promotion scan */
5080 afs_uint32 last_scan[VLRU_GENERATIONS+1]; /**< timestamp of last detach scan */
5082 int scanner_state; /**< state of scanner thread */
5083 pthread_cond_t cv; /**< state transition CV */
5086 /** global VLRU state */
5087 static struct VLRU volume_LRU;
5090 * defined states for VLRU scanner thread.
5093 VLRU_SCANNER_STATE_OFFLINE = 0, /**< vlru scanner thread is offline */
5094 VLRU_SCANNER_STATE_ONLINE = 1, /**< vlru scanner thread is online */
5095 VLRU_SCANNER_STATE_SHUTTING_DOWN = 2, /**< vlru scanner thread is shutting down */
5096 VLRU_SCANNER_STATE_PAUSING = 3, /**< vlru scanner thread is getting ready to pause */
5097 VLRU_SCANNER_STATE_PAUSED = 4 /**< vlru scanner thread is paused */
5098 } vlru_thread_state_t;
5100 /* vlru disk data header stuff */
5101 #define VLRU_DISK_MAGIC 0x7a8b9cad /**< vlru disk entry magic number */
5102 #define VLRU_DISK_VERSION 1 /**< vlru disk entry version number */
5104 /** vlru default expiration time (for eventual fs state serialization of vlru data) */
5105 #define VLRU_DUMP_EXPIRATION_TIME (60*60*24*7) /* expire vlru data after 1 week */
5108 /** minimum volume inactivity (in seconds) before a volume becomes eligible for
5109 * soft detachment. */
5110 static afs_uint32 VLRU_offline_thresh = VLRU_DEFAULT_OFFLINE_THRESH;
5112 /** time interval (in seconds) between VLRU scanner thread soft detach passes. */
5113 static afs_uint32 VLRU_offline_interval = VLRU_DEFAULT_OFFLINE_INTERVAL;
5115 /** maximum number of volumes to soft detach in a VLRU soft detach pass. */
5116 static afs_uint32 VLRU_offline_max = VLRU_DEFAULT_OFFLINE_MAX;
5118 /** VLRU control flag. non-zero value implies VLRU subsystem is activated. */
5119 static afs_uint32 VLRU_enabled = 1;
5121 /* queue synchronization routines */
5122 static void VLRU_BeginExclusive_r(struct VLRU_q * q);
5123 static void VLRU_EndExclusive_r(struct VLRU_q * q);
5124 static void VLRU_Wait_r(struct VLRU_q * q);
5127 * set VLRU subsystem tunable parameters.
5129 * @param[in] option tunable option to modify
5130 * @param[in] val new value for tunable parameter
5132 * @pre @c VInitVolumePackage has not yet been called.
5134 * @post tunable parameter is modified
5138 * @note valid option parameters are:
5139 * @arg @c VLRU_SET_THRESH
5140 * set the period of inactivity after which
5141 * volumes are eligible for soft detachment
5142 * @arg @c VLRU_SET_INTERVAL
5143 * set the time interval between calls
5144 * to the volume LRU "garbage collector"
5145 * @arg @c VLRU_SET_MAX
5146 * set the max number of volumes to deallocate
5150 VLRU_SetOptions(int option, afs_uint32 val)
5152 if (option == VLRU_SET_THRESH) {
5153 VLRU_offline_thresh = val;
5154 } else if (option == VLRU_SET_INTERVAL) {
5155 VLRU_offline_interval = val;
5156 } else if (option == VLRU_SET_MAX) {
5157 VLRU_offline_max = val;
5158 } else if (option == VLRU_SET_ENABLED) {
5161 VLRU_ComputeConstants();
5165 * compute VLRU internal timing parameters.
5167 * @post VLRU scanner thread internal timing parameters are computed
5169 * @note computes internal timing parameters based upon user-modifiable
5170 * tunable parameters.
5174 * @internal volume package internal use only.
5177 VLRU_ComputeConstants(void)
5179 afs_uint32 factor = VLRU_offline_thresh / VLRU_offline_interval;
5181 /* compute the candidate scan interval */
5182 volume_LRU.scan_interval[VLRU_QUEUE_CANDIDATE] = VLRU_offline_interval;
5184 /* compute the promotion intervals */
5185 volume_LRU.promotion_interval[VLRU_QUEUE_NEW] = VLRU_offline_thresh * 2;
5186 volume_LRU.promotion_interval[VLRU_QUEUE_MID] = VLRU_offline_thresh * 4;
5189 /* compute the gen 0 scan interval */
5190 volume_LRU.scan_interval[VLRU_QUEUE_NEW] = VLRU_offline_thresh / 8;
5192 /* compute the gen 0 scan interval */
5193 volume_LRU.scan_interval[VLRU_QUEUE_NEW] = VLRU_offline_interval * 2;
5198 * initialize VLRU subsystem.
5200 * @pre this function has not yet been called
5202 * @post VLRU subsystem is initialized and VLRU scanner thread is starting
5206 * @internal volume package internal use only.
5212 pthread_attr_t attrs;
5215 if (!VLRU_enabled) {
5216 Log("VLRU: disabled\n");
5220 /* initialize each of the VLRU queues */
5221 for (i = 0; i < VLRU_QUEUES; i++) {
5222 queue_Init(&volume_LRU.q[i]);
5223 volume_LRU.q[i].len = 0;
5224 volume_LRU.q[i].busy = 0;
5225 assert(pthread_cond_init(&volume_LRU.q[i].cv, NULL) == 0);
5228 /* setup the timing constants */
5229 VLRU_ComputeConstants();
5231 /* XXX put inside LogLevel check? */
5232 Log("VLRU: starting scanner with the following configuration parameters:\n");
5233 Log("VLRU: offlining volumes after minimum of %d seconds of inactivity\n", VLRU_offline_thresh);
5234 Log("VLRU: running VLRU soft detach pass every %d seconds\n", VLRU_offline_interval);
5235 Log("VLRU: taking up to %d volumes offline per pass\n", VLRU_offline_max);
5236 Log("VLRU: scanning generation 0 for inactive volumes every %d seconds\n", volume_LRU.scan_interval[0]);
5237 Log("VLRU: scanning for promotion/demotion between generations 0 and 1 every %d seconds\n", volume_LRU.promotion_interval[0]);
5238 Log("VLRU: scanning for promotion/demotion between generations 1 and 2 every %d seconds\n", volume_LRU.promotion_interval[1]);
5240 /* start up the VLRU scanner */
5241 volume_LRU.scanner_state = VLRU_SCANNER_STATE_OFFLINE;
5242 if (programType == fileServer) {
5243 assert(pthread_cond_init(&volume_LRU.cv, NULL) == 0);
5244 assert(pthread_attr_init(&attrs) == 0);
5245 assert(pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED) == 0);
5246 assert(pthread_create(&tid, &attrs, &VLRU_ScannerThread, NULL) == 0);
5251 * initialize the VLRU-related fields of a newly allocated volume object.
5253 * @param[in] vp pointer to volume object
5256 * @arg @c VOL_LOCK is held.
5257 * @arg volume object is not on a VLRU queue.
5259 * @post VLRU fields are initialized to indicate that volume object is not
5260 * currently registered with the VLRU subsystem
5264 * @internal volume package interal use only.
5267 VLRU_Init_Node_r(volatile Volume * vp)
5272 assert(queue_IsNotOnQueue(&vp->vlru));
5273 vp->vlru.idx = VLRU_QUEUE_INVALID;
5277 * add a volume object to a VLRU queue.
5279 * @param[in] vp pointer to volume object
5282 * @arg @c VOL_LOCK is held.
5283 * @arg caller MUST hold a lightweight ref on @p vp.
5284 * @arg caller MUST NOT hold exclusive ownership of the VLRU queue.
5286 * @post the volume object is added to the appropriate VLRU queue
5288 * @note if @c vp->vlru.idx contains the index of a valid VLRU queue,
5289 * then the volume is added to that queue. Otherwise, the value
5290 * @c VLRU_QUEUE_NEW is stored into @c vp->vlru.idx and the
5291 * volume is added to the NEW generation queue.
5293 * @note @c VOL_LOCK may be dropped internally
5295 * @note Volume state is temporarily set to @c VOL_STATE_VLRU_ADD
5296 * during the add operation, and is restored to the previous
5297 * state prior to return.
5301 * @internal volume package internal use only.
5304 VLRU_Add_r(volatile Volume * vp)
5307 VolState state_save;
5312 if (queue_IsOnQueue(&vp->vlru))
5315 state_save = VChangeState_r(vp, VOL_STATE_VLRU_ADD);
5318 if ((idx < 0) || (idx >= VLRU_QUEUE_INVALID)) {
5319 idx = VLRU_QUEUE_NEW;
5322 VLRU_Wait_r(&volume_LRU.q[idx]);
5324 /* repeat check since VLRU_Wait_r may have dropped
5326 if (queue_IsNotOnQueue(&vp->vlru)) {
5328 queue_Prepend(&volume_LRU.q[idx], &vp->vlru);
5329 volume_LRU.q[idx].len++;
5330 V_attachFlags(vp) |= VOL_ON_VLRU;
5331 vp->stats.last_promote = FT_ApproxTime();
5334 VChangeState_r(vp, state_save);
5338 * delete a volume object from a VLRU queue.
5340 * @param[in] vp pointer to volume object
5343 * @arg @c VOL_LOCK is held.
5344 * @arg caller MUST hold a lightweight ref on @p vp.
5345 * @arg caller MUST NOT hold exclusive ownership of the VLRU queue.
5347 * @post volume object is removed from the VLRU queue
5349 * @note @c VOL_LOCK may be dropped internally
5353 * @todo We should probably set volume state to something exlcusive
5354 * (as @c VLRU_Add_r does) prior to dropping @c VOL_LOCK.
5356 * @internal volume package internal use only.
5359 VLRU_Delete_r(volatile Volume * vp)
5366 if (queue_IsNotOnQueue(&vp->vlru))
5372 if (idx == VLRU_QUEUE_INVALID)
5374 VLRU_Wait_r(&volume_LRU.q[idx]);
5375 } while (idx != vp->vlru.idx);
5377 /* now remove from the VLRU and update
5378 * the appropriate counter */
5379 queue_Remove(&vp->vlru);
5380 volume_LRU.q[idx].len--;
5381 vp->vlru.idx = VLRU_QUEUE_INVALID;
5382 V_attachFlags(vp) &= ~(VOL_ON_VLRU);
5386 * tell the VLRU subsystem that a volume was just accessed.
5388 * @param[in] vp pointer to volume object
5391 * @arg @c VOL_LOCK is held
5392 * @arg caller MUST hold a lightweight ref on @p vp
5393 * @arg caller MUST NOT hold exclusive ownership of any VLRU queue
5395 * @post volume VLRU access statistics are updated. If the volume was on
5396 * the VLRU soft detach candidate queue, it is moved to the NEW
5399 * @note @c VOL_LOCK may be dropped internally
5403 * @internal volume package internal use only.
5406 VLRU_UpdateAccess_r(volatile Volume * vp)
5408 afs_uint32 live_interval;
5409 Volume * rvp = NULL;
5414 if (queue_IsNotOnQueue(&vp->vlru))
5417 assert(V_attachFlags(vp) & VOL_ON_VLRU);
5419 /* update the access timestamp */
5420 vp->stats.last_get = FT_ApproxTime();
5423 * if the volume is on the soft detach candidate
5424 * list, we need to safely move it back to a
5425 * regular generation. this has to be done
5426 * carefully so we don't race against the scanner
5430 /* if this volume is on the soft detach candidate queue,
5431 * then grab exclusive access to the necessary queues */
5432 if (vp->vlru.idx == VLRU_QUEUE_CANDIDATE) {
5434 VCreateReservation_r(rvp);
5436 VLRU_Wait_r(&volume_LRU.q[VLRU_QUEUE_NEW]);
5437 VLRU_BeginExclusive_r(&volume_LRU.q[VLRU_QUEUE_NEW]);
5438 VLRU_Wait_r(&volume_LRU.q[VLRU_QUEUE_CANDIDATE]);
5439 VLRU_BeginExclusive_r(&volume_LRU.q[VLRU_QUEUE_CANDIDATE]);
5442 /* make sure multiple threads don't race to update */
5443 if (vp->vlru.idx == VLRU_QUEUE_CANDIDATE) {
5444 VLRU_SwitchQueues(vp, VLRU_QUEUE_NEW, 1);
5448 VLRU_EndExclusive_r(&volume_LRU.q[VLRU_QUEUE_CANDIDATE]);
5449 VLRU_EndExclusive_r(&volume_LRU.q[VLRU_QUEUE_NEW]);
5450 VCancelReservation_r(rvp);
5455 * switch a volume between two VLRU queues.
5457 * @param[in] vp pointer to volume object
5458 * @param[in] new_idx index of VLRU queue onto which the volume will be moved
5459 * @param[in] append controls whether the volume will be appended or
5460 * prepended to the queue. A nonzero value means it will
5461 * be appended; zero means it will be prepended.
5463 * @pre The new (and old, if applicable) queue(s) must either be owned
5464 * exclusively by the calling thread for asynchronous manipulation,
5465 * or the queue(s) must be quiescent and VOL_LOCK must be held.
5466 * Please see VLRU_BeginExclusive_r, VLRU_EndExclusive_r and VLRU_Wait_r
5467 * for further details of the queue asynchronous processing mechanism.
5469 * @post If the volume object was already on a VLRU queue, it is
5470 * removed from the queue. Depending on the value of the append
5471 * parameter, the volume object is either appended or prepended
5472 * to the VLRU queue referenced by the new_idx parameter.
5476 * @see VLRU_BeginExclusive_r
5477 * @see VLRU_EndExclusive_r
5480 * @internal volume package internal use only.
5483 VLRU_SwitchQueues(volatile Volume * vp, int new_idx, int append)
5485 if (queue_IsNotOnQueue(&vp->vlru))
5488 queue_Remove(&vp->vlru);
5489 volume_LRU.q[vp->vlru.idx].len--;
5491 /* put the volume back on the correct generational queue */
5493 queue_Append(&volume_LRU.q[new_idx], &vp->vlru);
5495 queue_Prepend(&volume_LRU.q[new_idx], &vp->vlru);
5498 volume_LRU.q[new_idx].len++;
5499 vp->vlru.idx = new_idx;
5503 * VLRU background thread.
5505 * The VLRU Scanner Thread is responsible for periodically scanning through
5506 * each VLRU queue looking for volumes which should be moved to another
5507 * queue, or soft detached.
5509 * @param[in] args unused thread arguments parameter
5511 * @return unused thread return value
5512 * @retval NULL always
5514 * @internal volume package internal use only.
5517 VLRU_ScannerThread(void * args)
5519 afs_uint32 now, min_delay, delay;
5520 afs_uint32 next_scan[VLRU_GENERATIONS];
5521 afs_uint32 next_promotion[VLRU_GENERATIONS];
5522 int i, min_idx, min_op, overdue, state;
5524 /* set t=0 for promotion cycle to be
5525 * fileserver startup */
5526 now = FT_ApproxTime();
5527 for (i=0; i < VLRU_GENERATIONS-1; i++) {
5528 volume_LRU.last_promotion[i] = now;
5531 /* don't start the scanner until VLRU_offline_thresh
5532 * plus a small delay for VInitVolumePackage to finish
5535 sleep(VLRU_offline_thresh + 60);
5537 /* set t=0 for scan cycle to be now */
5538 now = FT_ApproxTime();
5539 for (i=0; i < VLRU_GENERATIONS+1; i++) {
5540 volume_LRU.last_scan[i] = now;
5544 if (volume_LRU.scanner_state == VLRU_SCANNER_STATE_OFFLINE) {
5545 volume_LRU.scanner_state = VLRU_SCANNER_STATE_ONLINE;
5548 while ((state = volume_LRU.scanner_state) != VLRU_SCANNER_STATE_SHUTTING_DOWN) {
5549 /* check to see if we've been asked to pause */
5550 if (volume_LRU.scanner_state == VLRU_SCANNER_STATE_PAUSING) {
5551 volume_LRU.scanner_state = VLRU_SCANNER_STATE_PAUSED;
5552 assert(pthread_cond_broadcast(&volume_LRU.cv) == 0);
5554 VOL_CV_WAIT(&volume_LRU.cv);
5555 } while (volume_LRU.scanner_state == VLRU_SCANNER_STATE_PAUSED);
5558 /* scheduling can happen outside the glock */
5561 /* figure out what is next on the schedule */
5563 /* figure out a potential schedule for the new generation first */
5565 min_delay = volume_LRU.scan_interval[0] + volume_LRU.last_scan[0] - now;
5568 if (min_delay > volume_LRU.scan_interval[0]) {
5569 /* unsigned overflow -- we're overdue to run this scan */
5574 /* if we're not overdue for gen 0, figure out schedule for candidate gen */
5576 i = VLRU_QUEUE_CANDIDATE;
5577 delay = volume_LRU.scan_interval[i] + volume_LRU.last_scan[i] - now;
5578 if (delay < min_delay) {
5582 if (delay > volume_LRU.scan_interval[i]) {
5583 /* unsigned overflow -- we're overdue to run this scan */
5591 /* if we're still not overdue for something, figure out schedules for promotions */
5592 for (i=0; !overdue && i < VLRU_GENERATIONS-1; i++) {
5593 delay = volume_LRU.promotion_interval[i] + volume_LRU.last_promotion[i] - now;
5594 if (delay < min_delay) {
5599 if (delay > volume_LRU.promotion_interval[i]) {
5600 /* unsigned overflow -- we're overdue to run this promotion */
5609 /* sleep as needed */
5614 /* do whatever is next */
5617 VLRU_Promote_r(min_idx);
5618 VLRU_Demote_r(min_idx+1);
5620 VLRU_Scan_r(min_idx);
5622 now = FT_ApproxTime();
5625 Log("VLRU scanner asked to go offline (scanner_state=%d)\n", state);
5627 /* signal that scanner is down */
5628 volume_LRU.scanner_state = VLRU_SCANNER_STATE_OFFLINE;
5629 assert(pthread_cond_broadcast(&volume_LRU.cv) == 0);
5635 * promote volumes from one VLRU generation to the next.
5637 * This routine scans a VLRU generation looking for volumes which are
5638 * eligible to be promoted to the next generation. All volumes which
5639 * meet the eligibility requirement are promoted.
5641 * Promotion eligibility is based upon meeting both of the following
5644 * @arg The volume has been accessed since the last promotion:
5645 * @c (vp->stats.last_get >= vp->stats.last_promote)
5646 * @arg The last promotion occurred at least
5647 * @c volume_LRU.promotion_interval[idx] seconds ago
5649 * As a performance optimization, promotions are "globbed". In other
5650 * words, we promote arbitrarily large contiguous sublists of elements
5653 * @param[in] idx VLRU queue index to scan
5657 * @internal VLRU internal use only.
5660 VLRU_Promote_r(int idx)
5662 int len, chaining, promote;
5663 afs_uint32 now, thresh;
5664 struct rx_queue *qp, *nqp;
5665 Volume * vp, *start, *end;
5667 /* get exclusive access to two chains, and drop the glock */
5668 VLRU_Wait_r(&volume_LRU.q[idx]);
5669 VLRU_BeginExclusive_r(&volume_LRU.q[idx]);
5670 VLRU_Wait_r(&volume_LRU.q[idx+1]);
5671 VLRU_BeginExclusive_r(&volume_LRU.q[idx+1]);
5674 thresh = volume_LRU.promotion_interval[idx];
5675 now = FT_ApproxTime();
5678 for (queue_ScanBackwards(&volume_LRU.q[idx], qp, nqp, rx_queue)) {
5679 vp = (Volume *)((char *)qp - offsetof(Volume, vlru));
5680 promote = (((vp->stats.last_promote + thresh) <= now) &&
5681 (vp->stats.last_get >= vp->stats.last_promote));
5689 /* promote and prepend chain */
5690 queue_MoveChainAfter(&volume_LRU.q[idx+1], &start->vlru, &end->vlru);
5704 /* promote and prepend */
5705 queue_MoveChainAfter(&volume_LRU.q[idx+1], &start->vlru, &end->vlru);
5709 volume_LRU.q[idx].len -= len;
5710 volume_LRU.q[idx+1].len += len;
5713 /* release exclusive access to the two chains */
5715 volume_LRU.last_promotion[idx] = now;
5716 VLRU_EndExclusive_r(&volume_LRU.q[idx+1]);
5717 VLRU_EndExclusive_r(&volume_LRU.q[idx]);
5720 /* run the demotions */
5722 VLRU_Demote_r(int idx)
5725 int len, chaining, demote;
5726 afs_uint32 now, thresh;
5727 struct rx_queue *qp, *nqp;
5728 Volume * vp, *start, *end;
5729 Volume ** salv_flag_vec = NULL;
5730 int salv_vec_offset = 0;
5732 assert(idx == VLRU_QUEUE_MID || idx == VLRU_QUEUE_OLD);
5734 /* get exclusive access to two chains, and drop the glock */
5735 VLRU_Wait_r(&volume_LRU.q[idx-1]);
5736 VLRU_BeginExclusive_r(&volume_LRU.q[idx-1]);
5737 VLRU_Wait_r(&volume_LRU.q[idx]);
5738 VLRU_BeginExclusive_r(&volume_LRU.q[idx]);
5741 /* no big deal if this allocation fails */
5742 if (volume_LRU.q[idx].len) {
5743 salv_flag_vec = (Volume **) malloc(volume_LRU.q[idx].len * sizeof(Volume *));
5746 now = FT_ApproxTime();
5747 thresh = volume_LRU.promotion_interval[idx-1];
5750 for (queue_ScanBackwards(&volume_LRU.q[idx], qp, nqp, rx_queue)) {
5751 vp = (Volume *)((char *)qp - offsetof(Volume, vlru));
5752 demote = (((vp->stats.last_promote + thresh) <= now) &&
5753 (vp->stats.last_get < (now - thresh)));
5755 /* we now do volume update list DONT_SALVAGE flag setting during
5756 * demotion passes */
5757 if (salv_flag_vec &&
5758 !(V_attachFlags(vp) & VOL_HDR_DONTSALV) &&
5760 (vp->updateTime < (now - SALVAGE_INTERVAL)) &&
5761 (V_attachState(vp) == VOL_STATE_ATTACHED)) {
5762 salv_flag_vec[salv_vec_offset++] = vp;
5763 VCreateReservation_r(vp);
5772 /* demote and append chain */
5773 queue_MoveChainBefore(&volume_LRU.q[idx-1], &start->vlru, &end->vlru);
5787 queue_MoveChainBefore(&volume_LRU.q[idx-1], &start->vlru, &end->vlru);
5791 volume_LRU.q[idx].len -= len;
5792 volume_LRU.q[idx-1].len += len;
5795 /* release exclusive access to the two chains */
5797 VLRU_EndExclusive_r(&volume_LRU.q[idx]);
5798 VLRU_EndExclusive_r(&volume_LRU.q[idx-1]);
5800 /* now go back and set the DONT_SALVAGE flags as appropriate */
5801 if (salv_flag_vec) {
5803 for (i = 0; i < salv_vec_offset; i++) {
5804 vp = salv_flag_vec[i];
5805 if (!(V_attachFlags(vp) & VOL_HDR_DONTSALV) &&
5806 (vp->updateTime < (now - SALVAGE_INTERVAL)) &&
5807 (V_attachState(vp) == VOL_STATE_ATTACHED)) {
5810 V_attachFlags(vp) |= VOL_HDR_DONTSALV;
5811 V_dontSalvage(vp) = DONT_SALVAGE;
5812 VUpdateVolume_r(&ec, vp, 0);
5816 VCancelReservation_r(vp);
5818 free(salv_flag_vec);
5822 /* run a pass of the VLRU GC scanner */
5824 VLRU_Scan_r(int idx)
5826 afs_uint32 now, thresh;
5827 struct rx_queue *qp, *nqp;
5828 volatile Volume * vp;
5831 assert(idx == VLRU_QUEUE_NEW || idx == VLRU_QUEUE_CANDIDATE);
5833 /* gain exclusive access to the idx VLRU */
5834 VLRU_Wait_r(&volume_LRU.q[idx]);
5835 VLRU_BeginExclusive_r(&volume_LRU.q[idx]);
5837 if (idx != VLRU_QUEUE_CANDIDATE) {
5838 /* gain exclusive access to the candidate VLRU */
5839 VLRU_Wait_r(&volume_LRU.q[VLRU_QUEUE_CANDIDATE]);
5840 VLRU_BeginExclusive_r(&volume_LRU.q[VLRU_QUEUE_CANDIDATE]);
5843 now = FT_ApproxTime();
5844 thresh = now - VLRU_offline_thresh;
5846 /* perform candidate selection and soft detaching */
5847 if (idx == VLRU_QUEUE_CANDIDATE) {
5848 /* soft detach some volumes from the candidate pool */
5852 for (i=0,queue_ScanBackwards(&volume_LRU.q[idx], qp, nqp, rx_queue)) {
5853 vp = (Volume *)((char *)qp - offsetof(Volume, vlru));
5854 if (i >= VLRU_offline_max) {
5857 /* check timestamp to see if it's a candidate for soft detaching */
5858 if (vp->stats.last_get <= thresh) {
5860 if (VCheckSoftDetach(vp, thresh))
5866 /* scan for volumes to become soft detach candidates */
5867 for (i=1,queue_ScanBackwards(&volume_LRU.q[idx], qp, nqp, rx_queue),i++) {
5868 vp = (Volume *)((char *)qp - offsetof(Volume, vlru));
5870 /* check timestamp to see if it's a candidate for soft detaching */
5871 if (vp->stats.last_get <= thresh) {
5872 VCheckSoftDetachCandidate(vp, thresh);
5875 if (!(i&0x7f)) { /* lock coarsening optimization */
5883 /* relinquish exclusive access to the VLRU chains */
5887 volume_LRU.last_scan[idx] = now;
5888 if (idx != VLRU_QUEUE_CANDIDATE) {
5889 VLRU_EndExclusive_r(&volume_LRU.q[VLRU_QUEUE_CANDIDATE]);
5891 VLRU_EndExclusive_r(&volume_LRU.q[idx]);
5894 /* check whether volume is safe to soft detach
5895 * caller MUST NOT hold a ref count on vp */
5897 VCheckSoftDetach(volatile Volume * vp, afs_uint32 thresh)
5901 if (vp->nUsers || vp->nWaiters)
5904 if (vp->stats.last_get <= thresh) {
5905 ret = VSoftDetachVolume_r(vp, thresh);
5911 /* check whether volume should be made a
5912 * soft detach candidate */
5914 VCheckSoftDetachCandidate(volatile Volume * vp, afs_uint32 thresh)
5917 if (vp->nUsers || vp->nWaiters)
5922 assert(idx == VLRU_QUEUE_NEW);
5924 if (vp->stats.last_get <= thresh) {
5925 /* move to candidate pool */
5926 queue_Remove(&vp->vlru);
5927 volume_LRU.q[VLRU_QUEUE_NEW].len--;
5928 queue_Prepend(&volume_LRU.q[VLRU_QUEUE_CANDIDATE], &vp->vlru);
5929 vp->vlru.idx = VLRU_QUEUE_CANDIDATE;
5930 volume_LRU.q[VLRU_QUEUE_CANDIDATE].len++;
5938 /* begin exclusive access on VLRU */
5940 VLRU_BeginExclusive_r(struct VLRU_q * q)
5942 assert(q->busy == 0);
5946 /* end exclusive access on VLRU */
5948 VLRU_EndExclusive_r(struct VLRU_q * q)
5952 assert(pthread_cond_broadcast(&q->cv) == 0);
5955 /* wait for another thread to end exclusive access on VLRU */
5957 VLRU_Wait_r(struct VLRU_q * q)
5960 VOL_CV_WAIT(&q->cv);
5965 * volume soft detach
5967 * caller MUST NOT hold a ref count on vp */
5969 VSoftDetachVolume_r(volatile Volume * vp, afs_uint32 thresh)
5974 assert(vp->vlru.idx == VLRU_QUEUE_CANDIDATE);
5976 ts_save = vp->stats.last_get;
5977 if (ts_save > thresh)
5980 if (vp->nUsers || vp->nWaiters)
5983 if (VIsExclusiveState(V_attachState(vp))) {
5987 switch (V_attachState(vp)) {
5988 case VOL_STATE_UNATTACHED:
5989 case VOL_STATE_PREATTACHED:
5990 case VOL_STATE_ERROR:
5991 case VOL_STATE_GOING_OFFLINE:
5992 case VOL_STATE_SHUTTING_DOWN:
5993 case VOL_STATE_SALVAGING:
5994 volume_LRU.q[vp->vlru.idx].len--;
5996 /* create and cancel a reservation to
5997 * give the volume an opportunity to
5999 VCreateReservation_r(vp);
6000 queue_Remove(&vp->vlru);
6001 vp->vlru.idx = VLRU_QUEUE_INVALID;
6002 V_attachFlags(vp) &= ~(VOL_ON_VLRU);
6003 VCancelReservation_r(vp);
6007 /* hold the volume and take it offline.
6008 * no need for reservations, as VHold_r
6009 * takes care of that internally. */
6010 if (VHold_r(vp) == 0) {
6011 /* vhold drops the glock, so now we should
6012 * check to make sure we aren't racing against
6013 * other threads. if we are racing, offlining vp
6014 * would be wasteful, and block the scanner for a while
6018 (vp->shuttingDown) ||
6019 (vp->goingOffline) ||
6020 (vp->stats.last_get != ts_save)) {
6021 /* looks like we're racing someone else. bail */
6025 /* pull it off the VLRU */
6026 assert(vp->vlru.idx == VLRU_QUEUE_CANDIDATE);
6027 volume_LRU.q[VLRU_QUEUE_CANDIDATE].len--;
6028 queue_Remove(&vp->vlru);
6029 vp->vlru.idx = VLRU_QUEUE_INVALID;
6030 V_attachFlags(vp) &= ~(VOL_ON_VLRU);
6032 /* take if offline */
6033 VOffline_r(vp, "volume has been soft detached");
6035 /* invalidate the volume header cache */
6036 FreeVolumeHeader(vp);
6039 IncUInt64(&VStats.soft_detaches);
6040 vp->stats.soft_detaches++;
6042 /* put in pre-attached state so demand
6043 * attacher can work on it */
6044 VChangeState_r(vp, VOL_STATE_PREATTACHED);
6050 #endif /* AFS_DEMAND_ATTACH_FS */
6053 /***************************************************/
6054 /* Volume Header Cache routines */
6055 /***************************************************/
6058 * volume header cache.
6060 struct volume_hdr_LRU_t volume_hdr_LRU;
6063 * initialize the volume header cache.
6065 * @param[in] howMany number of header cache entries to preallocate
6067 * @pre VOL_LOCK held. Function has never been called before.
6069 * @post howMany cache entries are allocated, initialized, and added
6070 * to the LRU list. Header cache statistics are initialized.
6072 * @note only applicable to fileServer program type. Should only be
6073 * called once during volume package initialization.
6075 * @internal volume package internal use only.
6078 VInitVolumeHeaderCache(afs_uint32 howMany)
6080 register struct volHeader *hp;
6081 if (programType != fileServer)
6083 queue_Init(&volume_hdr_LRU);
6084 volume_hdr_LRU.stats.free = 0;
6085 volume_hdr_LRU.stats.used = howMany;
6086 volume_hdr_LRU.stats.attached = 0;
6087 hp = (struct volHeader *)(calloc(howMany, sizeof(struct volHeader)));
6089 ReleaseVolumeHeader(hp++);
6093 * get a volume header and attach it to the volume object.
6095 * @param[in] vp pointer to volume object
6097 * @return cache entry status
6098 * @retval 0 volume header was newly attached; cache data is invalid
6099 * @retval 1 volume header was previously attached; cache data is valid
6101 * @pre VOL_LOCK held. For DAFS, lightweight ref must be held on volume object.
6103 * @post volume header attached to volume object. if necessary, header cache
6104 * entry on LRU is synchronized to disk. Header is removed from LRU list.
6106 * @note VOL_LOCK may be dropped
6108 * @warning this interface does not load header data from disk. it merely
6109 * attaches a header object to the volume object, and may sync the old
6110 * header cache data out to disk in the process.
6112 * @internal volume package internal use only.
6115 GetVolumeHeader(register Volume * vp)
6118 register struct volHeader *hd;
6120 static int everLogged = 0;
6122 #ifdef AFS_DEMAND_ATTACH_FS
6123 VolState vp_save, back_save;
6125 /* XXX debug 9/19/05 we've apparently got
6126 * a ref counting bug somewhere that's
6127 * breaking the nUsers == 0 => header on LRU
6129 if (vp->header && queue_IsNotOnQueue(vp->header)) {
6130 Log("nUsers == 0, but header not on LRU\n");
6135 old = (vp->header != NULL); /* old == volume already has a header */
6137 if (programType != fileServer) {
6138 /* for volume utilities, we allocate volHeaders as needed */
6140 hd = (struct volHeader *)calloc(1, sizeof(*vp->header));
6144 #ifdef AFS_DEMAND_ATTACH_FS
6145 V_attachFlags(vp) |= VOL_HDR_ATTACHED;
6149 /* for the fileserver, we keep a volume header cache */
6151 /* the header we previously dropped in the lru is
6152 * still available. pull it off the lru and return */
6155 assert(hd->back == vp);
6157 /* we need to grab a new element off the LRU */
6158 if (queue_IsNotEmpty(&volume_hdr_LRU)) {
6159 /* grab an element and pull off of LRU */
6160 hd = queue_First(&volume_hdr_LRU, volHeader);
6163 /* LRU is empty, so allocate a new volHeader
6164 * this is probably indicative of a leak, so let the user know */
6165 hd = (struct volHeader *)calloc(1, sizeof(struct volHeader));
6168 Log("****Allocated more volume headers, probably leak****\n");
6171 volume_hdr_LRU.stats.free++;
6174 /* this header used to belong to someone else.
6175 * we'll need to check if the header needs to
6176 * be sync'd out to disk */
6178 #ifdef AFS_DEMAND_ATTACH_FS
6179 /* if hd->back were in an exclusive state, then
6180 * its volHeader would not be on the LRU... */
6181 assert(!VIsExclusiveState(V_attachState(hd->back)));
6184 if (hd->diskstuff.inUse) {
6185 /* volume was in use, so we'll need to sync
6186 * its header to disk */
6188 #ifdef AFS_DEMAND_ATTACH_FS
6189 back_save = VChangeState_r(hd->back, VOL_STATE_UPDATING);
6190 vp_save = VChangeState_r(vp, VOL_STATE_HDR_ATTACHING);
6191 VCreateReservation_r(hd->back);
6195 WriteVolumeHeader_r(&error, hd->back);
6196 /* Ignore errors; catch them later */
6198 #ifdef AFS_DEMAND_ATTACH_FS
6203 hd->back->header = NULL;
6204 #ifdef AFS_DEMAND_ATTACH_FS
6205 V_attachFlags(hd->back) &= ~(VOL_HDR_ATTACHED | VOL_HDR_LOADED | VOL_HDR_IN_LRU);
6207 if (hd->diskstuff.inUse) {
6208 VChangeState_r(hd->back, back_save);
6209 VCancelReservation_r(hd->back);
6210 VChangeState_r(vp, vp_save);
6214 volume_hdr_LRU.stats.attached++;
6218 #ifdef AFS_DEMAND_ATTACH_FS
6219 V_attachFlags(vp) |= VOL_HDR_ATTACHED;
6222 volume_hdr_LRU.stats.free--;
6223 volume_hdr_LRU.stats.used++;
6225 IncUInt64(&VStats.hdr_gets);
6226 #ifdef AFS_DEMAND_ATTACH_FS
6227 IncUInt64(&vp->stats.hdr_gets);
6228 vp->stats.last_hdr_get = FT_ApproxTime();
6235 * make sure volume header is attached and contains valid cache data.
6237 * @param[out] ec outbound error code
6238 * @param[in] vp pointer to volume object
6240 * @pre VOL_LOCK held. For DAFS, lightweight ref held on vp.
6242 * @post header cache entry attached, and loaded with valid data, or
6243 * *ec is nonzero, and the header is released back into the LRU.
6245 * @internal volume package internal use only.
6248 LoadVolumeHeader(Error * ec, Volume * vp)
6250 #ifdef AFS_DEMAND_ATTACH_FS
6251 VolState state_save;
6255 if (vp->nUsers == 0 && !GetVolumeHeader(vp)) {
6256 IncUInt64(&VStats.hdr_loads);
6257 state_save = VChangeState_r(vp, VOL_STATE_HDR_LOADING);
6260 ReadHeader(ec, V_diskDataHandle(vp), (char *)&V_disk(vp),
6261 sizeof(V_disk(vp)), VOLUMEINFOMAGIC,
6263 IncUInt64(&vp->stats.hdr_loads);
6264 now = FT_ApproxTime();
6268 V_attachFlags(vp) |= VOL_HDR_LOADED;
6269 vp->stats.last_hdr_load = now;
6271 VChangeState_r(vp, state_save);
6273 #else /* AFS_DEMAND_ATTACH_FS */
6275 if (vp->nUsers == 0 && !GetVolumeHeader(vp)) {
6276 IncUInt64(&VStats.hdr_loads);
6278 ReadHeader(ec, V_diskDataHandle(vp), (char *)&V_disk(vp),
6279 sizeof(V_disk(vp)), VOLUMEINFOMAGIC,
6282 #endif /* AFS_DEMAND_ATTACH_FS */
6284 /* maintain (nUsers==0) => header in LRU invariant */
6285 ReleaseVolumeHeader(vp->header);
6290 * release a header cache entry back into the LRU list.
6292 * @param[in] hd pointer to volume header cache object
6294 * @pre VOL_LOCK held.
6296 * @post header cache object appended onto end of LRU list.
6298 * @note only applicable to fileServer program type.
6300 * @note used to place a header cache entry back into the
6301 * LRU pool without invalidating it as a cache entry.
6303 * @internal volume package internal use only.
6306 ReleaseVolumeHeader(register struct volHeader *hd)
6308 if (programType != fileServer)
6310 if (!hd || queue_IsOnQueue(hd)) /* no header, or header already released */
6312 queue_Append(&volume_hdr_LRU, hd);
6313 #ifdef AFS_DEMAND_ATTACH_FS
6315 V_attachFlags(hd->back) |= VOL_HDR_IN_LRU;
6318 volume_hdr_LRU.stats.free++;
6319 volume_hdr_LRU.stats.used--;
6323 * free/invalidate a volume header cache entry.
6325 * @param[in] vp pointer to volume object
6327 * @pre VOL_LOCK is held.
6329 * @post For fileserver, header cache entry is returned to LRU, and it is
6330 * invalidated as a cache entry. For volume utilities, the header
6331 * cache entry is freed.
6333 * @note For fileserver, this should be utilized instead of ReleaseVolumeHeader
6334 * whenever it is necessary to invalidate the header cache entry.
6336 * @see ReleaseVolumeHeader
6338 * @internal volume package internal use only.
6341 FreeVolumeHeader(register Volume * vp)
6343 register struct volHeader *hd = vp->header;
6346 if (programType == fileServer) {
6347 ReleaseVolumeHeader(hd);
6352 #ifdef AFS_DEMAND_ATTACH_FS
6353 V_attachFlags(vp) &= ~(VOL_HDR_ATTACHED | VOL_HDR_IN_LRU | VOL_HDR_LOADED);
6355 volume_hdr_LRU.stats.attached--;
6360 /***************************************************/
6361 /* Volume Hash Table routines */
6362 /***************************************************/
6365 * set size of volume object hash table.
6367 * @param[in] logsize log(2) of desired hash table size
6369 * @return operation status
6371 * @retval -1 failure
6373 * @pre MUST be called prior to VInitVolumePackage
6375 * @post Volume Hash Table will have 2^logsize buckets
6378 VSetVolHashSize(int logsize)
6380 /* 64 to 16384 hash buckets seems like a reasonable range */
6381 if ((logsize < 6 ) || (logsize > 14)) {
6386 VolumeHashTable.Size = 1 << logsize;
6387 VolumeHashTable.Mask = VolumeHashTable.Size - 1;
6389 /* we can't yet support runtime modification of this
6390 * parameter. we'll need a configuration rwlock to
6391 * make runtime modification feasible.... */
6398 * initialize dynamic data structures for volume hash table.
6400 * @post hash table is allocated, and fields are initialized.
6402 * @internal volume package internal use only.
6405 VInitVolumeHash(void)
6409 VolumeHashTable.Table = (VolumeHashChainHead *) calloc(VolumeHashTable.Size,
6410 sizeof(VolumeHashChainHead));
6411 assert(VolumeHashTable.Table != NULL);
6413 for (i=0; i < VolumeHashTable.Size; i++) {
6414 queue_Init(&VolumeHashTable.Table[i]);
6415 #ifdef AFS_DEMAND_ATTACH_FS
6416 assert(pthread_cond_init(&VolumeHashTable.Table[i].chain_busy_cv, NULL) == 0);
6417 #endif /* AFS_DEMAND_ATTACH_FS */
6422 * add a volume object to the hash table.
6424 * @param[in] vp pointer to volume object
6425 * @param[in] hashid hash of volume id
6427 * @pre VOL_LOCK is held. For DAFS, caller must hold a lightweight
6430 * @post volume is added to hash chain.
6432 * @internal volume package internal use only.
6434 * @note For DAFS, VOL_LOCK may be dropped in order to wait for an
6435 * asynchronous hash chain reordering to finish.
6438 AddVolumeToHashTable(register Volume * vp, int hashid)
6440 VolumeHashChainHead * head;
6442 if (queue_IsOnQueue(vp))
6445 head = &VolumeHashTable.Table[VOLUME_HASH(hashid)];
6447 #ifdef AFS_DEMAND_ATTACH_FS
6448 /* wait for the hash chain to become available */
6451 V_attachFlags(vp) |= VOL_IN_HASH;
6452 vp->chainCacheCheck = ++head->cacheCheck;
6453 #endif /* AFS_DEMAND_ATTACH_FS */
6456 vp->hashid = hashid;
6457 queue_Append(head, vp);
6458 vp->vnodeHashOffset = VolumeHashOffset_r();
6462 * delete a volume object from the hash table.
6464 * @param[in] vp pointer to volume object
6466 * @pre VOL_LOCK is held. For DAFS, caller must hold a lightweight
6469 * @post volume is removed from hash chain.
6471 * @internal volume package internal use only.
6473 * @note For DAFS, VOL_LOCK may be dropped in order to wait for an
6474 * asynchronous hash chain reordering to finish.
6477 DeleteVolumeFromHashTable(register Volume * vp)
6479 VolumeHashChainHead * head;
6481 if (!queue_IsOnQueue(vp))
6484 head = &VolumeHashTable.Table[VOLUME_HASH(vp->hashid)];
6486 #ifdef AFS_DEMAND_ATTACH_FS
6487 /* wait for the hash chain to become available */
6490 V_attachFlags(vp) &= ~(VOL_IN_HASH);
6492 #endif /* AFS_DEMAND_ATTACH_FS */
6496 /* do NOT reset hashid to zero, as the online
6497 * salvager package may need to know the volume id
6498 * after the volume is removed from the hash */
6502 * lookup a volume object in the hash table given a volume id.
6504 * @param[out] ec error code return
6505 * @param[in] volumeId volume id
6506 * @param[in] hint volume object which we believe could be the correct
6509 * @return volume object pointer
6510 * @retval NULL no such volume id is registered with the hash table.
6512 * @pre VOL_LOCK is held. For DAFS, caller must hold a lightweight
6515 * @post volume object with the given id is returned. volume object and
6516 * hash chain access statistics are updated. hash chain may have
6519 * @note For DAFS, VOL_LOCK may be dropped in order to wait for an
6520 * asynchronous hash chain reordering operation to finish, or
6521 * in order for us to perform an asynchronous chain reordering.
6523 * @note Hash chain reorderings occur when the access count for the
6524 * volume object being looked up exceeds the sum of the previous
6525 * node's (the node ahead of it in the hash chain linked list)
6526 * access count plus the constant VOLUME_HASH_REORDER_THRESHOLD.
6528 * @note For DAFS, the hint parameter allows us to short-circuit if the
6529 * cacheCheck fields match between the hash chain head and the
6530 * hint volume object.
6533 VLookupVolume_r(Error * ec, VolId volumeId, Volume * hint)
6535 register int looks = 0;
6536 Volume * vp, *np, *pp;
6537 VolumeHashChainHead * head;
6540 head = &VolumeHashTable.Table[VOLUME_HASH(volumeId)];
6542 #ifdef AFS_DEMAND_ATTACH_FS
6543 /* wait for the hash chain to become available */
6546 /* check to see if we can short circuit without walking the hash chain */
6547 if (hint && (hint->chainCacheCheck == head->cacheCheck)) {
6548 IncUInt64(&hint->stats.hash_short_circuits);
6551 #endif /* AFS_DEMAND_ATTACH_FS */
6553 /* someday we need to either do per-chain locks, RWlocks,
6554 * or both for volhash access.
6555 * (and move to a data structure with better cache locality) */
6557 /* search the chain for this volume id */
6558 for(queue_Scan(head, vp, np, Volume)) {
6560 if ((vp->hashid == volumeId)) {
6565 if (queue_IsEnd(head, vp)) {
6569 #ifdef AFS_DEMAND_ATTACH_FS
6570 /* update hash chain statistics */
6573 FillInt64(lks, 0, looks);
6574 AddUInt64(head->looks, lks, &head->looks);
6575 AddUInt64(VStats.hash_looks, lks, &VStats.hash_looks);
6576 IncUInt64(&head->gets);
6581 IncUInt64(&vp->stats.hash_lookups);
6583 /* for demand attach fileserver, we permit occasional hash chain reordering
6584 * so that frequently looked up volumes move towards the head of the chain */
6585 pp = queue_Prev(vp, Volume);
6586 if (!queue_IsEnd(head, pp)) {
6587 FillInt64(thresh, 0, VOLUME_HASH_REORDER_THRESHOLD);
6588 AddUInt64(thresh, pp->stats.hash_lookups, &thresh);
6589 if (GEInt64(vp->stats.hash_lookups, thresh)) {
6590 VReorderHash_r(head, pp, vp);
6594 /* update the short-circuit cache check */
6595 vp->chainCacheCheck = head->cacheCheck;
6597 #endif /* AFS_DEMAND_ATTACH_FS */
6602 #ifdef AFS_DEMAND_ATTACH_FS
6603 /* perform volume hash chain reordering.
6605 * advance a subchain beginning at vp ahead of
6606 * the adjacent subchain ending at pp */
6608 VReorderHash_r(VolumeHashChainHead * head, Volume * pp, Volume * vp)
6610 Volume *tp, *np, *lp;
6611 afs_uint64 move_thresh;
6613 /* this should never be called if the chain is already busy, so
6614 * no need to wait for other exclusive chain ops to finish */
6616 /* this is a rather heavy set of operations,
6617 * so let's set the chain busy flag and drop
6619 VHashBeginExclusive_r(head);
6622 /* scan forward in the chain from vp looking for the last element
6623 * in the chain we want to advance */
6624 FillInt64(move_thresh, 0, VOLUME_HASH_REORDER_CHAIN_THRESH);
6625 AddUInt64(move_thresh, pp->stats.hash_lookups, &move_thresh);
6626 for(queue_ScanFrom(head, vp, tp, np, Volume)) {
6627 if (LTInt64(tp->stats.hash_lookups, move_thresh)) {
6631 lp = queue_Prev(tp, Volume);
6633 /* scan backwards from pp to determine where to splice and
6634 * insert the subchain we're advancing */
6635 for(queue_ScanBackwardsFrom(head, pp, tp, np, Volume)) {
6636 if (GTInt64(tp->stats.hash_lookups, move_thresh)) {
6640 tp = queue_Next(tp, Volume);
6642 /* rebalance chain(vp,...,lp) ahead of chain(tp,...,pp) */
6643 queue_MoveChainBefore(tp,vp,lp);
6646 IncUInt64(&VStats.hash_reorders);
6648 IncUInt64(&head->reorders);
6650 /* wake up any threads waiting for the hash chain */
6651 VHashEndExclusive_r(head);
6655 /* demand-attach fs volume hash
6656 * asynchronous exclusive operations */
6659 * begin an asynchronous exclusive operation on a volume hash chain.
6661 * @param[in] head pointer to volume hash chain head object
6663 * @pre VOL_LOCK held. hash chain is quiescent.
6665 * @post hash chain marked busy.
6667 * @note this interface is used in conjunction with VHashEndExclusive_r and
6668 * VHashWait_r to perform asynchronous (wrt VOL_LOCK) operations on a
6669 * volume hash chain. Its main use case is hash chain reordering, which
6670 * has the potential to be a highly latent operation.
6672 * @see VHashEndExclusive_r
6677 * @internal volume package internal use only.
6680 VHashBeginExclusive_r(VolumeHashChainHead * head)
6682 assert(head->busy == 0);
6687 * relinquish exclusive ownership of a volume hash chain.
6689 * @param[in] head pointer to volume hash chain head object
6691 * @pre VOL_LOCK held. thread owns the hash chain exclusively.
6693 * @post hash chain is marked quiescent. threads awaiting use of
6694 * chain are awakened.
6696 * @see VHashBeginExclusive_r
6701 * @internal volume package internal use only.
6704 VHashEndExclusive_r(VolumeHashChainHead * head)
6708 assert(pthread_cond_broadcast(&head->chain_busy_cv) == 0);
6712 * wait for all asynchronous operations on a hash chain to complete.
6714 * @param[in] head pointer to volume hash chain head object
6716 * @pre VOL_LOCK held.
6718 * @post hash chain object is quiescent.
6720 * @see VHashBeginExclusive_r
6721 * @see VHashEndExclusive_r
6725 * @note This interface should be called before any attempt to
6726 * traverse the hash chain. It is permissible for a thread
6727 * to gain exclusive access to the chain, and then perform
6728 * latent operations on the chain asynchronously wrt the
6731 * @warning if waiting is necessary, VOL_LOCK is dropped
6733 * @internal volume package internal use only.
6736 VHashWait_r(VolumeHashChainHead * head)
6738 while (head->busy) {
6739 VOL_CV_WAIT(&head->chain_busy_cv);
6742 #endif /* AFS_DEMAND_ATTACH_FS */
6745 /***************************************************/
6746 /* Volume by Partition List routines */
6747 /***************************************************/
6750 * demand attach fileserver adds a
6751 * linked list of volumes to each
6752 * partition object, thus allowing
6753 * for quick enumeration of all
6754 * volumes on a partition
6757 #ifdef AFS_DEMAND_ATTACH_FS
6759 * add a volume to its disk partition VByPList.
6761 * @param[in] vp pointer to volume object
6763 * @pre either the disk partition VByPList is owned exclusively
6764 * by the calling thread, or the list is quiescent and
6767 * @post volume is added to disk partition VByPList
6771 * @warning it is the caller's responsibility to ensure list
6774 * @see VVByPListWait_r
6775 * @see VVByPListBeginExclusive_r
6776 * @see VVByPListEndExclusive_r
6778 * @internal volume package internal use only.
6781 AddVolumeToVByPList_r(Volume * vp)
6783 if (queue_IsNotOnQueue(&vp->vol_list)) {
6784 queue_Append(&vp->partition->vol_list, &vp->vol_list);
6785 V_attachFlags(vp) |= VOL_ON_VBYP_LIST;
6786 vp->partition->vol_list.len++;
6791 * delete a volume from its disk partition VByPList.
6793 * @param[in] vp pointer to volume object
6795 * @pre either the disk partition VByPList is owned exclusively
6796 * by the calling thread, or the list is quiescent and
6799 * @post volume is removed from the disk partition VByPList
6803 * @warning it is the caller's responsibility to ensure list
6806 * @see VVByPListWait_r
6807 * @see VVByPListBeginExclusive_r
6808 * @see VVByPListEndExclusive_r
6810 * @internal volume package internal use only.
6813 DeleteVolumeFromVByPList_r(Volume * vp)
6815 if (queue_IsOnQueue(&vp->vol_list)) {
6816 queue_Remove(&vp->vol_list);
6817 V_attachFlags(vp) &= ~(VOL_ON_VBYP_LIST);
6818 vp->partition->vol_list.len--;
6823 * begin an asynchronous exclusive operation on a VByPList.
6825 * @param[in] dp pointer to disk partition object
6827 * @pre VOL_LOCK held. VByPList is quiescent.
6829 * @post VByPList marked busy.
6831 * @note this interface is used in conjunction with VVByPListEndExclusive_r and
6832 * VVByPListWait_r to perform asynchronous (wrt VOL_LOCK) operations on a
6835 * @see VVByPListEndExclusive_r
6836 * @see VVByPListWait_r
6840 * @internal volume package internal use only.
6842 /* take exclusive control over the list */
6844 VVByPListBeginExclusive_r(struct DiskPartition * dp)
6846 assert(dp->vol_list.busy == 0);
6847 dp->vol_list.busy = 1;
6851 * relinquish exclusive ownership of a VByPList.
6853 * @param[in] dp pointer to disk partition object
6855 * @pre VOL_LOCK held. thread owns the VByPList exclusively.
6857 * @post VByPList is marked quiescent. threads awaiting use of
6858 * the list are awakened.
6860 * @see VVByPListBeginExclusive_r
6861 * @see VVByPListWait_r
6865 * @internal volume package internal use only.
6868 VVByPListEndExclusive_r(struct DiskPartition * dp)
6870 assert(dp->vol_list.busy);
6871 dp->vol_list.busy = 0;
6872 assert(pthread_cond_broadcast(&dp->vol_list.cv) == 0);
6876 * wait for all asynchronous operations on a VByPList to complete.
6878 * @param[in] dp pointer to disk partition object
6880 * @pre VOL_LOCK is held.
6882 * @post disk partition's VByP list is quiescent
6886 * @note This interface should be called before any attempt to
6887 * traverse the VByPList. It is permissible for a thread
6888 * to gain exclusive access to the list, and then perform
6889 * latent operations on the list asynchronously wrt the
6892 * @warning if waiting is necessary, VOL_LOCK is dropped
6894 * @see VVByPListEndExclusive_r
6895 * @see VVByPListBeginExclusive_r
6897 * @internal volume package internal use only.
6900 VVByPListWait_r(struct DiskPartition * dp)
6902 while (dp->vol_list.busy) {
6903 VOL_CV_WAIT(&dp->vol_list.cv);
6906 #endif /* AFS_DEMAND_ATTACH_FS */
6908 /***************************************************/
6909 /* Volume Cache Statistics routines */
6910 /***************************************************/
6913 VPrintCacheStats_r(void)
6915 afs_uint32 get_hi, get_lo, load_hi, load_lo;
6916 register struct VnodeClassInfo *vcp;
6917 vcp = &VnodeClassInfo[vLarge];
6918 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);
6919 vcp = &VnodeClassInfo[vSmall];
6920 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);
6921 SplitInt64(VStats.hdr_gets, get_hi, get_lo);
6922 SplitInt64(VStats.hdr_loads, load_hi, load_lo);
6923 Log("Volume header cache, %d entries, %d gets, %d replacements\n",
6924 VStats.hdr_cache_size, get_lo, load_lo);
6928 VPrintCacheStats(void)
6931 VPrintCacheStats_r();
6935 #ifdef AFS_DEMAND_ATTACH_FS
6937 UInt64ToDouble(afs_uint64 * x)
6939 static double c32 = 4.0 * 1.073741824 * 1000000000.0;
6941 SplitInt64(*x, h, l);
6942 return (((double)h) * c32) + ((double) l);
6946 DoubleToPrintable(double x, char * buf, int len)
6948 static double billion = 1000000000.0;
6951 y[0] = (afs_uint32) (x / (billion * billion));
6952 y[1] = (afs_uint32) ((x - (((double)y[0]) * billion * billion)) / billion);
6953 y[2] = (afs_uint32) (x - ((((double)y[0]) * billion * billion) + (((double)y[1]) * billion)));
6956 snprintf(buf, len, "%d%09d%09d", y[0], y[1], y[2]);
6958 snprintf(buf, len, "%d%09d", y[1], y[2]);
6960 snprintf(buf, len, "%d", y[2]);
6967 VPrintExtendedCacheStats_r(int flags)
6976 struct stats looks, gets, reorders, len;
6977 struct stats ch_looks, ch_gets, ch_reorders;
6979 VolumeHashChainHead *head;
6982 /* zero out stats */
6983 memset(&looks, 0, sizeof(struct stats));
6984 memset(&gets, 0, sizeof(struct stats));
6985 memset(&reorders, 0, sizeof(struct stats));
6986 memset(&len, 0, sizeof(struct stats));
6987 memset(&ch_looks, 0, sizeof(struct stats));
6988 memset(&ch_gets, 0, sizeof(struct stats));
6989 memset(&ch_reorders, 0, sizeof(struct stats));
6991 for (i = 0; i < VolumeHashTable.Size; i++) {
6992 head = &VolumeHashTable.Table[i];
6995 VHashBeginExclusive_r(head);
6998 ch_looks.sum = UInt64ToDouble(&head->looks);
6999 ch_gets.sum = UInt64ToDouble(&head->gets);
7000 ch_reorders.sum = UInt64ToDouble(&head->reorders);
7002 /* update global statistics */
7004 looks.sum += ch_looks.sum;
7005 gets.sum += ch_gets.sum;
7006 reorders.sum += ch_reorders.sum;
7007 len.sum += (double)head->len;
7010 len.min = (double) head->len;
7011 len.max = (double) head->len;
7012 looks.min = ch_looks.sum;
7013 looks.max = ch_looks.sum;
7014 gets.min = ch_gets.sum;
7015 gets.max = ch_gets.sum;
7016 reorders.min = ch_reorders.sum;
7017 reorders.max = ch_reorders.sum;
7019 if (((double)head->len) < len.min)
7020 len.min = (double) head->len;
7021 if (((double)head->len) > len.max)
7022 len.max = (double) head->len;
7023 if (ch_looks.sum < looks.min)
7024 looks.min = ch_looks.sum;
7025 else if (ch_looks.sum > looks.max)
7026 looks.max = ch_looks.sum;
7027 if (ch_gets.sum < gets.min)
7028 gets.min = ch_gets.sum;
7029 else if (ch_gets.sum > gets.max)
7030 gets.max = ch_gets.sum;
7031 if (ch_reorders.sum < reorders.min)
7032 reorders.min = ch_reorders.sum;
7033 else if (ch_reorders.sum > reorders.max)
7034 reorders.max = ch_reorders.sum;
7038 if ((flags & VOL_STATS_PER_CHAIN2) && queue_IsNotEmpty(head)) {
7039 /* compute detailed per-chain stats */
7040 struct stats hdr_loads, hdr_gets;
7041 double v_looks, v_loads, v_gets;
7043 /* initialize stats with data from first element in chain */
7044 vp = queue_First(head, Volume);
7045 v_looks = UInt64ToDouble(&vp->stats.hash_lookups);
7046 v_loads = UInt64ToDouble(&vp->stats.hdr_loads);
7047 v_gets = UInt64ToDouble(&vp->stats.hdr_gets);
7048 ch_gets.min = ch_gets.max = v_looks;
7049 hdr_loads.min = hdr_loads.max = v_loads;
7050 hdr_gets.min = hdr_gets.max = v_gets;
7051 hdr_loads.sum = hdr_gets.sum = 0;
7053 vp = queue_Next(vp, Volume);
7055 /* pull in stats from remaining elements in chain */
7056 for (queue_ScanFrom(head, vp, vp, np, Volume)) {
7057 v_looks = UInt64ToDouble(&vp->stats.hash_lookups);
7058 v_loads = UInt64ToDouble(&vp->stats.hdr_loads);
7059 v_gets = UInt64ToDouble(&vp->stats.hdr_gets);
7061 hdr_loads.sum += v_loads;
7062 hdr_gets.sum += v_gets;
7064 if (v_looks < ch_gets.min)
7065 ch_gets.min = v_looks;
7066 else if (v_looks > ch_gets.max)
7067 ch_gets.max = v_looks;
7069 if (v_loads < hdr_loads.min)
7070 hdr_loads.min = v_loads;
7071 else if (v_loads > hdr_loads.max)
7072 hdr_loads.max = v_loads;
7074 if (v_gets < hdr_gets.min)
7075 hdr_gets.min = v_gets;
7076 else if (v_gets > hdr_gets.max)
7077 hdr_gets.max = v_gets;
7080 /* compute per-chain averages */
7081 ch_gets.avg = ch_gets.sum / ((double)head->len);
7082 hdr_loads.avg = hdr_loads.sum / ((double)head->len);
7083 hdr_gets.avg = hdr_gets.sum / ((double)head->len);
7085 /* dump per-chain stats */
7086 Log("Volume hash chain %d : len=%d, looks=%s, reorders=%s\n",
7088 DoubleToPrintable(ch_looks.sum, pr_buf[0], sizeof(pr_buf[0])),
7089 DoubleToPrintable(ch_reorders.sum, pr_buf[1], sizeof(pr_buf[1])));
7090 Log("\tVolume gets : min=%s, max=%s, avg=%s, total=%s\n",
7091 DoubleToPrintable(ch_gets.min, pr_buf[0], sizeof(pr_buf[0])),
7092 DoubleToPrintable(ch_gets.max, pr_buf[1], sizeof(pr_buf[1])),
7093 DoubleToPrintable(ch_gets.avg, pr_buf[2], sizeof(pr_buf[2])),
7094 DoubleToPrintable(ch_gets.sum, pr_buf[3], sizeof(pr_buf[3])));
7095 Log("\tHDR gets : min=%s, max=%s, avg=%s, total=%s\n",
7096 DoubleToPrintable(hdr_gets.min, pr_buf[0], sizeof(pr_buf[0])),
7097 DoubleToPrintable(hdr_gets.max, pr_buf[1], sizeof(pr_buf[1])),
7098 DoubleToPrintable(hdr_gets.avg, pr_buf[2], sizeof(pr_buf[2])),
7099 DoubleToPrintable(hdr_gets.sum, pr_buf[3], sizeof(pr_buf[3])));
7100 Log("\tHDR loads : min=%s, max=%s, avg=%s, total=%s\n",
7101 DoubleToPrintable(hdr_loads.min, pr_buf[0], sizeof(pr_buf[0])),
7102 DoubleToPrintable(hdr_loads.max, pr_buf[1], sizeof(pr_buf[1])),
7103 DoubleToPrintable(hdr_loads.avg, pr_buf[2], sizeof(pr_buf[2])),
7104 DoubleToPrintable(hdr_loads.sum, pr_buf[3], sizeof(pr_buf[3])));
7105 } else if (flags & VOL_STATS_PER_CHAIN) {
7106 /* dump simple per-chain stats */
7107 Log("Volume hash chain %d : len=%d, looks=%s, gets=%s, reorders=%s\n",
7109 DoubleToPrintable(ch_looks.sum, pr_buf[0], sizeof(pr_buf[0])),
7110 DoubleToPrintable(ch_gets.sum, pr_buf[1], sizeof(pr_buf[1])),
7111 DoubleToPrintable(ch_reorders.sum, pr_buf[2], sizeof(pr_buf[2])));
7115 VHashEndExclusive_r(head);
7120 /* compute global averages */
7121 len.avg = len.sum / ((double)VolumeHashTable.Size);
7122 looks.avg = looks.sum / ((double)VolumeHashTable.Size);
7123 gets.avg = gets.sum / ((double)VolumeHashTable.Size);
7124 reorders.avg = reorders.sum / ((double)VolumeHashTable.Size);
7126 /* dump global stats */
7127 Log("Volume hash summary: %d buckets\n", VolumeHashTable.Size);
7128 Log(" chain length : min=%s, max=%s, avg=%s, total=%s\n",
7129 DoubleToPrintable(len.min, pr_buf[0], sizeof(pr_buf[0])),
7130 DoubleToPrintable(len.max, pr_buf[1], sizeof(pr_buf[1])),
7131 DoubleToPrintable(len.avg, pr_buf[2], sizeof(pr_buf[2])),
7132 DoubleToPrintable(len.sum, pr_buf[3], sizeof(pr_buf[3])));
7133 Log(" looks : min=%s, max=%s, avg=%s, total=%s\n",
7134 DoubleToPrintable(looks.min, pr_buf[0], sizeof(pr_buf[0])),
7135 DoubleToPrintable(looks.max, pr_buf[1], sizeof(pr_buf[1])),
7136 DoubleToPrintable(looks.avg, pr_buf[2], sizeof(pr_buf[2])),
7137 DoubleToPrintable(looks.sum, pr_buf[3], sizeof(pr_buf[3])));
7138 Log(" gets : min=%s, max=%s, avg=%s, total=%s\n",
7139 DoubleToPrintable(gets.min, pr_buf[0], sizeof(pr_buf[0])),
7140 DoubleToPrintable(gets.max, pr_buf[1], sizeof(pr_buf[1])),
7141 DoubleToPrintable(gets.avg, pr_buf[2], sizeof(pr_buf[2])),
7142 DoubleToPrintable(gets.sum, pr_buf[3], sizeof(pr_buf[3])));
7143 Log(" reorders : min=%s, max=%s, avg=%s, total=%s\n",
7144 DoubleToPrintable(reorders.min, pr_buf[0], sizeof(pr_buf[0])),
7145 DoubleToPrintable(reorders.max, pr_buf[1], sizeof(pr_buf[1])),
7146 DoubleToPrintable(reorders.avg, pr_buf[2], sizeof(pr_buf[2])),
7147 DoubleToPrintable(reorders.sum, pr_buf[3], sizeof(pr_buf[3])));
7149 /* print extended disk related statistics */
7151 struct DiskPartition * diskP;
7152 afs_uint32 vol_count[VOLMAXPARTS+1];
7153 byte part_exists[VOLMAXPARTS+1];
7157 memset(vol_count, 0, sizeof(vol_count));
7158 memset(part_exists, 0, sizeof(part_exists));
7162 for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
7164 vol_count[id] = diskP->vol_list.len;
7165 part_exists[id] = 1;
7169 for (i = 0; i <= VOLMAXPARTS; i++) {
7170 if (part_exists[i]) {
7171 diskP = VGetPartitionById_r(i, 0);
7173 Log("Partition %s has %d online volumes\n",
7174 VPartitionPath(diskP), diskP->vol_list.len);
7184 VPrintExtendedCacheStats(int flags)
7187 VPrintExtendedCacheStats_r(flags);
7190 #endif /* AFS_DEMAND_ATTACH_FS */