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>
26 #include <afs/afsint.h>
29 #include <sys/param.h>
30 #if !defined(AFS_SGI_ENV)
33 #else /* AFS_OSF_ENV */
34 #ifdef AFS_VFSINCL_ENV
37 #include <sys/fs/ufs_fs.h>
39 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
40 #include <ufs/ufs/dinode.h>
41 #include <ufs/ffs/fs.h>
46 #else /* AFS_VFSINCL_ENV */
47 #if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_XBSD_ENV)
50 #endif /* AFS_VFSINCL_ENV */
51 #endif /* AFS_OSF_ENV */
52 #endif /* AFS_SGI_ENV */
53 #endif /* AFS_NT40_ENV */
71 #if defined(AFS_SUN_ENV) || defined(AFS_SUN5_ENV)
73 #include <sys/mnttab.h>
74 #include <sys/mntent.h>
80 #if defined(AFS_SGI_ENV)
85 #ifndef AFS_LINUX20_ENV
86 #include <fstab.h> /* Need to find in libc 5, present in libc 6 */
89 #endif /* AFS_SGI_ENV */
91 #endif /* AFS_HPUX_ENV */
95 #include <netinet/in.h>
100 #endif /* ITIMER_REAL */
101 #endif /* AFS_NT40_ENV */
102 #if defined(AFS_SUN5_ENV) || defined(AFS_NT40_ENV) || defined(AFS_LINUX20_ENV)
109 #include <afs/errors.h>
112 #include <afs/afssyscalls.h>
114 #include <afs/afsutil.h>
118 #include "daemon_com.h"
120 #include "salvsync.h"
123 #include "partition.h"
124 #include "volume_inline.h"
125 #ifdef AFS_PTHREAD_ENV
127 #else /* AFS_PTHREAD_ENV */
128 #include "afs/assert.h"
129 #endif /* AFS_PTHREAD_ENV */
136 #if !defined(offsetof)
141 #define afs_stat stat64
142 #define afs_fstat fstat64
143 #define afs_open open64
144 #else /* !O_LARGEFILE */
145 #define afs_stat stat
146 #define afs_fstat fstat
147 #define afs_open open
148 #endif /* !O_LARGEFILE */
150 #ifdef AFS_PTHREAD_ENV
151 pthread_mutex_t vol_glock_mutex;
152 pthread_mutex_t vol_trans_mutex;
153 pthread_cond_t vol_put_volume_cond;
154 pthread_cond_t vol_sleep_cond;
155 int vol_attach_threads = 1;
156 #endif /* AFS_PTHREAD_ENV */
158 #ifdef AFS_DEMAND_ATTACH_FS
159 pthread_mutex_t vol_salvsync_mutex;
160 #endif /* AFS_DEMAND_ATTACH_FS */
163 extern void *calloc(), *realloc();
166 /*@printflike@*/ extern void Log(const char *format, ...);
168 /* Forward declarations */
169 static Volume *attach2(Error * ec, VolId vid, char *path,
170 register struct VolumeHeader *header,
171 struct DiskPartition64 *partp, Volume * vp,
172 int isbusy, int mode);
173 static void ReallyFreeVolume(Volume * vp);
174 #ifdef AFS_DEMAND_ATTACH_FS
175 static void FreeVolume(Volume * vp);
176 #else /* !AFS_DEMAND_ATTACH_FS */
177 #define FreeVolume(vp) ReallyFreeVolume(vp)
178 static void VScanUpdateList(void);
179 #endif /* !AFS_DEMAND_ATTACH_FS */
180 static void VInitVolumeHeaderCache(afs_uint32 howMany);
181 static int GetVolumeHeader(register Volume * vp);
182 static void ReleaseVolumeHeader(register struct volHeader *hd);
183 static void FreeVolumeHeader(register Volume * vp);
184 static void AddVolumeToHashTable(register Volume * vp, int hashid);
185 static void DeleteVolumeFromHashTable(register Volume * vp);
187 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 VReleaseVolumeHandles_r(Volume * vp);
192 static void VCloseVolumeHandles_r(Volume * vp);
193 static void LoadVolumeHeader(Error * ec, Volume * vp);
194 static int VCheckOffline(register Volume * vp);
195 static int VCheckDetach(register Volume * vp);
196 static Volume * GetVolume(Error * ec, Error * client_ec, VolId volumeId, Volume * hint, int flags);
197 #ifdef AFS_DEMAND_ATTACH_FS
198 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 DiskPartition64 * 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 DiskPartition64 *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 DiskPartition64 * dp);
349 static void VVByPListEndExclusive_r(struct DiskPartition64 * dp);
350 static void VVByPListWait_r(struct DiskPartition64 * 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);
357 /* Volume hash table */
358 static void VReorderHash_r(VolumeHashChainHead * head, Volume * pp, Volume * vp);
359 static void VHashBeginExclusive_r(VolumeHashChainHead * head);
360 static void VHashEndExclusive_r(VolumeHashChainHead * head);
361 static void VHashWait_r(VolumeHashChainHead * head);
364 static int ShutdownVByPForPass_r(struct DiskPartition64 * dp, int pass);
365 static int ShutdownVolumeWalk_r(struct DiskPartition64 * dp, int pass,
366 struct rx_queue ** idx);
367 static void ShutdownController(vshutdown_thread_t * params);
368 static void ShutdownCreateSchedule(vshutdown_thread_t * params);
371 static void VLRU_ComputeConstants(void);
372 static void VInitVLRU(void);
373 static void VLRU_Init_Node_r(volatile Volume * vp);
374 static void VLRU_Add_r(volatile Volume * vp);
375 static void VLRU_Delete_r(volatile Volume * vp);
376 static void VLRU_UpdateAccess_r(volatile Volume * vp);
377 static void * VLRU_ScannerThread(void * args);
378 static void VLRU_Scan_r(int idx);
379 static void VLRU_Promote_r(int idx);
380 static void VLRU_Demote_r(int idx);
381 static void VLRU_SwitchQueues(volatile Volume * vp, int new_idx, int append);
384 static int VCheckSoftDetach(volatile Volume * vp, afs_uint32 thresh);
385 static int VCheckSoftDetachCandidate(volatile Volume * vp, afs_uint32 thresh);
386 static int VSoftDetachVolume_r(volatile Volume * vp, afs_uint32 thresh);
389 pthread_key_t VThread_key;
390 VThreadOptions_t VThread_defaults = {
391 0 /**< allow salvsync */
393 #endif /* AFS_DEMAND_ATTACH_FS */
396 struct Lock vol_listLock; /* Lock obtained when listing volumes:
397 * prevents a volume from being missed
398 * if the volume is attached during a
402 static int TimeZoneCorrection; /* Number of seconds west of GMT */
404 /* Common message used when the volume goes off line */
405 char *VSalvageMessage =
406 "Files in this volume are currently unavailable; call operations";
408 int VInit; /* 0 - uninitialized,
409 * 1 - initialized but not all volumes have been attached,
410 * 2 - initialized and all volumes have been attached,
411 * 3 - initialized, all volumes have been attached, and
412 * VConnectFS() has completed. */
415 bit32 VolumeCacheCheck; /* Incremented everytime a volume goes on line--
416 * used to stamp volume headers and in-core
417 * vnodes. When the volume goes on-line the
418 * vnode will be invalidated
419 * access only with VOL_LOCK held */
424 /***************************************************/
425 /* Startup routines */
426 /***************************************************/
429 VInitVolumePackage(ProgramType pt, afs_uint32 nLargeVnodes, afs_uint32 nSmallVnodes,
430 int connect, afs_uint32 volcache)
432 int errors = 0; /* Number of errors while finding vice partitions. */
438 memset(&VStats, 0, sizeof(VStats));
439 VStats.hdr_cache_size = 200;
441 VInitPartitionPackage();
443 #ifdef AFS_DEMAND_ATTACH_FS
444 if (programType == fileServer) {
447 VLRU_SetOptions(VLRU_SET_ENABLED, 0);
449 assert(pthread_key_create(&VThread_key, NULL) == 0);
452 #ifdef AFS_PTHREAD_ENV
453 assert(pthread_mutex_init(&vol_glock_mutex, NULL) == 0);
454 assert(pthread_mutex_init(&vol_trans_mutex, NULL) == 0);
455 assert(pthread_cond_init(&vol_put_volume_cond, NULL) == 0);
456 assert(pthread_cond_init(&vol_sleep_cond, NULL) == 0);
457 #else /* AFS_PTHREAD_ENV */
459 #endif /* AFS_PTHREAD_ENV */
460 Lock_Init(&vol_listLock);
462 srandom(time(0)); /* For VGetVolumeInfo */
463 gettimeofday(&tv, &tz);
464 TimeZoneCorrection = tz.tz_minuteswest * 60;
466 #ifdef AFS_DEMAND_ATTACH_FS
467 assert(pthread_mutex_init(&vol_salvsync_mutex, NULL) == 0);
468 #endif /* AFS_DEMAND_ATTACH_FS */
470 /* Ok, we have done enough initialization that fileserver can
471 * start accepting calls, even though the volumes may not be
472 * available just yet.
476 #if defined(AFS_DEMAND_ATTACH_FS) && defined(SALVSYNC_BUILD_SERVER)
477 if (programType == salvageServer) {
480 #endif /* AFS_DEMAND_ATTACH_FS */
481 #ifdef FSSYNC_BUILD_SERVER
482 if (programType == fileServer) {
486 #if defined(AFS_DEMAND_ATTACH_FS) && defined(SALVSYNC_BUILD_CLIENT)
487 if (programType == fileServer) {
488 /* establish a connection to the salvager at this point */
489 assert(VConnectSALV() != 0);
491 #endif /* AFS_DEMAND_ATTACH_FS */
493 if (volcache > VStats.hdr_cache_size)
494 VStats.hdr_cache_size = volcache;
495 VInitVolumeHeaderCache(VStats.hdr_cache_size);
497 VInitVnodes(vLarge, nLargeVnodes);
498 VInitVnodes(vSmall, nSmallVnodes);
501 errors = VAttachPartitions();
505 if (programType == fileServer) {
506 struct DiskPartition64 *diskP;
507 #ifdef AFS_PTHREAD_ENV
508 struct vinitvolumepackage_thread_t params;
509 struct diskpartition_queue_t * dpq;
510 int i, threads, parts;
512 pthread_attr_t attrs;
514 assert(pthread_cond_init(¶ms.thread_done_cv,NULL) == 0);
516 params.n_threads_complete = 0;
518 /* create partition work queue */
519 for (parts=0, diskP = DiskPartitionList; diskP; diskP = diskP->next, parts++) {
520 dpq = (diskpartition_queue_t *) malloc(sizeof(struct diskpartition_queue_t));
523 queue_Append(¶ms,dpq);
526 threads = MIN(parts, vol_attach_threads);
529 /* spawn off a bunch of initialization threads */
530 assert(pthread_attr_init(&attrs) == 0);
531 assert(pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED) == 0);
533 Log("VInitVolumePackage: beginning parallel fileserver startup\n");
534 #ifdef AFS_DEMAND_ATTACH_FS
535 Log("VInitVolumePackage: using %d threads to pre-attach volumes on %d partitions\n",
537 #else /* AFS_DEMAND_ATTACH_FS */
538 Log("VInitVolumePackage: using %d threads to attach volumes on %d partitions\n",
540 #endif /* AFS_DEMAND_ATTACH_FS */
543 for (i=0; i < threads; i++) {
544 assert(pthread_create
545 (&tid, &attrs, &VInitVolumePackageThread,
549 while(params.n_threads_complete < threads) {
550 VOL_CV_WAIT(¶ms.thread_done_cv);
554 assert(pthread_attr_destroy(&attrs) == 0);
556 /* if we're only going to run one init thread, don't bother creating
558 Log("VInitVolumePackage: beginning single-threaded fileserver startup\n");
559 #ifdef AFS_DEMAND_ATTACH_FS
560 Log("VInitVolumePackage: using 1 thread to pre-attach volumes on %d partition(s)\n",
562 #else /* AFS_DEMAND_ATTACH_FS */
563 Log("VInitVolumePackage: using 1 thread to attach volumes on %d partition(s)\n",
565 #endif /* AFS_DEMAND_ATTACH_FS */
567 VInitVolumePackageThread(¶ms);
570 assert(pthread_cond_destroy(¶ms.thread_done_cv) == 0);
572 #else /* AFS_PTHREAD_ENV */
574 /* Attach all the volumes in this partition */
575 for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
576 int nAttached = 0, nUnattached = 0;
577 assert(VAttachVolumesByPartition(diskP, &nAttached, &nUnattached) == 0);
579 #endif /* AFS_PTHREAD_ENV */
582 VInit = 2; /* Initialized, and all volumes have been attached */
583 #ifdef FSSYNC_BUILD_CLIENT
584 if (programType == volumeUtility && connect) {
586 Log("Unable to connect to file server; will retry at need\n");
590 #ifdef AFS_DEMAND_ATTACH_FS
591 else if (programType == salvageServer) {
593 Log("Unable to connect to file server; aborted\n");
597 #endif /* AFS_DEMAND_ATTACH_FS */
598 #endif /* FSSYNC_BUILD_CLIENT */
602 #ifdef AFS_PTHREAD_ENV
604 VInitVolumePackageThread(void * args) {
606 struct DiskPartition64 *diskP;
607 struct vinitvolumepackage_thread_t * params;
608 struct diskpartition_queue_t * dpq;
610 params = (vinitvolumepackage_thread_t *) args;
614 /* Attach all the volumes in this partition */
615 while (queue_IsNotEmpty(params)) {
616 int nAttached = 0, nUnattached = 0;
618 dpq = queue_First(params,diskpartition_queue_t);
624 assert(VAttachVolumesByPartition(diskP, &nAttached, &nUnattached) == 0);
629 params->n_threads_complete++;
630 pthread_cond_signal(¶ms->thread_done_cv);
634 #endif /* AFS_PTHREAD_ENV */
637 * attach all volumes on a given disk partition
640 VAttachVolumesByPartition(struct DiskPartition64 *diskP, int * nAttached, int * nUnattached)
646 Log("Partition %s: attaching volumes\n", diskP->name);
647 dirp = opendir(VPartitionPath(diskP));
649 Log("opendir on Partition %s failed!\n", diskP->name);
653 while ((dp = readdir(dirp))) {
655 p = strrchr(dp->d_name, '.');
656 if (p != NULL && strcmp(p, VHDREXT) == 0) {
659 #ifdef AFS_DEMAND_ATTACH_FS
660 vp = VPreAttachVolumeByName(&error, diskP->name, dp->d_name);
661 #else /* AFS_DEMAND_ATTACH_FS */
662 vp = VAttachVolumeByName(&error, diskP->name, dp->d_name,
664 #endif /* AFS_DEMAND_ATTACH_FS */
665 (*(vp ? nAttached : nUnattached))++;
666 if (error == VOFFLINE)
667 Log("Volume %d stays offline (/vice/offline/%s exists)\n", VolumeNumber(dp->d_name), dp->d_name);
668 else if (LogLevel >= 5) {
669 Log("Partition %s: attached volume %d (%s)\n",
670 diskP->name, VolumeNumber(dp->d_name),
673 #if !defined(AFS_DEMAND_ATTACH_FS)
677 #endif /* AFS_DEMAND_ATTACH_FS */
681 Log("Partition %s: attached %d volumes; %d volumes not attached\n", diskP->name, *nAttached, *nUnattached);
687 /***************************************************/
688 /* Shutdown routines */
689 /***************************************************/
693 * highly multithreaded volume package shutdown
695 * with the demand attach fileserver extensions,
696 * VShutdown has been modified to be multithreaded.
697 * In order to achieve optimal use of many threads,
698 * the shutdown code involves one control thread and
699 * n shutdown worker threads. The control thread
700 * periodically examines the number of volumes available
701 * for shutdown on each partition, and produces a worker
702 * thread allocation schedule. The idea is to eliminate
703 * redundant scheduling computation on the workers by
704 * having a single master scheduler.
706 * The scheduler's objectives are:
708 * each partition with volumes remaining gets allocated
709 * at least 1 thread (assuming sufficient threads)
711 * threads are allocated proportional to the number of
712 * volumes remaining to be offlined. This ensures that
713 * the OS I/O scheduler has many requests to elevator
714 * seek on partitions that will (presumably) take the
715 * longest amount of time (from now) to finish shutdown
716 * (3) keep threads busy
717 * when there are extra threads, they are assigned to
718 * partitions using a simple round-robin algorithm
720 * In the future, we may wish to add the ability to adapt
721 * to the relative performance patterns of each disk
726 * multi-step shutdown process
728 * demand attach shutdown is a four-step process. Each
729 * shutdown "pass" shuts down increasingly more difficult
730 * volumes. The main purpose is to achieve better cache
731 * utilization during shutdown.
734 * shutdown volumes in the unattached, pre-attached
737 * shutdown attached volumes with cached volume headers
739 * shutdown all volumes in non-exclusive states
741 * shutdown all remaining volumes
748 register Volume *vp, *np;
749 register afs_int32 code;
750 #ifdef AFS_DEMAND_ATTACH_FS
751 struct DiskPartition64 * diskP;
752 struct diskpartition_queue_t * dpq;
753 vshutdown_thread_t params;
755 pthread_attr_t attrs;
757 memset(¶ms, 0, sizeof(vshutdown_thread_t));
759 for (params.n_parts=0, diskP = DiskPartitionList;
760 diskP; diskP = diskP->next, params.n_parts++);
762 Log("VShutdown: shutting down on-line volumes on %d partition%s...\n",
763 params.n_parts, params.n_parts > 1 ? "s" : "");
765 if (vol_attach_threads > 1) {
766 /* prepare for parallel shutdown */
767 params.n_threads = vol_attach_threads;
768 assert(pthread_mutex_init(¶ms.lock, NULL) == 0);
769 assert(pthread_cond_init(¶ms.cv, NULL) == 0);
770 assert(pthread_cond_init(¶ms.master_cv, NULL) == 0);
771 assert(pthread_attr_init(&attrs) == 0);
772 assert(pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED) == 0);
775 /* setup the basic partition information structures for
776 * parallel shutdown */
777 for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
779 struct rx_queue * qp, * nqp;
783 VVByPListWait_r(diskP);
784 VVByPListBeginExclusive_r(diskP);
787 for (queue_Scan(&diskP->vol_list, qp, nqp, rx_queue)) {
788 vp = (Volume *)((char *)qp - offsetof(Volume, vol_list));
792 Log("VShutdown: partition %s has %d volumes with attached headers\n",
793 VPartitionPath(diskP), count);
796 /* build up the pass 0 shutdown work queue */
797 dpq = (struct diskpartition_queue_t *) malloc(sizeof(struct diskpartition_queue_t));
800 queue_Prepend(¶ms, dpq);
802 params.part_pass_head[diskP->index] = queue_First(&diskP->vol_list, rx_queue);
805 Log("VShutdown: beginning parallel fileserver shutdown\n");
806 Log("VShutdown: using %d threads to offline volumes on %d partition%s\n",
807 vol_attach_threads, params.n_parts, params.n_parts > 1 ? "s" : "" );
809 /* do pass 0 shutdown */
810 assert(pthread_mutex_lock(¶ms.lock) == 0);
811 for (i=0; i < params.n_threads; i++) {
812 assert(pthread_create
813 (&tid, &attrs, &VShutdownThread,
817 /* wait for all the pass 0 shutdowns to complete */
818 while (params.n_threads_complete < params.n_threads) {
819 assert(pthread_cond_wait(¶ms.master_cv, ¶ms.lock) == 0);
821 params.n_threads_complete = 0;
823 assert(pthread_cond_broadcast(¶ms.cv) == 0);
824 assert(pthread_mutex_unlock(¶ms.lock) == 0);
826 Log("VShutdown: pass 0 completed using the 1 thread per partition algorithm\n");
827 Log("VShutdown: starting passes 1 through 3 using finely-granular mp-fast algorithm\n");
829 /* run the parallel shutdown scheduler. it will drop the glock internally */
830 ShutdownController(¶ms);
832 /* wait for all the workers to finish pass 3 and terminate */
833 while (params.pass < 4) {
834 VOL_CV_WAIT(¶ms.cv);
837 assert(pthread_attr_destroy(&attrs) == 0);
838 assert(pthread_cond_destroy(¶ms.cv) == 0);
839 assert(pthread_cond_destroy(¶ms.master_cv) == 0);
840 assert(pthread_mutex_destroy(¶ms.lock) == 0);
842 /* drop the VByPList exclusive reservations */
843 for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
844 VVByPListEndExclusive_r(diskP);
845 Log("VShutdown: %s stats : (pass[0]=%d, pass[1]=%d, pass[2]=%d, pass[3]=%d)\n",
846 VPartitionPath(diskP),
847 params.stats[0][diskP->index],
848 params.stats[1][diskP->index],
849 params.stats[2][diskP->index],
850 params.stats[3][diskP->index]);
853 Log("VShutdown: shutdown finished using %d threads\n", params.n_threads);
855 /* if we're only going to run one shutdown thread, don't bother creating
857 Log("VShutdown: beginning single-threaded fileserver shutdown\n");
859 for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
860 VShutdownByPartition_r(diskP);
864 Log("VShutdown: complete.\n");
865 #else /* AFS_DEMAND_ATTACH_FS */
866 Log("VShutdown: shutting down on-line volumes...\n");
867 for (i = 0; i < VolumeHashTable.Size; i++) {
868 /* try to hold first volume in the hash table */
869 for (queue_Scan(&VolumeHashTable.Table[i],vp,np,Volume)) {
873 Log("VShutdown: Attempting to take volume %u offline.\n",
876 /* next, take the volume offline (drops reference count) */
877 VOffline_r(vp, "File server was shut down");
881 Log("VShutdown: complete.\n");
882 #endif /* AFS_DEMAND_ATTACH_FS */
893 #ifdef AFS_DEMAND_ATTACH_FS
896 * shutdown control thread
899 ShutdownController(vshutdown_thread_t * params)
902 struct DiskPartition64 * diskP;
904 vshutdown_thread_t shadow;
906 ShutdownCreateSchedule(params);
908 while ((params->pass < 4) &&
909 (params->n_threads_complete < params->n_threads)) {
910 /* recompute schedule once per second */
912 memcpy(&shadow, params, sizeof(vshutdown_thread_t));
916 Log("ShutdownController: schedule version=%d, vol_remaining=%d, pass=%d\n",
917 shadow.schedule_version, shadow.vol_remaining, shadow.pass);
918 Log("ShutdownController: n_threads_complete=%d, n_parts_done_pass=%d\n",
919 shadow.n_threads_complete, shadow.n_parts_done_pass);
920 for (diskP = DiskPartitionList; diskP; diskP=diskP->next) {
922 Log("ShutdownController: part[%d] : (len=%d, thread_target=%d, done_pass=%d, pass_head=%p)\n",
925 shadow.part_thread_target[id],
926 shadow.part_done_pass[id],
927 shadow.part_pass_head[id]);
933 ShutdownCreateSchedule(params);
937 /* create the shutdown thread work schedule.
938 * this scheduler tries to implement fairness
939 * by allocating at least 1 thread to each
940 * partition with volumes to be shutdown,
941 * and then it attempts to allocate remaining
942 * threads based upon the amount of work left
945 ShutdownCreateSchedule(vshutdown_thread_t * params)
947 struct DiskPartition64 * diskP;
948 int sum, thr_workload, thr_left;
949 int part_residue[VOLMAXPARTS+1];
952 /* compute the total number of outstanding volumes */
954 for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
955 sum += diskP->vol_list.len;
958 params->schedule_version++;
959 params->vol_remaining = sum;
964 /* compute average per-thread workload */
965 thr_workload = sum / params->n_threads;
966 if (sum % params->n_threads)
969 thr_left = params->n_threads;
970 memset(&part_residue, 0, sizeof(part_residue));
972 /* for fairness, give every partition with volumes remaining
973 * at least one thread */
974 for (diskP = DiskPartitionList; diskP && thr_left; diskP = diskP->next) {
976 if (diskP->vol_list.len) {
977 params->part_thread_target[id] = 1;
980 params->part_thread_target[id] = 0;
984 if (thr_left && thr_workload) {
985 /* compute length-weighted workloads */
988 for (diskP = DiskPartitionList; diskP && thr_left; diskP = diskP->next) {
990 delta = (diskP->vol_list.len / thr_workload) -
991 params->part_thread_target[id];
995 if (delta < thr_left) {
996 params->part_thread_target[id] += delta;
999 params->part_thread_target[id] += thr_left;
1007 /* try to assign any leftover threads to partitions that
1008 * had volume lengths closer to needing thread_target+1 */
1009 int max_residue, max_id;
1011 /* compute the residues */
1012 for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
1014 part_residue[id] = diskP->vol_list.len -
1015 (params->part_thread_target[id] * thr_workload);
1018 /* now try to allocate remaining threads to partitions with the
1019 * highest residues */
1022 for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
1024 if (part_residue[id] > max_residue) {
1025 max_residue = part_residue[id];
1034 params->part_thread_target[max_id]++;
1036 part_residue[max_id] = 0;
1041 /* punt and give any remaining threads equally to each partition */
1043 if (thr_left >= params->n_parts) {
1044 alloc = thr_left / params->n_parts;
1045 for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
1047 params->part_thread_target[id] += alloc;
1052 /* finish off the last of the threads */
1053 for (diskP = DiskPartitionList; thr_left && diskP; diskP = diskP->next) {
1055 params->part_thread_target[id]++;
1061 /* worker thread for parallel shutdown */
1063 VShutdownThread(void * args)
1065 struct rx_queue *qp;
1067 vshutdown_thread_t * params;
1068 int part, code, found, pass, schedule_version_save, count;
1069 struct DiskPartition64 *diskP;
1070 struct diskpartition_queue_t * dpq;
1073 params = (vshutdown_thread_t *) args;
1075 /* acquire the shutdown pass 0 lock */
1076 assert(pthread_mutex_lock(¶ms->lock) == 0);
1078 /* if there's still pass 0 work to be done,
1079 * get a work entry, and do a pass 0 shutdown */
1080 if (queue_IsNotEmpty(params)) {
1081 dpq = queue_First(params, diskpartition_queue_t);
1083 assert(pthread_mutex_unlock(¶ms->lock) == 0);
1089 while (ShutdownVolumeWalk_r(diskP, 0, ¶ms->part_pass_head[id]))
1091 params->stats[0][diskP->index] = count;
1092 assert(pthread_mutex_lock(¶ms->lock) == 0);
1095 params->n_threads_complete++;
1096 if (params->n_threads_complete == params->n_threads) {
1097 /* notify control thread that all workers have completed pass 0 */
1098 assert(pthread_cond_signal(¶ms->master_cv) == 0);
1100 while (params->pass == 0) {
1101 assert(pthread_cond_wait(¶ms->cv, ¶ms->lock) == 0);
1105 assert(pthread_mutex_unlock(¶ms->lock) == 0);
1108 pass = params->pass;
1111 /* now escalate through the more complicated shutdowns */
1113 schedule_version_save = params->schedule_version;
1115 /* find a disk partition to work on */
1116 for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
1118 if (params->part_thread_target[id] && !params->part_done_pass[id]) {
1119 params->part_thread_target[id]--;
1126 /* hmm. for some reason the controller thread couldn't find anything for
1127 * us to do. let's see if there's anything we can do */
1128 for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
1130 if (diskP->vol_list.len && !params->part_done_pass[id]) {
1133 } else if (!params->part_done_pass[id]) {
1134 params->part_done_pass[id] = 1;
1135 params->n_parts_done_pass++;
1137 Log("VShutdown: done shutting down volumes on partition %s.\n",
1138 VPartitionPath(diskP));
1144 /* do work on this partition until either the controller
1145 * creates a new schedule, or we run out of things to do
1146 * on this partition */
1149 while (!params->part_done_pass[id] &&
1150 (schedule_version_save == params->schedule_version)) {
1151 /* ShutdownVolumeWalk_r will drop the glock internally */
1152 if (!ShutdownVolumeWalk_r(diskP, pass, ¶ms->part_pass_head[id])) {
1153 if (!params->part_done_pass[id]) {
1154 params->part_done_pass[id] = 1;
1155 params->n_parts_done_pass++;
1157 Log("VShutdown: done shutting down volumes on partition %s.\n",
1158 VPartitionPath(diskP));
1166 params->stats[pass][id] += count;
1168 /* ok, everyone is done this pass, proceed */
1171 params->n_threads_complete++;
1172 while (params->pass == pass) {
1173 if (params->n_threads_complete == params->n_threads) {
1174 /* we are the last thread to complete, so we will
1175 * reinitialize worker pool state for the next pass */
1176 params->n_threads_complete = 0;
1177 params->n_parts_done_pass = 0;
1179 for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
1181 params->part_done_pass[id] = 0;
1182 params->part_pass_head[id] = queue_First(&diskP->vol_list, rx_queue);
1185 /* compute a new thread schedule before releasing all the workers */
1186 ShutdownCreateSchedule(params);
1188 /* wake up all the workers */
1189 assert(pthread_cond_broadcast(¶ms->cv) == 0);
1192 Log("VShutdown: pass %d completed using %d threads on %d partitions\n",
1193 pass, params->n_threads, params->n_parts);
1196 VOL_CV_WAIT(¶ms->cv);
1199 pass = params->pass;
1213 /* shut down all volumes on a given disk partition
1215 * note that this function will not allow mp-fast
1216 * shutdown of a partition */
1218 VShutdownByPartition_r(struct DiskPartition64 * dp)
1224 /* wait for other exclusive ops to finish */
1225 VVByPListWait_r(dp);
1227 /* begin exclusive access */
1228 VVByPListBeginExclusive_r(dp);
1230 /* pick the low-hanging fruit first,
1231 * then do the complicated ones last
1232 * (has the advantage of keeping
1233 * in-use volumes up until the bitter end) */
1234 for (pass = 0, total=0; pass < 4; pass++) {
1235 pass_stats[pass] = ShutdownVByPForPass_r(dp, pass);
1236 total += pass_stats[pass];
1239 /* end exclusive access */
1240 VVByPListEndExclusive_r(dp);
1242 Log("VShutdownByPartition: shut down %d volumes on %s (pass[0]=%d, pass[1]=%d, pass[2]=%d, pass[3]=%d)\n",
1243 total, VPartitionPath(dp), pass_stats[0], pass_stats[1], pass_stats[2], pass_stats[3]);
1248 /* internal shutdown functionality
1250 * for multi-pass shutdown:
1251 * 0 to only "shutdown" {pre,un}attached and error state volumes
1252 * 1 to also shutdown attached volumes w/ volume header loaded
1253 * 2 to also shutdown attached volumes w/o volume header loaded
1254 * 3 to also shutdown exclusive state volumes
1256 * caller MUST hold exclusive access on the hash chain
1257 * because we drop vol_glock_mutex internally
1259 * this function is reentrant for passes 1--3
1260 * (e.g. multiple threads can cooperate to
1261 * shutdown a partition mp-fast)
1263 * pass 0 is not scaleable because the volume state data is
1264 * synchronized by vol_glock mutex, and the locking overhead
1265 * is too high to drop the lock long enough to do linked list
1269 ShutdownVByPForPass_r(struct DiskPartition64 * dp, int pass)
1271 struct rx_queue * q = queue_First(&dp->vol_list, rx_queue);
1274 while (ShutdownVolumeWalk_r(dp, pass, &q))
1280 /* conditionally shutdown one volume on partition dp
1281 * returns 1 if a volume was shutdown in this pass,
1284 ShutdownVolumeWalk_r(struct DiskPartition64 * dp, int pass,
1285 struct rx_queue ** idx)
1287 struct rx_queue *qp, *nqp;
1292 for (queue_ScanFrom(&dp->vol_list, qp, qp, nqp, rx_queue)) {
1293 vp = (Volume *) (((char *)qp) - offsetof(Volume, vol_list));
1297 if ((V_attachState(vp) != VOL_STATE_UNATTACHED) &&
1298 (V_attachState(vp) != VOL_STATE_ERROR) &&
1299 (V_attachState(vp) != VOL_STATE_PREATTACHED)) {
1303 if ((V_attachState(vp) == VOL_STATE_ATTACHED) &&
1304 (vp->header == NULL)) {
1308 if (VIsExclusiveState(V_attachState(vp))) {
1313 DeleteVolumeFromVByPList_r(vp);
1314 VShutdownVolume_r(vp);
1324 * shutdown a specific volume
1326 /* caller MUST NOT hold a heavyweight ref on vp */
1328 VShutdownVolume_r(Volume * vp)
1332 VCreateReservation_r(vp);
1334 if (LogLevel >= 5) {
1335 Log("VShutdownVolume_r: vid=%u, device=%d, state=%hu\n",
1336 vp->hashid, vp->partition->device, V_attachState(vp));
1339 /* wait for other blocking ops to finish */
1340 VWaitExclusiveState_r(vp);
1342 assert(VIsValidState(V_attachState(vp)));
1344 switch(V_attachState(vp)) {
1345 case VOL_STATE_SALVAGING:
1346 /* Leave salvaging volumes alone. Any in-progress salvages will
1347 * continue working after viced shuts down. This is intentional.
1350 case VOL_STATE_PREATTACHED:
1351 case VOL_STATE_ERROR:
1352 VChangeState_r(vp, VOL_STATE_UNATTACHED);
1353 case VOL_STATE_UNATTACHED:
1355 case VOL_STATE_GOING_OFFLINE:
1356 case VOL_STATE_SHUTTING_DOWN:
1357 case VOL_STATE_ATTACHED:
1361 Log("VShutdown: Attempting to take volume %u offline.\n",
1364 /* take the volume offline (drops reference count) */
1365 VOffline_r(vp, "File server was shut down");
1370 VCancelReservation_r(vp);
1374 #endif /* AFS_DEMAND_ATTACH_FS */
1377 /***************************************************/
1378 /* Header I/O routines */
1379 /***************************************************/
1381 /* open a descriptor for the inode (h),
1382 * read in an on-disk structure into buffer (to) of size (size),
1383 * verify versionstamp in structure has magic (magic) and
1384 * optionally verify version (version) if (version) is nonzero
1387 ReadHeader(Error * ec, IHandle_t * h, char *to, int size, bit32 magic,
1390 struct versionStamp *vsn;
1405 if (FDH_SEEK(fdP, 0, SEEK_SET) < 0) {
1407 FDH_REALLYCLOSE(fdP);
1410 vsn = (struct versionStamp *)to;
1411 if (FDH_READ(fdP, to, size) != size || vsn->magic != magic) {
1413 FDH_REALLYCLOSE(fdP);
1418 /* Check is conditional, in case caller wants to inspect version himself */
1419 if (version && vsn->version != version) {
1425 WriteVolumeHeader_r(Error * ec, Volume * vp)
1427 IHandle_t *h = V_diskDataHandle(vp);
1437 if (FDH_SEEK(fdP, 0, SEEK_SET) < 0) {
1439 FDH_REALLYCLOSE(fdP);
1442 if (FDH_WRITE(fdP, (char *)&V_disk(vp), sizeof(V_disk(vp)))
1443 != sizeof(V_disk(vp))) {
1445 FDH_REALLYCLOSE(fdP);
1451 /* VolumeHeaderToDisk
1452 * Allows for storing 64 bit inode numbers in on-disk volume header
1455 /* convert in-memory representation of a volume header to the
1456 * on-disk representation of a volume header */
1458 VolumeHeaderToDisk(VolumeDiskHeader_t * dh, VolumeHeader_t * h)
1461 memset((char *)dh, 0, sizeof(VolumeDiskHeader_t));
1462 dh->stamp = h->stamp;
1464 dh->parent = h->parent;
1466 #ifdef AFS_64BIT_IOPS_ENV
1467 dh->volumeInfo_lo = (afs_int32) h->volumeInfo & 0xffffffff;
1468 dh->volumeInfo_hi = (afs_int32) (h->volumeInfo >> 32) & 0xffffffff;
1469 dh->smallVnodeIndex_lo = (afs_int32) h->smallVnodeIndex & 0xffffffff;
1470 dh->smallVnodeIndex_hi =
1471 (afs_int32) (h->smallVnodeIndex >> 32) & 0xffffffff;
1472 dh->largeVnodeIndex_lo = (afs_int32) h->largeVnodeIndex & 0xffffffff;
1473 dh->largeVnodeIndex_hi =
1474 (afs_int32) (h->largeVnodeIndex >> 32) & 0xffffffff;
1475 dh->linkTable_lo = (afs_int32) h->linkTable & 0xffffffff;
1476 dh->linkTable_hi = (afs_int32) (h->linkTable >> 32) & 0xffffffff;
1478 dh->volumeInfo_lo = h->volumeInfo;
1479 dh->smallVnodeIndex_lo = h->smallVnodeIndex;
1480 dh->largeVnodeIndex_lo = h->largeVnodeIndex;
1481 dh->linkTable_lo = h->linkTable;
1485 /* DiskToVolumeHeader
1486 * Converts an on-disk representation of a volume header to
1487 * the in-memory representation of a volume header.
1489 * Makes the assumption that AFS has *always*
1490 * zero'd the volume header file so that high parts of inode
1491 * numbers are 0 in older (SGI EFS) volume header files.
1494 DiskToVolumeHeader(VolumeHeader_t * h, VolumeDiskHeader_t * dh)
1496 memset((char *)h, 0, sizeof(VolumeHeader_t));
1497 h->stamp = dh->stamp;
1499 h->parent = dh->parent;
1501 #ifdef AFS_64BIT_IOPS_ENV
1503 (Inode) dh->volumeInfo_lo | ((Inode) dh->volumeInfo_hi << 32);
1505 h->smallVnodeIndex =
1506 (Inode) dh->smallVnodeIndex_lo | ((Inode) dh->
1507 smallVnodeIndex_hi << 32);
1509 h->largeVnodeIndex =
1510 (Inode) dh->largeVnodeIndex_lo | ((Inode) dh->
1511 largeVnodeIndex_hi << 32);
1513 (Inode) dh->linkTable_lo | ((Inode) dh->linkTable_hi << 32);
1515 h->volumeInfo = dh->volumeInfo_lo;
1516 h->smallVnodeIndex = dh->smallVnodeIndex_lo;
1517 h->largeVnodeIndex = dh->largeVnodeIndex_lo;
1518 h->linkTable = dh->linkTable_lo;
1523 /***************************************************/
1524 /* Volume Attachment routines */
1525 /***************************************************/
1527 #ifdef AFS_DEMAND_ATTACH_FS
1529 * pre-attach a volume given its path.
1531 * @param[out] ec outbound error code
1532 * @param[in] partition partition path string
1533 * @param[in] name volume id string
1535 * @return volume object pointer
1537 * @note A pre-attached volume will only have its partition
1538 * and hashid fields initialized. At first call to
1539 * VGetVolume, the volume will be fully attached.
1543 VPreAttachVolumeByName(Error * ec, char *partition, char *name)
1547 vp = VPreAttachVolumeByName_r(ec, partition, name);
1553 * pre-attach a volume given its path.
1555 * @param[out] ec outbound error code
1556 * @param[in] partition path to vice partition
1557 * @param[in] name volume id string
1559 * @return volume object pointer
1561 * @pre VOL_LOCK held
1563 * @internal volume package internal use only.
1566 VPreAttachVolumeByName_r(Error * ec, char *partition, char *name)
1568 return VPreAttachVolumeById_r(ec,
1570 VolumeNumber(name));
1574 * pre-attach a volume given its path and numeric volume id.
1576 * @param[out] ec error code return
1577 * @param[in] partition path to vice partition
1578 * @param[in] volumeId numeric volume id
1580 * @return volume object pointer
1582 * @pre VOL_LOCK held
1584 * @internal volume package internal use only.
1587 VPreAttachVolumeById_r(Error * ec,
1592 struct DiskPartition64 *partp;
1596 assert(programType == fileServer);
1598 if (!(partp = VGetPartition_r(partition, 0))) {
1600 Log("VPreAttachVolumeById_r: Error getting partition (%s)\n", partition);
1604 vp = VLookupVolume_r(ec, volumeId, NULL);
1609 return VPreAttachVolumeByVp_r(ec, partp, vp, volumeId);
1613 * preattach a volume.
1615 * @param[out] ec outbound error code
1616 * @param[in] partp pointer to partition object
1617 * @param[in] vp pointer to volume object
1618 * @param[in] vid volume id
1620 * @return volume object pointer
1622 * @pre VOL_LOCK is held.
1624 * @warning Returned volume object pointer does not have to
1625 * equal the pointer passed in as argument vp. There
1626 * are potential race conditions which can result in
1627 * the pointers having different values. It is up to
1628 * the caller to make sure that references are handled
1629 * properly in this case.
1631 * @note If there is already a volume object registered with
1632 * the same volume id, its pointer MUST be passed as
1633 * argument vp. Failure to do so will result in a silent
1634 * failure to preattach.
1636 * @internal volume package internal use only.
1639 VPreAttachVolumeByVp_r(Error * ec,
1640 struct DiskPartition64 * partp,
1648 /* check to see if pre-attach already happened */
1650 (V_attachState(vp) != VOL_STATE_UNATTACHED) &&
1651 (V_attachState(vp) != VOL_STATE_PREATTACHED) &&
1652 !VIsErrorState(V_attachState(vp))) {
1654 * pre-attach is a no-op in all but the following cases:
1656 * - volume is unattached
1657 * - volume is in an error state
1658 * - volume is pre-attached
1660 Log("VPreattachVolumeByVp_r: volume %u not in quiescent state\n", vid);
1663 /* we're re-attaching a volume; clear out some old state */
1664 memset(&vp->salvage, 0, sizeof(struct VolumeOnlineSalvage));
1666 if (V_partition(vp) != partp) {
1667 /* XXX potential race */
1668 DeleteVolumeFromVByPList_r(vp);
1671 /* if we need to allocate a new Volume struct,
1672 * go ahead and drop the vol glock, otherwise
1673 * do the basic setup synchronised, as it's
1674 * probably not worth dropping the lock */
1677 /* allocate the volume structure */
1678 vp = nvp = (Volume *) malloc(sizeof(Volume));
1680 memset(vp, 0, sizeof(Volume));
1681 queue_Init(&vp->vnode_list);
1682 assert(pthread_cond_init(&V_attachCV(vp), NULL) == 0);
1685 /* link the volume with its associated vice partition */
1686 vp->device = partp->device;
1687 vp->partition = partp;
1690 vp->specialStatus = 0;
1692 /* if we dropped the lock, reacquire the lock,
1693 * check for pre-attach races, and then add
1694 * the volume to the hash table */
1697 nvp = VLookupVolume_r(ec, vid, NULL);
1702 } else if (nvp) { /* race detected */
1707 /* hack to make up for VChangeState_r() decrementing
1708 * the old state counter */
1709 VStats.state_levels[0]++;
1713 /* put pre-attached volume onto the hash table
1714 * and bring it up to the pre-attached state */
1715 AddVolumeToHashTable(vp, vp->hashid);
1716 AddVolumeToVByPList_r(vp);
1717 VLRU_Init_Node_r(vp);
1718 VChangeState_r(vp, VOL_STATE_PREATTACHED);
1721 Log("VPreAttachVolumeByVp_r: volume %u pre-attached\n", vp->hashid);
1729 #endif /* AFS_DEMAND_ATTACH_FS */
1731 /* Attach an existing volume, given its pathname, and return a
1732 pointer to the volume header information. The volume also
1733 normally goes online at this time. An offline volume
1734 must be reattached to make it go online */
1736 VAttachVolumeByName(Error * ec, char *partition, char *name, int mode)
1740 retVal = VAttachVolumeByName_r(ec, partition, name, mode);
1746 VAttachVolumeByName_r(Error * ec, char *partition, char *name, int mode)
1748 register Volume *vp = NULL;
1750 struct afs_stat status;
1751 struct VolumeDiskHeader diskHeader;
1752 struct VolumeHeader iheader;
1753 struct DiskPartition64 *partp;
1757 #ifdef AFS_DEMAND_ATTACH_FS
1758 VolumeStats stats_save;
1760 #endif /* AFS_DEMAND_ATTACH_FS */
1764 volumeId = VolumeNumber(name);
1766 if (!(partp = VGetPartition_r(partition, 0))) {
1768 Log("VAttachVolume: Error getting partition (%s)\n", partition);
1772 if (programType == volumeUtility) {
1774 VLockPartition_r(partition);
1775 } else if (programType == fileServer) {
1776 #ifdef AFS_DEMAND_ATTACH_FS
1777 /* lookup the volume in the hash table */
1778 vp = VLookupVolume_r(ec, volumeId, NULL);
1784 /* save any counters that are supposed to
1785 * be monotonically increasing over the
1786 * lifetime of the fileserver */
1787 memcpy(&stats_save, &vp->stats, sizeof(VolumeStats));
1789 memset(&stats_save, 0, sizeof(VolumeStats));
1792 /* if there's something in the hash table, and it's not
1793 * in the pre-attach state, then we may need to detach
1794 * it before proceeding */
1795 if (vp && (V_attachState(vp) != VOL_STATE_PREATTACHED)) {
1796 VCreateReservation_r(vp);
1797 VWaitExclusiveState_r(vp);
1799 /* at this point state must be one of:
1808 if (vp->specialStatus == VBUSY)
1811 /* if it's already attached, see if we can return it */
1812 if (V_attachState(vp) == VOL_STATE_ATTACHED) {
1813 VGetVolumeByVp_r(ec, vp);
1814 if (V_inUse(vp) == fileServer) {
1815 VCancelReservation_r(vp);
1819 /* otherwise, we need to detach, and attempt to re-attach */
1820 VDetachVolume_r(ec, vp);
1822 Log("VAttachVolume: Error detaching old volume instance (%s)\n", name);
1825 /* if it isn't fully attached, delete from the hash tables,
1826 and let the refcounter handle the rest */
1827 DeleteVolumeFromHashTable(vp);
1828 DeleteVolumeFromVByPList_r(vp);
1831 VCancelReservation_r(vp);
1835 /* pre-attach volume if it hasn't been done yet */
1837 (V_attachState(vp) == VOL_STATE_UNATTACHED) ||
1838 (V_attachState(vp) == VOL_STATE_ERROR)) {
1840 vp = VPreAttachVolumeByVp_r(ec, partp, vp, volumeId);
1848 /* handle pre-attach races
1850 * multiple threads can race to pre-attach a volume,
1851 * but we can't let them race beyond that
1853 * our solution is to let the first thread to bring
1854 * the volume into an exclusive state win; the other
1855 * threads just wait until it finishes bringing the
1856 * volume online, and then they do a vgetvolumebyvp
1858 if (svp && (svp != vp)) {
1859 /* wait for other exclusive ops to finish */
1860 VCreateReservation_r(vp);
1861 VWaitExclusiveState_r(vp);
1863 /* get a heavyweight ref, kill the lightweight ref, and return */
1864 VGetVolumeByVp_r(ec, vp);
1865 VCancelReservation_r(vp);
1869 /* at this point, we are chosen as the thread to do
1870 * demand attachment for this volume. all other threads
1871 * doing a getvolume on vp->hashid will block until we finish */
1873 /* make sure any old header cache entries are invalidated
1874 * before proceeding */
1875 FreeVolumeHeader(vp);
1877 VChangeState_r(vp, VOL_STATE_ATTACHING);
1879 /* restore any saved counters */
1880 memcpy(&vp->stats, &stats_save, sizeof(VolumeStats));
1881 #else /* AFS_DEMAND_ATTACH_FS */
1882 vp = VGetVolume_r(ec, volumeId);
1884 if (V_inUse(vp) == fileServer)
1886 if (vp->specialStatus == VBUSY)
1888 VDetachVolume_r(ec, vp);
1890 Log("VAttachVolume: Error detaching volume (%s)\n", name);
1894 #endif /* AFS_DEMAND_ATTACH_FS */
1898 strcpy(path, VPartitionPath(partp));
1904 if ((fd = afs_open(path, O_RDONLY)) == -1 || afs_fstat(fd, &status) == -1) {
1905 Log("VAttachVolume: Failed to open %s (errno %d)\n", path, errno);
1912 n = read(fd, &diskHeader, sizeof(diskHeader));
1914 if (n != sizeof(diskHeader)
1915 || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
1916 Log("VAttachVolume: Error reading volume header %s\n", path);
1921 if (diskHeader.stamp.version != VOLUMEHEADERVERSION) {
1922 Log("VAttachVolume: Volume %s, version number is incorrect; volume needs salvaged\n", path);
1928 DiskToVolumeHeader(&iheader, &diskHeader);
1929 #ifdef FSSYNC_BUILD_CLIENT
1930 if (programType == volumeUtility && mode != V_SECRETLY && mode != V_PEEK) {
1932 if (FSYNC_VolOp(iheader.id, partition, FSYNC_VOL_NEEDVOLUME, mode, NULL)
1934 Log("VAttachVolume: attach of volume %u apparently denied by file server\n", iheader.id);
1935 *ec = VNOVOL; /* XXXX */
1943 vp = (Volume *) calloc(1, sizeof(Volume));
1945 vp->device = partp->device;
1946 vp->partition = partp;
1947 queue_Init(&vp->vnode_list);
1948 #ifdef AFS_DEMAND_ATTACH_FS
1949 assert(pthread_cond_init(&V_attachCV(vp), NULL) == 0);
1950 #endif /* AFS_DEMAND_ATTACH_FS */
1953 /* attach2 is entered without any locks, and returns
1954 * with vol_glock_mutex held */
1955 vp = attach2(ec, volumeId, path, &iheader, partp, vp, isbusy, mode);
1957 if (programType == volumeUtility && vp) {
1958 if ((mode == V_VOLUPD) || (VolumeWriteable(vp) && (mode == V_CLONE))) {
1959 /* mark volume header as in use so that volser crashes lead to a
1960 * salvage attempt */
1961 VUpdateVolume_r(ec, vp, 0);
1963 #ifdef AFS_DEMAND_ATTACH_FS
1964 /* for dafs, we should tell the fileserver, except for V_PEEK
1965 * where we know it is not necessary */
1966 if (mode == V_PEEK) {
1967 vp->needsPutBack = 0;
1969 vp->needsPutBack = 1;
1971 #else /* !AFS_DEMAND_ATTACH_FS */
1972 /* duplicate computation in fssync.c about whether the server
1973 * takes the volume offline or not. If the volume isn't
1974 * offline, we must not return it when we detach the volume,
1975 * or the server will abort */
1976 if (mode == V_READONLY || mode == V_PEEK
1977 || (!VolumeWriteable(vp) && (mode == V_CLONE || mode == V_DUMP)))
1978 vp->needsPutBack = 0;
1980 vp->needsPutBack = 1;
1981 #endif /* !AFS_DEMAND_ATTACH_FS */
1983 /* OK, there's a problem here, but one that I don't know how to
1984 * fix right now, and that I don't think should arise often.
1985 * Basically, we should only put back this volume to the server if
1986 * it was given to us by the server, but since we don't have a vp,
1987 * we can't run the VolumeWriteable function to find out as we do
1988 * above when computing vp->needsPutBack. So we send it back, but
1989 * there's a path in VAttachVolume on the server which may abort
1990 * if this volume doesn't have a header. Should be pretty rare
1991 * for all of that to happen, but if it does, probably the right
1992 * fix is for the server to allow the return of readonly volumes
1993 * that it doesn't think are really checked out. */
1994 #ifdef FSSYNC_BUILD_CLIENT
1995 if (programType == volumeUtility && vp == NULL &&
1996 mode != V_SECRETLY && mode != V_PEEK) {
1997 FSYNC_VolOp(iheader.id, partition, FSYNC_VOL_ON, 0, NULL);
2000 if (programType == fileServer && vp) {
2001 #ifdef AFS_DEMAND_ATTACH_FS
2003 * we can get here in cases where we don't "own"
2004 * the volume (e.g. volume owned by a utility).
2005 * short circuit around potential disk header races.
2007 if (V_attachState(vp) != VOL_STATE_ATTACHED) {
2011 V_needsCallback(vp) = 0;
2013 if (VInit >= 2 && V_BreakVolumeCallbacks) {
2014 Log("VAttachVolume: Volume %u was changed externally; breaking callbacks\n", V_id(vp));
2015 (*V_BreakVolumeCallbacks) (V_id(vp));
2018 VUpdateVolume_r(ec, vp, 0);
2020 Log("VAttachVolume: Error updating volume\n");
2025 if (VolumeWriteable(vp) && V_dontSalvage(vp) == 0) {
2026 #ifndef AFS_DEMAND_ATTACH_FS
2027 /* This is a hack: by temporarily setting the incore
2028 * dontSalvage flag ON, the volume will be put back on the
2029 * Update list (with dontSalvage OFF again). It will then
2030 * come back in N minutes with DONT_SALVAGE eventually
2031 * set. This is the way that volumes that have never had
2032 * it set get it set; or that volumes that have been
2033 * offline without DONT SALVAGE having been set also
2034 * eventually get it set */
2035 V_dontSalvage(vp) = DONT_SALVAGE;
2036 #endif /* !AFS_DEMAND_ATTACH_FS */
2037 VAddToVolumeUpdateList_r(ec, vp);
2039 Log("VAttachVolume: Error adding volume to update list\n");
2046 Log("VOnline: volume %u (%s) attached and online\n", V_id(vp),
2051 if (programType == volumeUtility) {
2052 VUnlockPartition_r(partition);
2055 #ifdef AFS_DEMAND_ATTACH_FS
2056 /* attach failed; make sure we're in error state */
2057 if (vp && !VIsErrorState(V_attachState(vp))) {
2058 VChangeState_r(vp, VOL_STATE_ERROR);
2060 #endif /* AFS_DEMAND_ATTACH_FS */
2067 #ifdef AFS_DEMAND_ATTACH_FS
2068 /* VAttachVolumeByVp_r
2070 * finish attaching a volume that is
2071 * in a less than fully attached state
2073 /* caller MUST hold a ref count on vp */
2075 VAttachVolumeByVp_r(Error * ec, Volume * vp, int mode)
2077 char name[VMAXPATHLEN];
2078 int fd, n, reserve = 0;
2079 struct afs_stat status;
2080 struct VolumeDiskHeader diskHeader;
2081 struct VolumeHeader iheader;
2082 struct DiskPartition64 *partp;
2087 VolumeStats stats_save;
2090 /* volume utility should never call AttachByVp */
2091 assert(programType == fileServer);
2093 volumeId = vp->hashid;
2094 partp = vp->partition;
2095 VolumeExternalName_r(volumeId, name, sizeof(name));
2098 /* if another thread is performing a blocking op, wait */
2099 VWaitExclusiveState_r(vp);
2101 memcpy(&stats_save, &vp->stats, sizeof(VolumeStats));
2103 /* if it's already attached, see if we can return it */
2104 if (V_attachState(vp) == VOL_STATE_ATTACHED) {
2105 VGetVolumeByVp_r(ec, vp);
2106 if (V_inUse(vp) == fileServer) {
2109 if (vp->specialStatus == VBUSY)
2111 VDetachVolume_r(ec, vp);
2113 Log("VAttachVolume: Error detaching volume (%s)\n", name);
2119 /* pre-attach volume if it hasn't been done yet */
2121 (V_attachState(vp) == VOL_STATE_UNATTACHED) ||
2122 (V_attachState(vp) == VOL_STATE_ERROR)) {
2123 nvp = VPreAttachVolumeByVp_r(ec, partp, vp, volumeId);
2129 VCreateReservation_r(nvp);
2135 VChangeState_r(vp, VOL_STATE_ATTACHING);
2137 /* restore monotonically increasing stats */
2138 memcpy(&vp->stats, &stats_save, sizeof(VolumeStats));
2143 /* compute path to disk header,
2145 * and verify magic and version stamps */
2146 strcpy(path, VPartitionPath(partp));
2152 if ((fd = afs_open(path, O_RDONLY)) == -1 || afs_fstat(fd, &status) == -1) {
2153 Log("VAttachVolume: Failed to open %s (errno %d)\n", path, errno);
2160 n = read(fd, &diskHeader, sizeof(diskHeader));
2162 if (n != sizeof(diskHeader)
2163 || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
2164 Log("VAttachVolume: Error reading volume header %s\n", path);
2169 if (diskHeader.stamp.version != VOLUMEHEADERVERSION) {
2170 Log("VAttachVolume: Volume %s, version number is incorrect; volume needs salvaged\n", path);
2176 /* convert on-disk header format to in-memory header format */
2177 DiskToVolumeHeader(&iheader, &diskHeader);
2181 * NOTE: attach2 is entered without any locks, and returns
2182 * with vol_glock_mutex held */
2183 vp = attach2(ec, volumeId, path, &iheader, partp, vp, isbusy, mode);
2186 * the event that an error was encountered, or
2187 * the volume was not brought to an attached state
2188 * for any reason, skip to the end. We cannot
2189 * safely call VUpdateVolume unless we "own" it.
2193 (V_attachState(vp) != VOL_STATE_ATTACHED)) {
2197 V_needsCallback(vp) = 0;
2198 VUpdateVolume_r(ec, vp, 0);
2200 Log("VAttachVolume: Error updating volume %u\n", vp->hashid);
2204 if (VolumeWriteable(vp) && V_dontSalvage(vp) == 0) {
2205 #ifndef AFS_DEMAND_ATTACH_FS
2206 /* This is a hack: by temporarily setting the incore
2207 * dontSalvage flag ON, the volume will be put back on the
2208 * Update list (with dontSalvage OFF again). It will then
2209 * come back in N minutes with DONT_SALVAGE eventually
2210 * set. This is the way that volumes that have never had
2211 * it set get it set; or that volumes that have been
2212 * offline without DONT SALVAGE having been set also
2213 * eventually get it set */
2214 V_dontSalvage(vp) = DONT_SALVAGE;
2215 #endif /* !AFS_DEMAND_ATTACH_FS */
2216 VAddToVolumeUpdateList_r(ec, vp);
2218 Log("VAttachVolume: Error adding volume %u to update list\n", vp->hashid);
2225 Log("VOnline: volume %u (%s) attached and online\n", V_id(vp),
2229 VCancelReservation_r(nvp);
2232 if (*ec && (*ec != VOFFLINE) && (*ec != VSALVAGE)) {
2233 if (vp && !VIsErrorState(V_attachState(vp))) {
2234 VChangeState_r(vp, VOL_STATE_ERROR);
2241 #endif /* AFS_DEMAND_ATTACH_FS */
2244 * called without any locks held
2245 * returns with vol_glock_mutex held
2248 attach2(Error * ec, VolId volumeId, char *path, register struct VolumeHeader * header,
2249 struct DiskPartition64 * partp, register Volume * vp, int isbusy, int mode)
2251 vp->specialStatus = (byte) (isbusy ? VBUSY : 0);
2252 IH_INIT(vp->vnodeIndex[vLarge].handle, partp->device, header->parent,
2253 header->largeVnodeIndex);
2254 IH_INIT(vp->vnodeIndex[vSmall].handle, partp->device, header->parent,
2255 header->smallVnodeIndex);
2256 IH_INIT(vp->diskDataHandle, partp->device, header->parent,
2257 header->volumeInfo);
2258 IH_INIT(vp->linkHandle, partp->device, header->parent, header->linkTable);
2259 vp->shuttingDown = 0;
2260 vp->goingOffline = 0;
2262 #ifdef AFS_DEMAND_ATTACH_FS
2263 vp->stats.last_attach = FT_ApproxTime();
2264 vp->stats.attaches++;
2268 IncUInt64(&VStats.attaches);
2269 vp->cacheCheck = ++VolumeCacheCheck;
2270 /* just in case this ever rolls over */
2271 if (!vp->cacheCheck)
2272 vp->cacheCheck = ++VolumeCacheCheck;
2273 GetVolumeHeader(vp);
2276 #if defined(AFS_DEMAND_ATTACH_FS) && defined(FSSYNC_BUILD_CLIENT)
2277 /* demand attach changes the V_PEEK mechanism
2279 * we can now suck the current disk data structure over
2280 * the fssync interface without going to disk
2282 * (technically, we don't need to restrict this feature
2283 * to demand attach fileservers. However, I'm trying
2284 * to limit the number of common code changes)
2286 if (programType != fileServer && mode == V_PEEK) {
2288 res.payload.len = sizeof(VolumeDiskData);
2289 res.payload.buf = &vp->header->diskstuff;
2291 if (FSYNC_VolOp(volumeId,
2293 FSYNC_VOL_QUERY_HDR,
2296 goto disk_header_loaded;
2299 #endif /* AFS_DEMAND_ATTACH_FS && FSSYNC_BUILD_CLIENT */
2300 (void)ReadHeader(ec, V_diskDataHandle(vp), (char *)&V_disk(vp),
2301 sizeof(V_disk(vp)), VOLUMEINFOMAGIC, VOLUMEINFOVERSION);
2303 #ifdef AFS_DEMAND_ATTACH_FS
2306 IncUInt64(&VStats.hdr_loads);
2307 IncUInt64(&vp->stats.hdr_loads);
2309 #endif /* AFS_DEMAND_ATTACH_FS */
2312 Log("VAttachVolume: Error reading diskDataHandle vol header %s; error=%u\n", path, *ec);
2315 #ifdef AFS_DEMAND_ATTACH_FS
2319 /* check for pending volume operations */
2320 if (vp->pending_vol_op) {
2321 /* see if the pending volume op requires exclusive access */
2322 switch (vp->pending_vol_op->vol_op_state) {
2323 case FSSYNC_VolOpPending:
2324 /* this should never happen */
2325 assert(vp->pending_vol_op->vol_op_state != FSSYNC_VolOpPending);
2328 case FSSYNC_VolOpRunningUnknown:
2329 if (VVolOpLeaveOnline_r(vp, vp->pending_vol_op)) {
2330 vp->pending_vol_op->vol_op_state = FSSYNC_VolOpRunningOnline;
2333 vp->pending_vol_op->vol_op_state = FSSYNC_VolOpRunningOffline;
2334 /* fall through to take volume offline */
2337 case FSSYNC_VolOpRunningOffline:
2338 /* mark the volume down */
2340 VChangeState_r(vp, VOL_STATE_UNATTACHED);
2341 if (V_offlineMessage(vp)[0] == '\0')
2342 strlcpy(V_offlineMessage(vp),
2343 "A volume utility is running.",
2344 sizeof(V_offlineMessage(vp)));
2345 V_offlineMessage(vp)[sizeof(V_offlineMessage(vp)) - 1] = '\0';
2347 /* check to see if we should set the specialStatus flag */
2348 if (VVolOpSetVBusy_r(vp, vp->pending_vol_op)) {
2349 vp->specialStatus = VBUSY;
2354 V_attachFlags(vp) |= VOL_HDR_LOADED;
2355 vp->stats.last_hdr_load = vp->stats.last_attach;
2357 #endif /* AFS_DEMAND_ATTACH_FS */
2360 struct IndexFileHeader iHead;
2362 #if OPENAFS_VOL_STATS
2364 * We just read in the diskstuff part of the header. If the detailed
2365 * volume stats area has not yet been initialized, we should bzero the
2366 * area and mark it as initialized.
2368 if (!(V_stat_initialized(vp))) {
2369 memset((char *)(V_stat_area(vp)), 0, VOL_STATS_BYTES);
2370 V_stat_initialized(vp) = 1;
2372 #endif /* OPENAFS_VOL_STATS */
2374 (void)ReadHeader(ec, vp->vnodeIndex[vSmall].handle,
2375 (char *)&iHead, sizeof(iHead),
2376 SMALLINDEXMAGIC, SMALLINDEXVERSION);
2379 Log("VAttachVolume: Error reading smallVnode vol header %s; error=%u\n", path, *ec);
2384 struct IndexFileHeader iHead;
2386 (void)ReadHeader(ec, vp->vnodeIndex[vLarge].handle,
2387 (char *)&iHead, sizeof(iHead),
2388 LARGEINDEXMAGIC, LARGEINDEXVERSION);
2391 Log("VAttachVolume: Error reading largeVnode vol header %s; error=%u\n", path, *ec);
2395 #ifdef AFS_NAMEI_ENV
2397 struct versionStamp stamp;
2399 (void)ReadHeader(ec, V_linkHandle(vp), (char *)&stamp,
2400 sizeof(stamp), LINKTABLEMAGIC, LINKTABLEVERSION);
2403 Log("VAttachVolume: Error reading namei vol header %s; error=%u\n", path, *ec);
2406 #endif /* AFS_NAMEI_ENV */
2408 #if defined(AFS_DEMAND_ATTACH_FS)
2409 if (*ec && ((*ec != VOFFLINE) || (V_attachState(vp) != VOL_STATE_UNATTACHED))) {
2411 if (programType == fileServer) {
2412 VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, VOL_SALVAGE_INVALIDATE_HEADER);
2415 Log("VAttachVolume: Error attaching volume %s; volume needs salvage; error=%u\n", path, *ec);
2421 /* volume operation in progress */
2425 #else /* AFS_DEMAND_ATTACH_FS */
2427 Log("VAttachVolume: Error attaching volume %s; volume needs salvage; error=%u\n", path, *ec);
2432 #endif /* AFS_DEMAND_ATTACH_FS */
2434 if (V_needsSalvaged(vp)) {
2435 if (vp->specialStatus)
2436 vp->specialStatus = 0;
2438 #if defined(AFS_DEMAND_ATTACH_FS)
2439 if (programType == fileServer) {
2440 VRequestSalvage_r(ec, vp, SALVSYNC_NEEDED, VOL_SALVAGE_INVALIDATE_HEADER);
2443 Log("VAttachVolume: volume salvage flag is ON for %s; volume needs salvage\n", path);
2447 #else /* AFS_DEMAND_ATTACH_FS */
2450 #endif /* AFS_DEMAND_ATTACH_FS */
2455 if (programType == fileServer) {
2456 #ifndef FAST_RESTART
2457 if (V_inUse(vp) && VolumeWriteable(vp)) {
2458 if (!V_needsSalvaged(vp)) {
2459 V_needsSalvaged(vp) = 1;
2460 VUpdateVolume_r(ec, vp, 0);
2462 #if defined(AFS_DEMAND_ATTACH_FS)
2463 VRequestSalvage_r(ec, vp, SALVSYNC_NEEDED, VOL_SALVAGE_INVALIDATE_HEADER);
2465 #else /* AFS_DEMAND_ATTACH_FS */
2466 Log("VAttachVolume: volume %s needs to be salvaged; not attached.\n", path);
2469 #endif /* AFS_DEMAND_ATTACH_FS */
2472 #endif /* FAST_RESTART */
2474 if (V_destroyMe(vp) == DESTROY_ME) {
2475 #if defined(AFS_DEMAND_ATTACH_FS)
2476 /* schedule a salvage so the volume goes away on disk */
2477 VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, VOL_SALVAGE_INVALIDATE_HEADER);
2478 VChangeState_r(vp, VOL_STATE_ERROR);
2480 #endif /* AFS_DEMAND_ATTACH_FS */
2482 Log("VAttachVolume: volume %s is junk; it should be destroyed at next salvage\n", path);
2488 vp->nextVnodeUnique = V_uniquifier(vp);
2489 vp->vnodeIndex[vSmall].bitmap = vp->vnodeIndex[vLarge].bitmap = NULL;
2490 #ifndef BITMAP_LATER
2491 if (programType == fileServer && VolumeWriteable(vp)) {
2493 for (i = 0; i < nVNODECLASSES; i++) {
2494 VGetBitmap_r(ec, vp, i);
2496 #ifdef AFS_DEMAND_ATTACH_FS
2497 VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, VOL_SALVAGE_INVALIDATE_HEADER);
2499 #else /* AFS_DEMAND_ATTACH_FS */
2501 #endif /* AFS_DEMAND_ATTACH_FS */
2502 Log("VAttachVolume: error getting bitmap for volume (%s)\n",
2508 #endif /* BITMAP_LATER */
2510 if (programType == fileServer) {
2511 if (vp->specialStatus)
2512 vp->specialStatus = 0;
2513 if (V_blessed(vp) && V_inService(vp) && !V_needsSalvaged(vp)) {
2514 V_inUse(vp) = fileServer;
2515 V_offlineMessage(vp)[0] = '\0';
2518 if ((mode != V_PEEK) && (mode != V_SECRETLY))
2519 V_inUse(vp) = programType;
2520 V_checkoutMode(vp) = mode;
2523 AddVolumeToHashTable(vp, V_id(vp));
2524 #ifdef AFS_DEMAND_ATTACH_FS
2525 if ((programType != fileServer) ||
2526 (V_inUse(vp) == fileServer)) {
2527 AddVolumeToVByPList_r(vp);
2529 VChangeState_r(vp, VOL_STATE_ATTACHED);
2531 VChangeState_r(vp, VOL_STATE_UNATTACHED);
2537 /* Attach an existing volume.
2538 The volume also normally goes online at this time.
2539 An offline volume must be reattached to make it go online.
2543 VAttachVolume(Error * ec, VolumeId volumeId, int mode)
2547 retVal = VAttachVolume_r(ec, volumeId, mode);
2553 VAttachVolume_r(Error * ec, VolumeId volumeId, int mode)
2556 VGetVolumePath(ec, volumeId, &part, &name);
2558 register Volume *vp;
2560 vp = VGetVolume_r(&error, volumeId);
2562 assert(V_inUse(vp) == 0);
2563 VDetachVolume_r(ec, vp);
2567 return VAttachVolumeByName_r(ec, part, name, mode);
2570 /* Increment a reference count to a volume, sans context swaps. Requires
2571 * possibly reading the volume header in from the disk, since there's
2572 * an invariant in the volume package that nUsers>0 ==> vp->header is valid.
2574 * N.B. This call can fail if we can't read in the header!! In this case
2575 * we still guarantee we won't context swap, but the ref count won't be
2576 * incremented (otherwise we'd violate the invariant).
2578 /* NOTE: with the demand attach fileserver extensions, the global lock
2579 * is dropped within VHold */
2580 #ifdef AFS_DEMAND_ATTACH_FS
2582 VHold_r(register Volume * vp)
2586 VCreateReservation_r(vp);
2587 VWaitExclusiveState_r(vp);
2589 LoadVolumeHeader(&error, vp);
2591 VCancelReservation_r(vp);
2595 VCancelReservation_r(vp);
2598 #else /* AFS_DEMAND_ATTACH_FS */
2600 VHold_r(register Volume * vp)
2604 LoadVolumeHeader(&error, vp);
2610 #endif /* AFS_DEMAND_ATTACH_FS */
2614 VHold(register Volume * vp)
2618 retVal = VHold_r(vp);
2625 /***************************************************/
2626 /* get and put volume routines */
2627 /***************************************************/
2630 * put back a heavyweight reference to a volume object.
2632 * @param[in] vp volume object pointer
2634 * @pre VOL_LOCK held
2636 * @post heavyweight volume reference put back.
2637 * depending on state, volume may have been taken offline,
2638 * detached, salvaged, freed, etc.
2640 * @internal volume package internal use only
2643 VPutVolume_r(register Volume * vp)
2645 assert(--vp->nUsers >= 0);
2646 if (vp->nUsers == 0) {
2648 ReleaseVolumeHeader(vp->header);
2649 #ifdef AFS_DEMAND_ATTACH_FS
2650 if (!VCheckDetach(vp)) {
2654 #else /* AFS_DEMAND_ATTACH_FS */
2656 #endif /* AFS_DEMAND_ATTACH_FS */
2661 VPutVolume(register Volume * vp)
2669 /* Get a pointer to an attached volume. The pointer is returned regardless
2670 of whether or not the volume is in service or on/off line. An error
2671 code, however, is returned with an indication of the volume's status */
2673 VGetVolume(Error * ec, Error * client_ec, VolId volumeId)
2677 retVal = GetVolume(ec, client_ec, volumeId, NULL, 0);
2683 VGetVolume_r(Error * ec, VolId volumeId)
2685 return GetVolume(ec, NULL, volumeId, NULL, 0);
2688 /* try to get a volume we've previously looked up */
2689 /* for demand attach fs, caller MUST NOT hold a ref count on vp */
2691 VGetVolumeByVp_r(Error * ec, Volume * vp)
2693 return GetVolume(ec, NULL, vp->hashid, vp, 0);
2696 /* private interface for getting a volume handle
2697 * volumeId must be provided.
2698 * hint is an optional parameter to speed up hash lookups
2699 * flags is not used at this time
2701 /* for demand attach fs, caller MUST NOT hold a ref count on hint */
2703 GetVolume(Error * ec, Error * client_ec, VolId volumeId, Volume * hint, int flags)
2706 /* pull this profiling/debugging code out of regular builds */
2708 #define VGET_CTR_INC(x) x++
2709 unsigned short V0 = 0, V1 = 0, V2 = 0, V3 = 0, V5 = 0, V6 =
2710 0, V7 = 0, V8 = 0, V9 = 0;
2711 unsigned short V10 = 0, V11 = 0, V12 = 0, V13 = 0, V14 = 0, V15 = 0;
2713 #define VGET_CTR_INC(x)
2715 #ifdef AFS_DEMAND_ATTACH_FS
2716 Volume *avp, * rvp = hint;
2720 * if VInit is zero, the volume package dynamic
2721 * data structures have not been initialized yet,
2722 * and we must immediately return an error
2728 *client_ec = VOFFLINE;
2733 #ifdef AFS_DEMAND_ATTACH_FS
2735 VCreateReservation_r(rvp);
2737 #endif /* AFS_DEMAND_ATTACH_FS */
2745 vp = VLookupVolume_r(ec, volumeId, vp);
2751 #ifdef AFS_DEMAND_ATTACH_FS
2752 if (rvp && (rvp != vp)) {
2753 /* break reservation on old vp */
2754 VCancelReservation_r(rvp);
2757 #endif /* AFS_DEMAND_ATTACH_FS */
2763 /* Until we have reached an initialization level of 2
2764 * we don't know whether this volume exists or not.
2765 * We can't sleep and retry later because before a volume
2766 * is attached, the caller tries to get it first. Just
2767 * return VOFFLINE and the caller can choose whether to
2768 * retry the command or not. */
2778 IncUInt64(&VStats.hdr_gets);
2780 #ifdef AFS_DEMAND_ATTACH_FS
2781 /* block if someone else is performing an exclusive op on this volume */
2784 VCreateReservation_r(rvp);
2786 VWaitExclusiveState_r(vp);
2788 /* short circuit with VNOVOL in the following circumstances:
2791 * - VOL_STATE_SHUTTING_DOWN
2793 if ((V_attachState(vp) == VOL_STATE_ERROR) ||
2794 (V_attachState(vp) == VOL_STATE_SHUTTING_DOWN) ||
2795 (V_attachState(vp) == VOL_STATE_GOING_OFFLINE)) {
2802 * short circuit with VOFFLINE in the following circumstances:
2804 * - VOL_STATE_UNATTACHED
2806 if (V_attachState(vp) == VOL_STATE_UNATTACHED) {
2807 if (vp->specialStatus) {
2808 *ec = vp->specialStatus;
2816 /* allowable states:
2822 if (vp->salvage.requested) {
2823 VUpdateSalvagePriority_r(vp);
2826 if (V_attachState(vp) == VOL_STATE_PREATTACHED) {
2827 avp = VAttachVolumeByVp_r(ec, vp, 0);
2830 /* VAttachVolumeByVp_r can return a pointer
2831 * != the vp passed to it under certain
2832 * conditions; make sure we don't leak
2833 * reservations if that happens */
2835 VCancelReservation_r(rvp);
2837 VCreateReservation_r(rvp);
2847 if (!vp->pending_vol_op) {
2862 if ((V_attachState(vp) == VOL_STATE_SALVAGING) ||
2863 (*ec == VSALVAGING)) {
2865 /* see CheckVnode() in afsfileprocs.c for an explanation
2866 * of this error code logic */
2867 afs_uint32 now = FT_ApproxTime();
2868 if ((vp->stats.last_salvage + (10 * 60)) >= now) {
2871 *client_ec = VRESTARTING;
2880 LoadVolumeHeader(ec, vp);
2883 /* Only log the error if it was a totally unexpected error. Simply
2884 * a missing inode is likely to be caused by the volume being deleted */
2885 if (errno != ENXIO || LogLevel)
2886 Log("Volume %u: couldn't reread volume header\n",
2888 #ifdef AFS_DEMAND_ATTACH_FS
2889 if (programType == fileServer) {
2890 VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, VOL_SALVAGE_INVALIDATE_HEADER);
2895 #else /* AFS_DEMAND_ATTACH_FS */
2898 #endif /* AFS_DEMAND_ATTACH_FS */
2902 #ifdef AFS_DEMAND_ATTACH_FS
2904 * this test MUST happen after the volume header is loaded
2907 /* only valid before/during demand attachment */
2908 assert(!vp->pending_vol_op || vp->pending_vol_op != FSSYNC_VolOpRunningUnknown);
2910 /* deny getvolume due to running mutually exclusive vol op */
2911 if (vp->pending_vol_op && vp->pending_vol_op->vol_op_state==FSSYNC_VolOpRunningOffline) {
2913 * volume cannot remain online during this volume operation.
2916 if (vp->specialStatus) {
2918 * special status codes outrank normal VOFFLINE code
2920 *ec = vp->specialStatus;
2922 *client_ec = vp->specialStatus;
2926 /* see CheckVnode() in afsfileprocs.c for an explanation
2927 * of this error code logic */
2928 afs_uint32 now = FT_ApproxTime();
2929 if ((vp->stats.last_vol_op + (10 * 60)) >= now) {
2932 *client_ec = VRESTARTING;
2937 VChangeState_r(vp, VOL_STATE_UNATTACHED);
2938 FreeVolumeHeader(vp);
2942 #endif /* AFS_DEMAND_ATTACH_FS */
2945 if (vp->shuttingDown) {
2952 if (programType == fileServer) {
2954 if (vp->goingOffline) {
2956 #ifdef AFS_DEMAND_ATTACH_FS
2957 /* wait for the volume to go offline */
2958 if (V_attachState(vp) == VOL_STATE_GOING_OFFLINE) {
2959 VWaitStateChange_r(vp);
2961 #elif defined(AFS_PTHREAD_ENV)
2962 VOL_CV_WAIT(&vol_put_volume_cond);
2963 #else /* AFS_PTHREAD_ENV */
2964 LWP_WaitProcess(VPutVolume);
2965 #endif /* AFS_PTHREAD_ENV */
2968 if (vp->specialStatus) {
2970 *ec = vp->specialStatus;
2971 } else if (V_inService(vp) == 0 || V_blessed(vp) == 0) {
2974 } else if (V_inUse(vp) == 0) {
2985 #ifdef AFS_DEMAND_ATTACH_FS
2986 /* if no error, bump nUsers */
2989 VLRU_UpdateAccess_r(vp);
2992 VCancelReservation_r(rvp);
2995 if (client_ec && !*client_ec) {
2998 #else /* AFS_DEMAND_ATTACH_FS */
2999 /* if no error, bump nUsers */
3006 #endif /* AFS_DEMAND_ATTACH_FS */
3014 /***************************************************/
3015 /* Volume offline/detach routines */
3016 /***************************************************/
3018 /* caller MUST hold a heavyweight ref on vp */
3019 #ifdef AFS_DEMAND_ATTACH_FS
3021 VTakeOffline_r(register Volume * vp)
3025 assert(vp->nUsers > 0);
3026 assert(programType == fileServer);
3028 VCreateReservation_r(vp);
3029 VWaitExclusiveState_r(vp);
3031 vp->goingOffline = 1;
3032 V_needsSalvaged(vp) = 1;
3034 VRequestSalvage_r(&error, vp, SALVSYNC_ERROR, 0);
3035 VCancelReservation_r(vp);
3037 #else /* AFS_DEMAND_ATTACH_FS */
3039 VTakeOffline_r(register Volume * vp)
3041 assert(vp->nUsers > 0);
3042 assert(programType == fileServer);
3044 vp->goingOffline = 1;
3045 V_needsSalvaged(vp) = 1;
3047 #endif /* AFS_DEMAND_ATTACH_FS */
3050 VTakeOffline(register Volume * vp)
3058 * force a volume offline.
3060 * @param[in] vp volume object pointer
3061 * @param[in] flags flags (see note below)
3063 * @note the flag VOL_FORCEOFF_NOUPDATE is a recursion control flag
3064 * used when VUpdateVolume_r needs to call VForceOffline_r
3065 * (which in turn would normally call VUpdateVolume_r)
3067 * @see VUpdateVolume_r
3069 * @pre VOL_LOCK must be held.
3070 * for DAFS, caller must hold ref.
3072 * @note for DAFS, it _is safe_ to call this function from an
3075 * @post needsSalvaged flag is set.
3076 * for DAFS, salvage is requested.
3077 * no further references to the volume through the volume
3078 * package will be honored.
3079 * all file descriptor and vnode caches are invalidated.
3081 * @warning this is a heavy-handed interface. it results in
3082 * a volume going offline regardless of the current
3083 * reference count state.
3085 * @internal volume package internal use only
3088 VForceOffline_r(Volume * vp, int flags)
3092 #ifdef AFS_DEMAND_ATTACH_FS
3093 VChangeState_r(vp, VOL_STATE_ERROR);
3098 strcpy(V_offlineMessage(vp),
3099 "Forced offline due to internal error: volume needs to be salvaged");
3100 Log("Volume %u forced offline: it needs salvaging!\n", V_id(vp));
3103 vp->goingOffline = 0;
3104 V_needsSalvaged(vp) = 1;
3105 if (!(flags & VOL_FORCEOFF_NOUPDATE)) {
3106 VUpdateVolume_r(&error, vp, VOL_UPDATE_NOFORCEOFF);
3109 #ifdef AFS_DEMAND_ATTACH_FS
3110 VRequestSalvage_r(&error, vp, SALVSYNC_ERROR, VOL_SALVAGE_INVALIDATE_HEADER);
3111 #endif /* AFS_DEMAND_ATTACH_FS */
3113 #ifdef AFS_PTHREAD_ENV
3114 assert(pthread_cond_broadcast(&vol_put_volume_cond) == 0);
3115 #else /* AFS_PTHREAD_ENV */
3116 LWP_NoYieldSignal(VPutVolume);
3117 #endif /* AFS_PTHREAD_ENV */
3119 VReleaseVolumeHandles_r(vp);
3123 * force a volume offline.
3125 * @param[in] vp volume object pointer
3127 * @see VForceOffline_r
3130 VForceOffline(Volume * vp)
3133 VForceOffline_r(vp, 0);
3137 /* The opposite of VAttachVolume. The volume header is written to disk, with
3138 the inUse bit turned off. A copy of the header is maintained in memory,
3139 however (which is why this is VOffline, not VDetach).
3142 VOffline_r(Volume * vp, char *message)
3145 VolumeId vid = V_id(vp);
3147 assert(programType != volumeUtility);
3152 if (V_offlineMessage(vp)[0] == '\0')
3153 strncpy(V_offlineMessage(vp), message, sizeof(V_offlineMessage(vp)));
3154 V_offlineMessage(vp)[sizeof(V_offlineMessage(vp)) - 1] = '\0';
3156 vp->goingOffline = 1;
3157 #ifdef AFS_DEMAND_ATTACH_FS
3158 VChangeState_r(vp, VOL_STATE_GOING_OFFLINE);
3159 VCreateReservation_r(vp);
3162 /* wait for the volume to go offline */
3163 if (V_attachState(vp) == VOL_STATE_GOING_OFFLINE) {
3164 VWaitStateChange_r(vp);
3166 VCancelReservation_r(vp);
3167 #else /* AFS_DEMAND_ATTACH_FS */
3169 vp = VGetVolume_r(&error, vid); /* Wait for it to go offline */
3170 if (vp) /* In case it was reattached... */
3172 #endif /* AFS_DEMAND_ATTACH_FS */
3175 #ifdef AFS_DEMAND_ATTACH_FS
3177 * Take a volume offline in order to perform a volume operation.
3179 * @param[inout] ec address in which to store error code
3180 * @param[in] vp volume object pointer
3181 * @param[in] message volume offline status message
3184 * - VOL_LOCK is held
3185 * - caller MUST hold a heavyweight ref on vp
3188 * - volume is taken offline
3189 * - if possible, volume operation is promoted to running state
3190 * - on failure, *ec is set to nonzero
3192 * @note Although this function does not return any value, it may
3193 * still fail to promote our pending volume operation to
3194 * a running state. Any caller MUST check the value of *ec,
3195 * and MUST NOT blindly assume success.
3197 * @warning if the caller does not hold a lightweight ref on vp,
3198 * then it MUST NOT reference vp after this function
3199 * returns to the caller.
3201 * @internal volume package internal use only
3204 VOfflineForVolOp_r(Error *ec, Volume *vp, char *message)
3206 assert(vp->pending_vol_op);
3212 if (V_offlineMessage(vp)[0] == '\0')
3213 strncpy(V_offlineMessage(vp), message, sizeof(V_offlineMessage(vp)));
3214 V_offlineMessage(vp)[sizeof(V_offlineMessage(vp)) - 1] = '\0';
3216 vp->goingOffline = 1;
3217 VChangeState_r(vp, VOL_STATE_GOING_OFFLINE);
3218 VCreateReservation_r(vp);
3221 /* Wait for the volume to go offline */
3222 while (!VIsOfflineState(V_attachState(vp))) {
3223 /* do not give corrupted volumes to the volserver */
3224 if (vp->salvage.requested && vp->pending_vol_op->com.programType != salvageServer) {
3228 VWaitStateChange_r(vp);
3232 VCancelReservation_r(vp);
3234 #endif /* AFS_DEMAND_ATTACH_FS */
3237 VOffline(Volume * vp, char *message)
3240 VOffline_r(vp, message);
3244 /* This gets used for the most part by utility routines that don't want
3245 * to keep all the volume headers around. Generally, the file server won't
3246 * call this routine, because then the offline message in the volume header
3247 * (or other information) won't be available to clients. For NAMEI, also
3248 * close the file handles. However, the fileserver does call this during
3249 * an attach following a volume operation.
3252 VDetachVolume_r(Error * ec, Volume * vp)
3255 struct DiskPartition64 *tpartp;
3256 int notifyServer = 0;
3257 int useDone = FSYNC_VOL_ON;
3259 *ec = 0; /* always "succeeds" */
3260 if (programType == volumeUtility) {
3261 notifyServer = vp->needsPutBack;
3262 if (V_destroyMe(vp) == DESTROY_ME)
3263 useDone = FSYNC_VOL_DONE;
3264 #ifdef AFS_DEMAND_ATTACH_FS
3265 else if (!V_blessed(vp) || !V_inService(vp))
3266 useDone = FSYNC_VOL_LEAVE_OFF;
3269 tpartp = vp->partition;
3271 DeleteVolumeFromHashTable(vp);
3272 vp->shuttingDown = 1;
3273 #ifdef AFS_DEMAND_ATTACH_FS
3274 DeleteVolumeFromVByPList_r(vp);
3276 VChangeState_r(vp, VOL_STATE_SHUTTING_DOWN);
3278 if (programType != fileServer)
3280 #endif /* AFS_DEMAND_ATTACH_FS */
3282 /* Will be detached sometime in the future--this is OK since volume is offline */
3284 /* XXX the following code should really be moved to VCheckDetach() since the volume
3285 * is not technically detached until the refcounts reach zero
3287 #ifdef FSSYNC_BUILD_CLIENT
3288 if (programType == volumeUtility && notifyServer) {
3290 * Note: The server is not notified in the case of a bogus volume
3291 * explicitly to make it possible to create a volume, do a partial
3292 * restore, then abort the operation without ever putting the volume
3293 * online. This is essential in the case of a volume move operation
3294 * between two partitions on the same server. In that case, there
3295 * would be two instances of the same volume, one of them bogus,
3296 * which the file server would attempt to put on line
3298 FSYNC_VolOp(volume, tpartp->name, useDone, 0, NULL);
3299 /* XXX this code path is only hit by volume utilities, thus
3300 * V_BreakVolumeCallbacks will always be NULL. if we really
3301 * want to break callbacks in this path we need to use FSYNC_VolOp() */
3303 /* Dettaching it so break all callbacks on it */
3304 if (V_BreakVolumeCallbacks) {
3305 Log("volume %u detached; breaking all call backs\n", volume);
3306 (*V_BreakVolumeCallbacks) (volume);
3310 #endif /* FSSYNC_BUILD_CLIENT */
3314 VDetachVolume(Error * ec, Volume * vp)
3317 VDetachVolume_r(ec, vp);
3322 /***************************************************/
3323 /* Volume fd/inode handle closing routines */
3324 /***************************************************/
3326 /* For VDetachVolume, we close all cached file descriptors, but keep
3327 * the Inode handles in case we need to read from a busy volume.
3329 /* for demand attach, caller MUST hold ref count on vp */
3331 VCloseVolumeHandles_r(Volume * vp)
3333 #ifdef AFS_DEMAND_ATTACH_FS
3334 VolState state_save;
3336 state_save = VChangeState_r(vp, VOL_STATE_OFFLINING);
3341 * XXX need to investigate whether we can perform
3342 * DFlushVolume outside of vol_glock_mutex...
3344 * VCloseVnodeFiles_r drops the glock internally */
3345 DFlushVolume(V_id(vp));
3346 VCloseVnodeFiles_r(vp);
3348 #ifdef AFS_DEMAND_ATTACH_FS
3352 /* Too time consuming and unnecessary for the volserver */
3353 if (programType != volumeUtility) {
3354 IH_CONDSYNC(vp->vnodeIndex[vLarge].handle);
3355 IH_CONDSYNC(vp->vnodeIndex[vSmall].handle);
3356 IH_CONDSYNC(vp->diskDataHandle);
3358 IH_CONDSYNC(vp->linkHandle);
3359 #endif /* AFS_NT40_ENV */
3362 IH_REALLYCLOSE(vp->vnodeIndex[vLarge].handle);
3363 IH_REALLYCLOSE(vp->vnodeIndex[vSmall].handle);
3364 IH_REALLYCLOSE(vp->diskDataHandle);
3365 IH_REALLYCLOSE(vp->linkHandle);
3367 #ifdef AFS_DEMAND_ATTACH_FS
3369 VChangeState_r(vp, state_save);
3373 /* For both VForceOffline and VOffline, we close all relevant handles.
3374 * For VOffline, if we re-attach the volume, the files may possible be
3375 * different than before.
3377 /* for demand attach, caller MUST hold a ref count on vp */
3379 VReleaseVolumeHandles_r(Volume * vp)
3381 #ifdef AFS_DEMAND_ATTACH_FS
3382 VolState state_save;
3384 state_save = VChangeState_r(vp, VOL_STATE_DETACHING);
3387 /* XXX need to investigate whether we can perform
3388 * DFlushVolume outside of vol_glock_mutex... */
3389 DFlushVolume(V_id(vp));
3391 VReleaseVnodeFiles_r(vp); /* releases the glock internally */
3393 #ifdef AFS_DEMAND_ATTACH_FS
3397 /* Too time consuming and unnecessary for the volserver */
3398 if (programType != volumeUtility) {
3399 IH_CONDSYNC(vp->vnodeIndex[vLarge].handle);
3400 IH_CONDSYNC(vp->vnodeIndex[vSmall].handle);
3401 IH_CONDSYNC(vp->diskDataHandle);
3403 IH_CONDSYNC(vp->linkHandle);
3404 #endif /* AFS_NT40_ENV */
3407 IH_RELEASE(vp->vnodeIndex[vLarge].handle);
3408 IH_RELEASE(vp->vnodeIndex[vSmall].handle);
3409 IH_RELEASE(vp->diskDataHandle);
3410 IH_RELEASE(vp->linkHandle);
3412 #ifdef AFS_DEMAND_ATTACH_FS
3414 VChangeState_r(vp, state_save);
3419 /***************************************************/
3420 /* Volume write and fsync routines */
3421 /***************************************************/
3424 VUpdateVolume_r(Error * ec, Volume * vp, int flags)
3426 #ifdef AFS_DEMAND_ATTACH_FS
3427 VolState state_save;
3429 if (flags & VOL_UPDATE_WAIT) {
3430 VCreateReservation_r(vp);
3431 VWaitExclusiveState_r(vp);
3436 if (programType == fileServer)
3438 (V_inUse(vp) ? V_nextVnodeUnique(vp) +
3439 200 : V_nextVnodeUnique(vp));
3441 #ifdef AFS_DEMAND_ATTACH_FS
3442 state_save = VChangeState_r(vp, VOL_STATE_UPDATING);
3446 WriteVolumeHeader_r(ec, vp);
3448 #ifdef AFS_DEMAND_ATTACH_FS
3450 VChangeState_r(vp, state_save);
3451 if (flags & VOL_UPDATE_WAIT) {
3452 VCancelReservation_r(vp);
3457 Log("VUpdateVolume: error updating volume header, volume %u (%s)\n",
3458 V_id(vp), V_name(vp));
3459 /* try to update on-disk header,
3460 * while preventing infinite recursion */
3461 if (!(flags & VOL_UPDATE_NOFORCEOFF)) {
3462 VForceOffline_r(vp, VOL_FORCEOFF_NOUPDATE);
3468 VUpdateVolume(Error * ec, Volume * vp)
3471 VUpdateVolume_r(ec, vp, VOL_UPDATE_WAIT);
3476 VSyncVolume_r(Error * ec, Volume * vp, int flags)
3480 #ifdef AFS_DEMAND_ATTACH_FS
3481 VolState state_save;
3484 if (flags & VOL_SYNC_WAIT) {
3485 VUpdateVolume_r(ec, vp, VOL_UPDATE_WAIT);
3487 VUpdateVolume_r(ec, vp, 0);
3490 #ifdef AFS_DEMAND_ATTACH_FS
3491 state_save = VChangeState_r(vp, VOL_STATE_UPDATING);
3494 fdP = IH_OPEN(V_diskDataHandle(vp));
3495 assert(fdP != NULL);
3496 code = FDH_SYNC(fdP);
3499 #ifdef AFS_DEMAND_ATTACH_FS
3501 VChangeState_r(vp, state_save);
3507 VSyncVolume(Error * ec, Volume * vp)
3510 VSyncVolume_r(ec, vp, VOL_SYNC_WAIT);
3515 /***************************************************/
3516 /* Volume dealloaction routines */
3517 /***************************************************/
3519 #ifdef AFS_DEMAND_ATTACH_FS
3521 FreeVolume(Volume * vp)
3523 /* free the heap space, iff it's safe.
3524 * otherwise, pull it out of the hash table, so it
3525 * will get deallocated when all refs to it go away */
3526 if (!VCheckFree(vp)) {
3527 DeleteVolumeFromHashTable(vp);
3528 DeleteVolumeFromVByPList_r(vp);
3530 /* make sure we invalidate the header cache entry */
3531 FreeVolumeHeader(vp);
3534 #endif /* AFS_DEMAND_ATTACH_FS */
3537 ReallyFreeVolume(Volume * vp)
3542 #ifdef AFS_DEMAND_ATTACH_FS
3544 VChangeState_r(vp, VOL_STATE_FREED);
3545 if (vp->pending_vol_op)
3546 free(vp->pending_vol_op);
3547 #endif /* AFS_DEMAND_ATTACH_FS */
3548 for (i = 0; i < nVNODECLASSES; i++)
3549 if (vp->vnodeIndex[i].bitmap)
3550 free(vp->vnodeIndex[i].bitmap);
3551 FreeVolumeHeader(vp);
3552 #ifndef AFS_DEMAND_ATTACH_FS
3553 DeleteVolumeFromHashTable(vp);
3554 #endif /* AFS_DEMAND_ATTACH_FS */
3558 /* check to see if we should shutdown this volume
3559 * returns 1 if volume was freed, 0 otherwise */
3560 #ifdef AFS_DEMAND_ATTACH_FS
3562 VCheckDetach(register Volume * vp)
3567 if (vp->nUsers || vp->nWaiters)
3570 if (vp->shuttingDown) {
3572 if ((programType != fileServer) &&
3573 (V_inUse(vp) == programType) &&
3574 ((V_checkoutMode(vp) == V_VOLUPD) ||
3575 (V_checkoutMode(vp) == V_SECRETLY) ||
3576 ((V_checkoutMode(vp) == V_CLONE) &&
3577 (VolumeWriteable(vp))))) {
3579 VUpdateVolume_r(&ec, vp, VOL_UPDATE_NOFORCEOFF);
3581 Log("VCheckDetach: volume header update for volume %u "
3582 "failed with errno %d\n", vp->hashid, errno);
3585 VReleaseVolumeHandles_r(vp);
3587 ReallyFreeVolume(vp);
3588 if (programType == fileServer) {
3589 assert(pthread_cond_broadcast(&vol_put_volume_cond) == 0);
3594 #else /* AFS_DEMAND_ATTACH_FS */
3596 VCheckDetach(register Volume * vp)
3604 if (vp->shuttingDown) {
3606 if ((programType != fileServer) &&
3607 (V_inUse(vp) == programType) &&
3608 ((V_checkoutMode(vp) == V_VOLUPD) ||
3609 (V_checkoutMode(vp) == V_SECRETLY) ||
3610 ((V_checkoutMode(vp) == V_CLONE) &&
3611 (VolumeWriteable(vp))))) {
3613 VUpdateVolume_r(&ec, vp, VOL_UPDATE_NOFORCEOFF);
3615 Log("VCheckDetach: volume header update for volume %u failed with errno %d\n",
3619 VReleaseVolumeHandles_r(vp);
3620 ReallyFreeVolume(vp);
3621 if (programType == fileServer) {
3622 #if defined(AFS_PTHREAD_ENV)
3623 assert(pthread_cond_broadcast(&vol_put_volume_cond) == 0);
3624 #else /* AFS_PTHREAD_ENV */
3625 LWP_NoYieldSignal(VPutVolume);
3626 #endif /* AFS_PTHREAD_ENV */
3631 #endif /* AFS_DEMAND_ATTACH_FS */
3633 /* check to see if we should offline this volume
3634 * return 1 if volume went offline, 0 otherwise */
3635 #ifdef AFS_DEMAND_ATTACH_FS
3637 VCheckOffline(register Volume * vp)
3641 if (vp->goingOffline && !vp->nUsers) {
3643 assert(programType == fileServer);
3644 assert((V_attachState(vp) != VOL_STATE_ATTACHED) &&
3645 (V_attachState(vp) != VOL_STATE_FREED) &&
3646 (V_attachState(vp) != VOL_STATE_PREATTACHED) &&
3647 (V_attachState(vp) != VOL_STATE_UNATTACHED));
3651 * VOL_STATE_GOING_OFFLINE
3652 * VOL_STATE_SHUTTING_DOWN
3653 * VIsErrorState(V_attachState(vp))
3654 * VIsExclusiveState(V_attachState(vp))
3657 VCreateReservation_r(vp);
3658 VChangeState_r(vp, VOL_STATE_OFFLINING);
3661 /* must clear the goingOffline flag before we drop the glock */
3662 vp->goingOffline = 0;
3667 /* perform async operations */
3668 VUpdateVolume_r(&error, vp, 0);
3669 VCloseVolumeHandles_r(vp);
3672 Log("VOffline: Volume %u (%s) is now offline", V_id(vp),
3674 if (V_offlineMessage(vp)[0])
3675 Log(" (%s)", V_offlineMessage(vp));
3679 /* invalidate the volume header cache entry */
3680 FreeVolumeHeader(vp);
3682 /* if nothing changed state to error or salvaging,
3683 * drop state to unattached */
3684 if (!VIsErrorState(V_attachState(vp))) {
3685 VChangeState_r(vp, VOL_STATE_UNATTACHED);
3687 VCancelReservation_r(vp);
3688 /* no usage of vp is safe beyond this point */
3692 #else /* AFS_DEMAND_ATTACH_FS */
3694 VCheckOffline(register Volume * vp)
3698 if (vp->goingOffline && !vp->nUsers) {
3700 assert(programType == fileServer);
3703 vp->goingOffline = 0;
3705 VUpdateVolume_r(&error, vp, 0);
3706 VCloseVolumeHandles_r(vp);
3708 Log("VOffline: Volume %u (%s) is now offline", V_id(vp),
3710 if (V_offlineMessage(vp)[0])
3711 Log(" (%s)", V_offlineMessage(vp));
3714 FreeVolumeHeader(vp);
3715 #ifdef AFS_PTHREAD_ENV
3716 assert(pthread_cond_broadcast(&vol_put_volume_cond) == 0);
3717 #else /* AFS_PTHREAD_ENV */
3718 LWP_NoYieldSignal(VPutVolume);
3719 #endif /* AFS_PTHREAD_ENV */
3723 #endif /* AFS_DEMAND_ATTACH_FS */
3725 /***************************************************/
3726 /* demand attach fs ref counting routines */
3727 /***************************************************/
3729 #ifdef AFS_DEMAND_ATTACH_FS
3730 /* the following two functions handle reference counting for
3731 * asynchronous operations on volume structs.
3733 * their purpose is to prevent a VDetachVolume or VShutdown
3734 * from free()ing the Volume struct during an async i/o op */
3736 /* register with the async volume op ref counter */
3737 /* VCreateReservation_r moved into inline code header because it
3738 * is now needed in vnode.c -- tkeiser 11/20/2007
3742 * decrement volume-package internal refcount.
3744 * @param vp volume object pointer
3746 * @internal volume package internal use only
3749 * @arg VOL_LOCK is held
3750 * @arg lightweight refcount held
3752 * @post volume waiters refcount is decremented; volume may
3753 * have been deallocated/shutdown/offlined/salvaged/
3754 * whatever during the process
3756 * @warning once you have tossed your last reference (you can acquire
3757 * lightweight refs recursively) it is NOT SAFE to reference
3758 * a volume object pointer ever again
3760 * @see VCreateReservation_r
3762 * @note DEMAND_ATTACH_FS only
3765 VCancelReservation_r(Volume * vp)
3767 assert(--vp->nWaiters >= 0);
3768 if (vp->nWaiters == 0) {
3770 if (!VCheckDetach(vp)) {
3777 /* check to see if we should free this volume now
3778 * return 1 if volume was freed, 0 otherwise */
3780 VCheckFree(Volume * vp)
3783 if ((vp->nUsers == 0) &&
3784 (vp->nWaiters == 0) &&
3785 !(V_attachFlags(vp) & (VOL_IN_HASH |
3789 ReallyFreeVolume(vp);
3794 #endif /* AFS_DEMAND_ATTACH_FS */
3797 /***************************************************/
3798 /* online volume operations routines */
3799 /***************************************************/
3801 #ifdef AFS_DEMAND_ATTACH_FS
3803 * register a volume operation on a given volume.
3805 * @param[in] vp volume object
3806 * @param[in] vopinfo volume operation info object
3808 * @pre VOL_LOCK is held
3810 * @post volume operation info object attached to volume object.
3811 * volume operation statistics updated.
3813 * @note by "attached" we mean a copy of the passed in object is made
3815 * @internal volume package internal use only
3818 VRegisterVolOp_r(Volume * vp, FSSYNC_VolOp_info * vopinfo)
3820 FSSYNC_VolOp_info * info;
3822 /* attach a vol op info node to the volume struct */
3823 info = (FSSYNC_VolOp_info *) malloc(sizeof(FSSYNC_VolOp_info));
3824 assert(info != NULL);
3825 memcpy(info, vopinfo, sizeof(FSSYNC_VolOp_info));
3826 vp->pending_vol_op = info;
3829 vp->stats.last_vol_op = FT_ApproxTime();
3830 vp->stats.vol_ops++;
3831 IncUInt64(&VStats.vol_ops);
3837 * deregister the volume operation attached to this volume.
3839 * @param[in] vp volume object pointer
3841 * @pre VOL_LOCK is held
3843 * @post the volume operation info object is detached from the volume object
3845 * @internal volume package internal use only
3848 VDeregisterVolOp_r(Volume * vp)
3850 if (vp->pending_vol_op) {
3851 free(vp->pending_vol_op);
3852 vp->pending_vol_op = NULL;
3856 #endif /* AFS_DEMAND_ATTACH_FS */
3859 * determine whether it is safe to leave a volume online during
3860 * the volume operation described by the vopinfo object.
3862 * @param[in] vp volume object
3863 * @param[in] vopinfo volume operation info object
3865 * @return whether it is safe to leave volume online
3866 * @retval 0 it is NOT SAFE to leave the volume online
3867 * @retval 1 it is safe to leave the volume online during the operation
3870 * @arg VOL_LOCK is held
3871 * @arg disk header attached to vp (heavyweight ref on vp will guarantee
3872 * this condition is met)
3874 * @internal volume package internal use only
3877 VVolOpLeaveOnline_r(Volume * vp, FSSYNC_VolOp_info * vopinfo)
3879 return (vopinfo->vol_op_state == FSSYNC_VolOpRunningOnline ||
3880 (vopinfo->com.command == FSYNC_VOL_NEEDVOLUME &&
3881 (vopinfo->com.reason == V_READONLY ||
3882 (!VolumeWriteable(vp) &&
3883 (vopinfo->com.reason == V_CLONE ||
3884 vopinfo->com.reason == V_DUMP)))));
3888 * determine whether VBUSY should be set during this volume operation.
3890 * @param[in] vp volume object
3891 * @param[in] vopinfo volume operation info object
3893 * @return whether VBUSY should be set
3894 * @retval 0 VBUSY does NOT need to be set
3895 * @retval 1 VBUSY SHOULD be set
3897 * @pre VOL_LOCK is held
3899 * @internal volume package internal use only
3902 VVolOpSetVBusy_r(Volume * vp, FSSYNC_VolOp_info * vopinfo)
3904 return ((vopinfo->com.command == FSYNC_VOL_OFF &&
3905 vopinfo->com.reason == FSYNC_SALVAGE) ||
3906 (vopinfo->com.command == FSYNC_VOL_NEEDVOLUME &&
3907 (vopinfo->com.reason == V_CLONE ||
3908 vopinfo->com.reason == V_DUMP)));
3912 /***************************************************/
3913 /* online salvager routines */
3914 /***************************************************/
3915 #if defined(AFS_DEMAND_ATTACH_FS)
3916 #define SALVAGE_PRIO_UPDATE_INTERVAL 3 /**< number of seconds between prio updates */
3917 #define SALVAGE_COUNT_MAX 16 /**< number of online salvages we
3918 * allow before moving the volume
3919 * into a permanent error state
3921 * once this threshold is reached,
3922 * the operator will have to manually
3923 * issue a 'bos salvage' to bring
3924 * the volume back online
3928 * check whether a salvage needs to be performed on this volume.
3930 * @param[in] vp pointer to volume object
3932 * @return status code
3933 * @retval 0 no salvage scheduled
3934 * @retval 1 a salvage has been scheduled with the salvageserver
3936 * @pre VOL_LOCK is held
3938 * @post if salvage request flag is set and nUsers and nWaiters are zero,
3939 * then a salvage will be requested
3941 * @note this is one of the event handlers called by VCancelReservation_r
3943 * @see VCancelReservation_r
3945 * @internal volume package internal use only.
3948 VCheckSalvage(register Volume * vp)
3951 #ifdef SALVSYNC_BUILD_CLIENT
3952 if (vp->nUsers || vp->nWaiters)
3954 if (vp->salvage.requested) {
3955 VScheduleSalvage_r(vp);
3958 #endif /* SALVSYNC_BUILD_CLIENT */
3963 * request volume salvage.
3965 * @param[out] ec computed client error code
3966 * @param[in] vp volume object pointer
3967 * @param[in] reason reason code (passed to salvageserver via SALVSYNC)
3968 * @param[in] flags see flags note below
3971 * VOL_SALVAGE_INVALIDATE_HEADER causes volume header cache entry
3972 * to be invalidated.
3974 * @pre VOL_LOCK is held.
3976 * @post volume state is changed.
3977 * for fileserver, salvage will be requested once refcount reaches zero.
3979 * @return operation status code
3980 * @retval 0 volume salvage will occur
3981 * @retval 1 volume salvage could not be scheduled
3983 * @note DAFS fileserver only
3985 * @note this call does not synchronously schedule a volume salvage. rather,
3986 * it sets volume state so that when volume refcounts reach zero, a
3987 * volume salvage will occur. by "refcounts", we mean both nUsers and
3988 * nWaiters must be zero.
3990 * @internal volume package internal use only.
3993 VRequestSalvage_r(Error * ec, Volume * vp, int reason, int flags)
3997 * for DAFS volume utilities, transition to error state
3998 * (at some point in the future, we should consider
3999 * making volser talk to salsrv)
4001 if (programType != fileServer) {
4002 VChangeState_r(vp, VOL_STATE_ERROR);
4007 if (!vp->salvage.requested) {
4008 vp->salvage.requested = 1;
4009 vp->salvage.reason = reason;
4010 vp->stats.last_salvage = FT_ApproxTime();
4011 if (VIsSalvager(V_inUse(vp))) {
4012 Log("VRequestSalvage: volume %u appears to be salvaging, but we\n", vp->hashid);
4013 Log(" didn't request a salvage. Forcing it offline waiting for the\n");
4014 Log(" salvage to finish; if you are sure no salvage is running,\n");
4015 Log(" run a salvage manually.\n");
4017 /* make sure neither VScheduleSalvage_r nor
4018 * VUpdateSalvagePriority_r try to schedule another salvage */
4019 vp->salvage.requested = vp->salvage.scheduled = 0;
4021 /* these stats aren't correct, but doing this makes them
4022 * slightly closer to being correct */
4023 vp->stats.salvages++;
4024 vp->stats.last_salvage_req = FT_ApproxTime();
4025 IncUInt64(&VStats.salvages);
4027 VChangeState_r(vp, VOL_STATE_ERROR);
4031 } else if (vp->stats.salvages < SALVAGE_COUNT_MAX) {
4032 VChangeState_r(vp, VOL_STATE_SALVAGING);
4035 Log("VRequestSalvage: volume %u online salvaged too many times; forced offline.\n", vp->hashid);
4036 VChangeState_r(vp, VOL_STATE_ERROR);
4040 if (flags & VOL_SALVAGE_INVALIDATE_HEADER) {
4041 /* Instead of ReleaseVolumeHeader, we do FreeVolumeHeader()
4042 so that the the next VAttachVolumeByVp_r() invocation
4043 of attach2() will pull in a cached header
4044 entry and fail, then load a fresh one from disk and attach
4047 FreeVolumeHeader(vp);
4054 * update salvageserver scheduling priority for a volume.
4056 * @param[in] vp pointer to volume object
4058 * @return operation status
4060 * @retval 1 request denied, or SALVSYNC communications failure
4062 * @pre VOL_LOCK is held.
4064 * @post in-core salvage priority counter is incremented. if at least
4065 * SALVAGE_PRIO_UPDATE_INTERVAL seconds have elapsed since the
4066 * last SALVSYNC_RAISEPRIO request, we contact the salvageserver
4067 * to update its priority queue. if no salvage is scheduled,
4068 * this function is a no-op.
4070 * @note DAFS fileserver only
4072 * @note this should be called whenever a VGetVolume fails due to a
4073 * pending salvage request
4075 * @todo should set exclusive state and drop glock around salvsync call
4077 * @internal volume package internal use only.
4080 VUpdateSalvagePriority_r(Volume * vp)
4085 #ifdef SALVSYNC_BUILD_CLIENT
4087 now = FT_ApproxTime();
4089 /* update the salvageserver priority queue occasionally so that
4090 * frequently requested volumes get moved to the head of the queue
4092 if ((vp->salvage.scheduled) &&
4093 (vp->stats.last_salvage_req < (now-SALVAGE_PRIO_UPDATE_INTERVAL))) {
4094 code = SALVSYNC_SalvageVolume(vp->hashid,
4095 VPartitionPath(vp->partition),
4100 vp->stats.last_salvage_req = now;
4101 if (code != SYNC_OK) {
4105 #endif /* SALVSYNC_BUILD_CLIENT */
4111 * schedule a salvage with the salvage server.
4113 * @param[in] vp pointer to volume object
4115 * @return operation status
4116 * @retval 0 salvage scheduled successfully
4117 * @retval 1 salvage not scheduled, or SALVSYNC com error
4120 * @arg VOL_LOCK is held.
4121 * @arg nUsers and nWaiters should be zero.
4123 * @post salvageserver is sent a salvage request
4125 * @note DAFS fileserver only
4127 * @internal volume package internal use only.
4130 VScheduleSalvage_r(Volume * vp)
4133 #ifdef SALVSYNC_BUILD_CLIENT
4134 VolState state_save;
4135 VThreadOptions_t * thread_opts;
4138 if (vp->nWaiters || vp->nUsers) {
4142 /* prevent endless salvage,attach,salvage,attach,... loops */
4143 if (vp->stats.salvages >= SALVAGE_COUNT_MAX)
4147 * don't perform salvsync ops on certain threads
4149 thread_opts = pthread_getspecific(VThread_key);
4150 if (thread_opts == NULL) {
4151 thread_opts = &VThread_defaults;
4153 if (thread_opts->disallow_salvsync) {
4158 * XXX the scheduling process should really be done asynchronously
4159 * to avoid fssync deadlocks
4161 if (!vp->salvage.scheduled) {
4162 /* if we haven't previously scheduled a salvage, do so now
4164 * set the volume to an exclusive state and drop the lock
4165 * around the SALVSYNC call
4167 * note that we do NOT acquire a reservation here -- doing so
4168 * could result in unbounded recursion
4170 strlcpy(partName, VPartitionPath(vp->partition), sizeof(partName));
4171 state_save = VChangeState_r(vp, VOL_STATE_SALVSYNC_REQ);
4174 /* can't use V_id() since there's no guarantee
4175 * we have the disk data header at this point */
4176 code = SALVSYNC_SalvageVolume(vp->hashid,
4183 VChangeState_r(vp, state_save);
4185 if (code == SYNC_OK) {
4186 vp->salvage.scheduled = 1;
4187 vp->stats.salvages++;
4188 vp->stats.last_salvage_req = FT_ApproxTime();
4189 IncUInt64(&VStats.salvages);
4193 case SYNC_BAD_COMMAND:
4194 case SYNC_COM_ERROR:
4197 Log("VScheduleSalvage_r: SALVSYNC request denied\n");
4200 Log("VScheduleSalvage_r: SALVSYNC unknown protocol error\n");
4205 #endif /* SALVSYNC_BUILD_CLIENT */
4209 #ifdef SALVSYNC_BUILD_CLIENT
4211 * connect to the salvageserver SYNC service.
4213 * @return operation status
4217 * @post connection to salvageserver SYNC service established
4219 * @see VConnectSALV_r
4220 * @see VDisconnectSALV
4221 * @see VReconnectSALV
4228 retVal = VConnectSALV_r();
4234 * connect to the salvageserver SYNC service.
4236 * @return operation status
4240 * @pre VOL_LOCK is held.
4242 * @post connection to salvageserver SYNC service established
4245 * @see VDisconnectSALV_r
4246 * @see VReconnectSALV_r
4247 * @see SALVSYNC_clientInit
4249 * @internal volume package internal use only.
4252 VConnectSALV_r(void)
4254 return SALVSYNC_clientInit();
4258 * disconnect from the salvageserver SYNC service.
4260 * @return operation status
4263 * @pre client should have a live connection to the salvageserver
4265 * @post connection to salvageserver SYNC service destroyed
4267 * @see VDisconnectSALV_r
4269 * @see VReconnectSALV
4272 VDisconnectSALV(void)
4276 VDisconnectSALV_r();
4282 * disconnect from the salvageserver SYNC service.
4284 * @return operation status
4288 * @arg VOL_LOCK is held.
4289 * @arg client should have a live connection to the salvageserver.
4291 * @post connection to salvageserver SYNC service destroyed
4293 * @see VDisconnectSALV
4294 * @see VConnectSALV_r
4295 * @see VReconnectSALV_r
4296 * @see SALVSYNC_clientFinis
4298 * @internal volume package internal use only.
4301 VDisconnectSALV_r(void)
4303 return SALVSYNC_clientFinis();
4307 * disconnect and then re-connect to the salvageserver SYNC service.
4309 * @return operation status
4313 * @pre client should have a live connection to the salvageserver
4315 * @post old connection is dropped, and a new one is established
4318 * @see VDisconnectSALV
4319 * @see VReconnectSALV_r
4322 VReconnectSALV(void)
4326 retVal = VReconnectSALV_r();
4332 * disconnect and then re-connect to the salvageserver SYNC service.
4334 * @return operation status
4339 * @arg VOL_LOCK is held.
4340 * @arg client should have a live connection to the salvageserver.
4342 * @post old connection is dropped, and a new one is established
4344 * @see VConnectSALV_r
4345 * @see VDisconnectSALV
4346 * @see VReconnectSALV
4347 * @see SALVSYNC_clientReconnect
4349 * @internal volume package internal use only.
4352 VReconnectSALV_r(void)
4354 return SALVSYNC_clientReconnect();
4356 #endif /* SALVSYNC_BUILD_CLIENT */
4357 #endif /* AFS_DEMAND_ATTACH_FS */
4360 /***************************************************/
4361 /* FSSYNC routines */
4362 /***************************************************/
4364 /* This must be called by any volume utility which needs to run while the
4365 file server is also running. This is separated from VInitVolumePackage so
4366 that a utility can fork--and each of the children can independently
4367 initialize communication with the file server */
4368 #ifdef FSSYNC_BUILD_CLIENT
4370 * connect to the fileserver SYNC service.
4372 * @return operation status
4377 * @arg VInit must equal 2.
4378 * @arg Program Type must not be fileserver or salvager.
4380 * @post connection to fileserver SYNC service established
4383 * @see VDisconnectFS
4384 * @see VChildProcReconnectFS
4391 retVal = VConnectFS_r();
4397 * connect to the fileserver SYNC service.
4399 * @return operation status
4404 * @arg VInit must equal 2.
4405 * @arg Program Type must not be fileserver or salvager.
4406 * @arg VOL_LOCK is held.
4408 * @post connection to fileserver SYNC service established
4411 * @see VDisconnectFS_r
4412 * @see VChildProcReconnectFS_r
4414 * @internal volume package internal use only.
4420 assert((VInit == 2) &&
4421 (programType != fileServer) &&
4422 (programType != salvager));
4423 rc = FSYNC_clientInit();
4430 * disconnect from the fileserver SYNC service.
4433 * @arg client should have a live connection to the fileserver.
4434 * @arg VOL_LOCK is held.
4435 * @arg Program Type must not be fileserver or salvager.
4437 * @post connection to fileserver SYNC service destroyed
4439 * @see VDisconnectFS
4441 * @see VChildProcReconnectFS_r
4443 * @internal volume package internal use only.
4446 VDisconnectFS_r(void)
4448 assert((programType != fileServer) &&
4449 (programType != salvager));
4450 FSYNC_clientFinis();
4455 * disconnect from the fileserver SYNC service.
4458 * @arg client should have a live connection to the fileserver.
4459 * @arg Program Type must not be fileserver or salvager.
4461 * @post connection to fileserver SYNC service destroyed
4463 * @see VDisconnectFS_r
4465 * @see VChildProcReconnectFS
4476 * connect to the fileserver SYNC service from a child process following a fork.
4478 * @return operation status
4483 * @arg VOL_LOCK is held.
4484 * @arg current FSYNC handle is shared with a parent process
4486 * @post current FSYNC handle is discarded and a new connection to the
4487 * fileserver SYNC service is established
4489 * @see VChildProcReconnectFS
4491 * @see VDisconnectFS_r
4493 * @internal volume package internal use only.
4496 VChildProcReconnectFS_r(void)
4498 return FSYNC_clientChildProcReconnect();
4502 * connect to the fileserver SYNC service from a child process following a fork.
4504 * @return operation status
4508 * @pre current FSYNC handle is shared with a parent process
4510 * @post current FSYNC handle is discarded and a new connection to the
4511 * fileserver SYNC service is established
4513 * @see VChildProcReconnectFS_r
4515 * @see VDisconnectFS
4518 VChildProcReconnectFS(void)
4522 ret = VChildProcReconnectFS_r();
4526 #endif /* FSSYNC_BUILD_CLIENT */
4529 /***************************************************/
4530 /* volume bitmap routines */
4531 /***************************************************/
4534 * For demand attach fs, flags parameter controls
4535 * locking behavior. If (flags & VOL_ALLOC_BITMAP_WAIT)
4536 * is set, then this function will create a reservation
4537 * and block on any other exclusive operations. Otherwise,
4538 * this function assumes the caller already has exclusive
4539 * access to vp, and we just change the volume state.
4542 VAllocBitmapEntry_r(Error * ec, Volume * vp,
4543 struct vnodeIndex *index, int flags)
4546 register byte *bp, *ep;
4547 #ifdef AFS_DEMAND_ATTACH_FS
4548 VolState state_save;
4549 #endif /* AFS_DEMAND_ATTACH_FS */
4553 /* This test is probably redundant */
4554 if (!VolumeWriteable(vp)) {
4555 *ec = (bit32) VREADONLY;
4559 #ifdef AFS_DEMAND_ATTACH_FS
4560 if (flags & VOL_ALLOC_BITMAP_WAIT) {
4561 VCreateReservation_r(vp);
4562 VWaitExclusiveState_r(vp);
4564 state_save = VChangeState_r(vp, VOL_STATE_GET_BITMAP);
4565 #endif /* AFS_DEMAND_ATTACH_FS */
4568 if ((programType == fileServer) && !index->bitmap) {
4570 #ifndef AFS_DEMAND_ATTACH_FS
4571 /* demand attach fs uses the volume state to avoid races.
4572 * specialStatus field is not used at all */
4574 if (vp->specialStatus == VBUSY) {
4575 if (vp->goingOffline) { /* vos dump waiting for the volume to
4576 * go offline. We probably come here
4577 * from AddNewReadableResidency */
4580 while (vp->specialStatus == VBUSY) {
4581 #ifdef AFS_PTHREAD_ENV
4585 #else /* !AFS_PTHREAD_ENV */
4587 #endif /* !AFS_PTHREAD_ENV */
4591 #endif /* !AFS_DEMAND_ATTACH_FS */
4593 if (!index->bitmap) {
4594 #ifndef AFS_DEMAND_ATTACH_FS
4595 vp->specialStatus = VBUSY; /* Stop anyone else from using it. */
4596 #endif /* AFS_DEMAND_ATTACH_FS */
4597 for (i = 0; i < nVNODECLASSES; i++) {
4598 VGetBitmap_r(ec, vp, i);
4600 #ifdef AFS_DEMAND_ATTACH_FS
4601 VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, VOL_SALVAGE_INVALIDATE_HEADER);
4602 #else /* AFS_DEMAND_ATTACH_FS */
4603 DeleteVolumeFromHashTable(vp);
4604 vp->shuttingDown = 1; /* Let who has it free it. */
4605 vp->specialStatus = 0;
4606 #endif /* AFS_DEMAND_ATTACH_FS */
4611 #ifndef AFS_DEMAND_ATTACH_FS
4613 vp->specialStatus = 0; /* Allow others to have access. */
4614 #endif /* AFS_DEMAND_ATTACH_FS */
4617 #endif /* BITMAP_LATER */
4619 #ifdef AFS_DEMAND_ATTACH_FS
4621 #endif /* AFS_DEMAND_ATTACH_FS */
4622 bp = index->bitmap + index->bitmapOffset;
4623 ep = index->bitmap + index->bitmapSize;
4625 if ((*(bit32 *) bp) != (bit32) 0xffffffff) {
4627 index->bitmapOffset = (afs_uint32) (bp - index->bitmap);
4630 o = ffs(~*bp) - 1; /* ffs is documented in BSTRING(3) */
4632 ret = (VnodeId) ((bp - index->bitmap) * 8 + o);
4633 #ifdef AFS_DEMAND_ATTACH_FS
4635 #endif /* AFS_DEMAND_ATTACH_FS */
4638 bp += sizeof(bit32) /* i.e. 4 */ ;
4640 /* No bit map entry--must grow bitmap */
4642 realloc(index->bitmap, index->bitmapSize + VOLUME_BITMAP_GROWSIZE);
4645 bp += index->bitmapSize;
4646 memset(bp, 0, VOLUME_BITMAP_GROWSIZE);
4647 index->bitmapOffset = index->bitmapSize;
4648 index->bitmapSize += VOLUME_BITMAP_GROWSIZE;
4650 ret = index->bitmapOffset * 8;
4651 #ifdef AFS_DEMAND_ATTACH_FS
4653 #endif /* AFS_DEMAND_ATTACH_FS */
4656 #ifdef AFS_DEMAND_ATTACH_FS
4657 VChangeState_r(vp, state_save);
4658 if (flags & VOL_ALLOC_BITMAP_WAIT) {
4659 VCancelReservation_r(vp);
4661 #endif /* AFS_DEMAND_ATTACH_FS */
4666 VAllocBitmapEntry(Error * ec, Volume * vp, register struct vnodeIndex * index)
4670 retVal = VAllocBitmapEntry_r(ec, vp, index, VOL_ALLOC_BITMAP_WAIT);
4676 VFreeBitMapEntry_r(Error * ec, register struct vnodeIndex *index,
4679 unsigned int offset;
4685 #endif /* BITMAP_LATER */
4686 offset = bitNumber >> 3;
4687 if (offset >= index->bitmapSize) {
4691 if (offset < index->bitmapOffset)
4692 index->bitmapOffset = offset & ~3; /* Truncate to nearest bit32 */
4693 *(index->bitmap + offset) &= ~(1 << (bitNumber & 0x7));
4697 VFreeBitMapEntry(Error * ec, register struct vnodeIndex *index,
4701 VFreeBitMapEntry_r(ec, index, bitNumber);
4705 /* this function will drop the glock internally.
4706 * for old pthread fileservers, this is safe thanks to vbusy.
4708 * for demand attach fs, caller must have already called
4709 * VCreateReservation_r and VWaitExclusiveState_r */
4711 VGetBitmap_r(Error * ec, Volume * vp, VnodeClass class)
4713 StreamHandle_t *file;
4716 struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
4717 struct vnodeIndex *vip = &vp->vnodeIndex[class];
4718 struct VnodeDiskObject *vnode;
4719 unsigned int unique = 0;
4723 #endif /* BITMAP_LATER */
4724 #ifdef AFS_DEMAND_ATTACH_FS
4725 VolState state_save;
4726 #endif /* AFS_DEMAND_ATTACH_FS */
4730 #ifdef AFS_DEMAND_ATTACH_FS
4731 state_save = VChangeState_r(vp, VOL_STATE_GET_BITMAP);
4732 #endif /* AFS_DEMAND_ATTACH_FS */
4735 fdP = IH_OPEN(vip->handle);
4736 assert(fdP != NULL);
4737 file = FDH_FDOPEN(fdP, "r");
4738 assert(file != NULL);
4739 vnode = (VnodeDiskObject *) malloc(vcp->diskSize);
4740 assert(vnode != NULL);
4741 size = OS_SIZE(fdP->fd_fd);
4743 nVnodes = (size <= vcp->diskSize ? 0 : size - vcp->diskSize)
4745 vip->bitmapSize = ((nVnodes / 8) + 10) / 4 * 4; /* The 10 is a little extra so
4746 * a few files can be created in this volume,
4747 * the whole thing is rounded up to nearest 4
4748 * bytes, because the bit map allocator likes
4751 BitMap = (byte *) calloc(1, vip->bitmapSize);
4752 assert(BitMap != NULL);
4753 #else /* BITMAP_LATER */
4754 vip->bitmap = (byte *) calloc(1, vip->bitmapSize);
4755 assert(vip->bitmap != NULL);
4756 vip->bitmapOffset = 0;
4757 #endif /* BITMAP_LATER */
4758 if (STREAM_SEEK(file, vcp->diskSize, 0) != -1) {
4760 for (bitNumber = 0; bitNumber < nVnodes + 100; bitNumber++) {
4761 if (STREAM_READ(vnode, vcp->diskSize, 1, file) != 1)
4763 if (vnode->type != vNull) {
4764 if (vnode->vnodeMagic != vcp->magic) {
4765 Log("GetBitmap: addled vnode index in volume %s; volume needs salvage\n", V_name(vp));
4770 *(BitMap + (bitNumber >> 3)) |= (1 << (bitNumber & 0x7));
4771 #else /* BITMAP_LATER */
4772 *(vip->bitmap + (bitNumber >> 3)) |= (1 << (bitNumber & 0x7));
4773 #endif /* BITMAP_LATER */
4774 if (unique <= vnode->uniquifier)
4775 unique = vnode->uniquifier + 1;
4777 #ifndef AFS_PTHREAD_ENV
4778 if ((bitNumber & 0x00ff) == 0x0ff) { /* every 256 iterations */
4781 #endif /* !AFS_PTHREAD_ENV */
4784 if (vp->nextVnodeUnique < unique) {
4785 Log("GetBitmap: bad volume uniquifier for volume %s; volume needs salvage\n", V_name(vp));
4788 /* Paranoia, partly justified--I think fclose after fdopen
4789 * doesn't seem to close fd. In any event, the documentation
4790 * doesn't specify, so it's safer to close it twice.
4798 /* There may have been a racing condition with some other thread, both
4799 * creating the bitmaps for this volume. If the other thread was faster
4800 * the pointer to bitmap should already be filled and we can free ours.
4802 if (vip->bitmap == NULL) {
4803 vip->bitmap = BitMap;
4804 vip->bitmapOffset = 0;
4806 free((byte *) BitMap);
4807 #endif /* BITMAP_LATER */
4808 #ifdef AFS_DEMAND_ATTACH_FS
4809 VChangeState_r(vp, state_save);
4810 #endif /* AFS_DEMAND_ATTACH_FS */
4814 /***************************************************/
4815 /* Volume Path and Volume Number utility routines */
4816 /***************************************************/
4819 * find the first occurrence of a volume header file and return the path.
4821 * @param[out] ec outbound error code
4822 * @param[in] volumeId volume id to find
4823 * @param[out] partitionp pointer to disk partition path string
4824 * @param[out] namep pointer to volume header file name string
4826 * @post path to first occurrence of volume header is returned in partitionp
4827 * and namep, or ec is set accordingly.
4829 * @warning this function is NOT re-entrant -- partitionp and namep point to
4830 * static data segments
4832 * @note if a volume utility inadvertently leaves behind a stale volume header
4833 * on a vice partition, it is possible for callers to get the wrong one,
4834 * depending on the order of the disk partition linked list.
4838 VGetVolumePath(Error * ec, VolId volumeId, char **partitionp, char **namep)
4840 static char partition[VMAXPATHLEN], name[VMAXPATHLEN];
4841 char path[VMAXPATHLEN];
4843 struct DiskPartition64 *dp;
4847 (void)afs_snprintf(&name[1], (sizeof name) - 1, VFORMAT, afs_printable_uint32_lu(volumeId));
4848 for (dp = DiskPartitionList; dp; dp = dp->next) {
4849 struct afs_stat status;
4850 strcpy(path, VPartitionPath(dp));
4852 if (afs_stat(path, &status) == 0) {
4853 strcpy(partition, dp->name);
4860 *partitionp = *namep = NULL;
4862 *partitionp = partition;
4868 * extract a volume number from a volume header filename string.
4870 * @param[in] name volume header filename string
4872 * @return volume number
4874 * @note the string must be of the form VFORMAT. the only permissible
4875 * deviation is a leading '/' character.
4880 VolumeNumber(char *name)
4884 return atoi(name + 1);
4888 * compute the volume header filename.
4890 * @param[in] volumeId
4892 * @return volume header filename
4894 * @post volume header filename string is constructed
4896 * @warning this function is NOT re-entrant -- the returned string is
4897 * stored in a static char array. see VolumeExternalName_r
4898 * for a re-entrant equivalent.
4900 * @see VolumeExternalName_r
4902 * @deprecated due to the above re-entrancy warning, this interface should
4903 * be considered deprecated. Please use VolumeExternalName_r
4907 VolumeExternalName(VolumeId volumeId)
4909 static char name[VMAXPATHLEN];
4910 (void)afs_snprintf(name, sizeof name, VFORMAT, afs_printable_uint32_lu(volumeId));
4915 * compute the volume header filename.
4917 * @param[in] volumeId
4918 * @param[inout] name array in which to store filename
4919 * @param[in] len length of name array
4921 * @return result code from afs_snprintf
4923 * @see VolumeExternalName
4926 * @note re-entrant equivalent of VolumeExternalName
4928 * @internal volume package internal use only.
4930 #ifdef AFS_DEMAND_ATTACH_FS
4932 VolumeExternalName_r(VolumeId volumeId, char * name, size_t len)
4934 return afs_snprintf(name, len, VFORMAT, afs_printable_uint32_lu(volumeId));
4939 /***************************************************/
4940 /* Volume Usage Statistics routines */
4941 /***************************************************/
4943 #if OPENAFS_VOL_STATS
4944 #define OneDay (86400) /* 24 hours' worth of seconds */
4946 #define OneDay (24*60*60) /* 24 hours */
4947 #endif /* OPENAFS_VOL_STATS */
4949 #define Midnight(date) ((date-TimeZoneCorrection)/OneDay*OneDay+TimeZoneCorrection)
4951 /*------------------------------------------------------------------------
4952 * [export] VAdjustVolumeStatistics
4955 * If we've passed midnight, we need to update all the day use
4956 * statistics as well as zeroing the detailed volume statistics
4957 * (if we are implementing them).
4960 * vp : Pointer to the volume structure describing the lucky
4961 * volume being considered for update.
4967 * Nothing interesting.
4971 *------------------------------------------------------------------------*/
4974 VAdjustVolumeStatistics_r(register Volume * vp)
4976 unsigned int now = FT_ApproxTime();
4978 if (now - V_dayUseDate(vp) > OneDay) {
4979 register int ndays, i;
4981 ndays = (now - V_dayUseDate(vp)) / OneDay;
4982 for (i = 6; i > ndays - 1; i--)
4983 V_weekUse(vp)[i] = V_weekUse(vp)[i - ndays];
4984 for (i = 0; i < ndays - 1 && i < 7; i++)
4985 V_weekUse(vp)[i] = 0;
4987 V_weekUse(vp)[ndays - 1] = V_dayUse(vp);
4989 V_dayUseDate(vp) = Midnight(now);
4991 #if OPENAFS_VOL_STATS
4993 * All we need to do is bzero the entire VOL_STATS_BYTES of
4994 * the detailed volume statistics area.
4996 memset((char *)(V_stat_area(vp)), 0, VOL_STATS_BYTES);
4997 #endif /* OPENAFS_VOL_STATS */
5000 /*It's been more than a day of collection */
5002 * Always return happily.
5005 } /*VAdjustVolumeStatistics */
5008 VAdjustVolumeStatistics(register Volume * vp)
5012 retVal = VAdjustVolumeStatistics_r(vp);
5018 VBumpVolumeUsage_r(register Volume * vp)
5020 unsigned int now = FT_ApproxTime();
5021 if (now - V_dayUseDate(vp) > OneDay)
5022 VAdjustVolumeStatistics_r(vp);
5024 * Save the volume header image to disk after every 128 bumps to dayUse.
5026 if ((V_dayUse(vp)++ & 127) == 0) {
5028 VUpdateVolume_r(&error, vp, VOL_UPDATE_WAIT);
5033 VBumpVolumeUsage(register Volume * vp)
5036 VBumpVolumeUsage_r(vp);
5041 VSetDiskUsage_r(void)
5043 #ifndef AFS_DEMAND_ATTACH_FS
5044 static int FifteenMinuteCounter = 0;
5048 /* NOTE: Don't attempt to access the partitions list until the
5049 * initialization level indicates that all volumes are attached,
5050 * which implies that all partitions are initialized. */
5051 #ifdef AFS_PTHREAD_ENV
5053 #else /* AFS_PTHREAD_ENV */
5055 #endif /* AFS_PTHREAD_ENV */
5058 VResetDiskUsage_r();
5060 #ifndef AFS_DEMAND_ATTACH_FS
5061 if (++FifteenMinuteCounter == 3) {
5062 FifteenMinuteCounter = 0;
5065 #endif /* !AFS_DEMAND_ATTACH_FS */
5077 /***************************************************/
5078 /* Volume Update List routines */
5079 /***************************************************/
5081 /* The number of minutes that a volume hasn't been updated before the
5082 * "Dont salvage" flag in the volume header will be turned on */
5083 #define SALVAGE_INTERVAL (10*60)
5088 * volume update list functionality has been moved into the VLRU
5089 * the DONT_SALVAGE flag is now set during VLRU demotion
5092 #ifndef AFS_DEMAND_ATTACH_FS
5093 static VolumeId *UpdateList = NULL; /* Pointer to array of Volume ID's */
5094 static int nUpdatedVolumes = 0; /* Updated with entry in UpdateList, salvage after crash flag on */
5095 static int updateSize = 0; /* number of entries possible */
5096 #define UPDATE_LIST_SIZE 128 /* initial size increment (must be a power of 2!) */
5097 #endif /* !AFS_DEMAND_ATTACH_FS */
5100 VAddToVolumeUpdateList_r(Error * ec, Volume * vp)
5103 vp->updateTime = FT_ApproxTime();
5104 if (V_dontSalvage(vp) == 0)
5106 V_dontSalvage(vp) = 0;
5107 VSyncVolume_r(ec, vp, 0);
5108 #ifdef AFS_DEMAND_ATTACH_FS
5109 V_attachFlags(vp) &= ~(VOL_HDR_DONTSALV);
5110 #else /* !AFS_DEMAND_ATTACH_FS */
5113 if (UpdateList == NULL) {
5114 updateSize = UPDATE_LIST_SIZE;
5115 UpdateList = (VolumeId *) malloc(sizeof(VolumeId) * updateSize);
5117 if (nUpdatedVolumes == updateSize) {
5119 if (updateSize > 524288) {
5120 Log("warning: there is likely a bug in the volume update scanner\n");
5124 (VolumeId *) realloc(UpdateList,
5125 sizeof(VolumeId) * updateSize);
5128 assert(UpdateList != NULL);
5129 UpdateList[nUpdatedVolumes++] = V_id(vp);
5130 #endif /* !AFS_DEMAND_ATTACH_FS */
5133 #ifndef AFS_DEMAND_ATTACH_FS
5135 VScanUpdateList(void)
5137 register int i, gap;
5138 register Volume *vp;
5140 afs_uint32 now = FT_ApproxTime();
5141 /* Be careful with this code, since it works with interleaved calls to AddToVolumeUpdateList */
5142 for (i = gap = 0; i < nUpdatedVolumes; i++) {
5144 UpdateList[i - gap] = UpdateList[i];
5146 /* XXX this routine needlessly messes up the Volume LRU by
5147 * breaking the LRU temporal-locality assumptions.....
5148 * we should use a special volume header allocator here */
5149 vp = VGetVolume_r(&error, UpdateList[i - gap] = UpdateList[i]);
5152 } else if (vp->nUsers == 1 && now - vp->updateTime > SALVAGE_INTERVAL) {
5153 V_dontSalvage(vp) = DONT_SALVAGE;
5154 VUpdateVolume_r(&error, vp, 0); /* No need to fsync--not critical */
5162 #ifndef AFS_PTHREAD_ENV
5164 #endif /* !AFS_PTHREAD_ENV */
5166 nUpdatedVolumes -= gap;
5168 #endif /* !AFS_DEMAND_ATTACH_FS */
5171 /***************************************************/
5172 /* Volume LRU routines */
5173 /***************************************************/
5178 * with demand attach fs, we attempt to soft detach(1)
5179 * volumes which have not been accessed in a long time
5180 * in order to speed up fileserver shutdown
5182 * (1) by soft detach we mean a process very similar
5183 * to VOffline, except the final state of the
5184 * Volume will be VOL_STATE_PREATTACHED, instead
5185 * of the usual VOL_STATE_UNATTACHED
5187 #ifdef AFS_DEMAND_ATTACH_FS
5189 /* implementation is reminiscent of a generational GC
5191 * queue 0 is newly attached volumes. this queue is
5192 * sorted by attach timestamp
5194 * queue 1 is volumes that have been around a bit
5195 * longer than queue 0. this queue is sorted by
5198 * queue 2 is volumes tha have been around the longest.
5199 * this queue is unsorted
5201 * queue 3 is volumes that have been marked as
5202 * candidates for soft detachment. this queue is
5205 #define VLRU_GENERATIONS 3 /**< number of generations in VLRU */
5206 #define VLRU_QUEUES 5 /**< total number of VLRU queues */
5209 * definition of a VLRU queue.
5212 volatile struct rx_queue q;
5219 * main VLRU data structure.
5222 struct VLRU_q q[VLRU_QUEUES]; /**< VLRU queues */
5225 /** time interval (in seconds) between promotion passes for
5226 * each young generation queue. */
5227 afs_uint32 promotion_interval[VLRU_GENERATIONS-1];
5229 /** time interval (in seconds) between soft detach candidate
5230 * scans for each generation queue.
5232 * scan_interval[VLRU_QUEUE_CANDIDATE] defines how frequently
5233 * we perform a soft detach pass. */
5234 afs_uint32 scan_interval[VLRU_GENERATIONS+1];
5236 /* scheduler state */
5237 int next_idx; /**< next queue to receive attention */
5238 afs_uint32 last_promotion[VLRU_GENERATIONS-1]; /**< timestamp of last promotion scan */
5239 afs_uint32 last_scan[VLRU_GENERATIONS+1]; /**< timestamp of last detach scan */
5241 int scanner_state; /**< state of scanner thread */
5242 pthread_cond_t cv; /**< state transition CV */
5245 /** global VLRU state */
5246 static struct VLRU volume_LRU;
5249 * defined states for VLRU scanner thread.
5252 VLRU_SCANNER_STATE_OFFLINE = 0, /**< vlru scanner thread is offline */
5253 VLRU_SCANNER_STATE_ONLINE = 1, /**< vlru scanner thread is online */
5254 VLRU_SCANNER_STATE_SHUTTING_DOWN = 2, /**< vlru scanner thread is shutting down */
5255 VLRU_SCANNER_STATE_PAUSING = 3, /**< vlru scanner thread is getting ready to pause */
5256 VLRU_SCANNER_STATE_PAUSED = 4 /**< vlru scanner thread is paused */
5257 } vlru_thread_state_t;
5259 /* vlru disk data header stuff */
5260 #define VLRU_DISK_MAGIC 0x7a8b9cad /**< vlru disk entry magic number */
5261 #define VLRU_DISK_VERSION 1 /**< vlru disk entry version number */
5263 /** vlru default expiration time (for eventual fs state serialization of vlru data) */
5264 #define VLRU_DUMP_EXPIRATION_TIME (60*60*24*7) /* expire vlru data after 1 week */
5267 /** minimum volume inactivity (in seconds) before a volume becomes eligible for
5268 * soft detachment. */
5269 static afs_uint32 VLRU_offline_thresh = VLRU_DEFAULT_OFFLINE_THRESH;
5271 /** time interval (in seconds) between VLRU scanner thread soft detach passes. */
5272 static afs_uint32 VLRU_offline_interval = VLRU_DEFAULT_OFFLINE_INTERVAL;
5274 /** maximum number of volumes to soft detach in a VLRU soft detach pass. */
5275 static afs_uint32 VLRU_offline_max = VLRU_DEFAULT_OFFLINE_MAX;
5277 /** VLRU control flag. non-zero value implies VLRU subsystem is activated. */
5278 static afs_uint32 VLRU_enabled = 1;
5280 /* queue synchronization routines */
5281 static void VLRU_BeginExclusive_r(struct VLRU_q * q);
5282 static void VLRU_EndExclusive_r(struct VLRU_q * q);
5283 static void VLRU_Wait_r(struct VLRU_q * q);
5286 * set VLRU subsystem tunable parameters.
5288 * @param[in] option tunable option to modify
5289 * @param[in] val new value for tunable parameter
5291 * @pre @c VInitVolumePackage has not yet been called.
5293 * @post tunable parameter is modified
5297 * @note valid option parameters are:
5298 * @arg @c VLRU_SET_THRESH
5299 * set the period of inactivity after which
5300 * volumes are eligible for soft detachment
5301 * @arg @c VLRU_SET_INTERVAL
5302 * set the time interval between calls
5303 * to the volume LRU "garbage collector"
5304 * @arg @c VLRU_SET_MAX
5305 * set the max number of volumes to deallocate
5309 VLRU_SetOptions(int option, afs_uint32 val)
5311 if (option == VLRU_SET_THRESH) {
5312 VLRU_offline_thresh = val;
5313 } else if (option == VLRU_SET_INTERVAL) {
5314 VLRU_offline_interval = val;
5315 } else if (option == VLRU_SET_MAX) {
5316 VLRU_offline_max = val;
5317 } else if (option == VLRU_SET_ENABLED) {
5320 VLRU_ComputeConstants();
5324 * compute VLRU internal timing parameters.
5326 * @post VLRU scanner thread internal timing parameters are computed
5328 * @note computes internal timing parameters based upon user-modifiable
5329 * tunable parameters.
5333 * @internal volume package internal use only.
5336 VLRU_ComputeConstants(void)
5338 afs_uint32 factor = VLRU_offline_thresh / VLRU_offline_interval;
5340 /* compute the candidate scan interval */
5341 volume_LRU.scan_interval[VLRU_QUEUE_CANDIDATE] = VLRU_offline_interval;
5343 /* compute the promotion intervals */
5344 volume_LRU.promotion_interval[VLRU_QUEUE_NEW] = VLRU_offline_thresh * 2;
5345 volume_LRU.promotion_interval[VLRU_QUEUE_MID] = VLRU_offline_thresh * 4;
5348 /* compute the gen 0 scan interval */
5349 volume_LRU.scan_interval[VLRU_QUEUE_NEW] = VLRU_offline_thresh / 8;
5351 /* compute the gen 0 scan interval */
5352 volume_LRU.scan_interval[VLRU_QUEUE_NEW] = VLRU_offline_interval * 2;
5357 * initialize VLRU subsystem.
5359 * @pre this function has not yet been called
5361 * @post VLRU subsystem is initialized and VLRU scanner thread is starting
5365 * @internal volume package internal use only.
5371 pthread_attr_t attrs;
5374 if (!VLRU_enabled) {
5375 Log("VLRU: disabled\n");
5379 /* initialize each of the VLRU queues */
5380 for (i = 0; i < VLRU_QUEUES; i++) {
5381 queue_Init(&volume_LRU.q[i]);
5382 volume_LRU.q[i].len = 0;
5383 volume_LRU.q[i].busy = 0;
5384 assert(pthread_cond_init(&volume_LRU.q[i].cv, NULL) == 0);
5387 /* setup the timing constants */
5388 VLRU_ComputeConstants();
5390 /* XXX put inside LogLevel check? */
5391 Log("VLRU: starting scanner with the following configuration parameters:\n");
5392 Log("VLRU: offlining volumes after minimum of %d seconds of inactivity\n", VLRU_offline_thresh);
5393 Log("VLRU: running VLRU soft detach pass every %d seconds\n", VLRU_offline_interval);
5394 Log("VLRU: taking up to %d volumes offline per pass\n", VLRU_offline_max);
5395 Log("VLRU: scanning generation 0 for inactive volumes every %d seconds\n", volume_LRU.scan_interval[0]);
5396 Log("VLRU: scanning for promotion/demotion between generations 0 and 1 every %d seconds\n", volume_LRU.promotion_interval[0]);
5397 Log("VLRU: scanning for promotion/demotion between generations 1 and 2 every %d seconds\n", volume_LRU.promotion_interval[1]);
5399 /* start up the VLRU scanner */
5400 volume_LRU.scanner_state = VLRU_SCANNER_STATE_OFFLINE;
5401 if (programType == fileServer) {
5402 assert(pthread_cond_init(&volume_LRU.cv, NULL) == 0);
5403 assert(pthread_attr_init(&attrs) == 0);
5404 assert(pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED) == 0);
5405 assert(pthread_create(&tid, &attrs, &VLRU_ScannerThread, NULL) == 0);
5410 * initialize the VLRU-related fields of a newly allocated volume object.
5412 * @param[in] vp pointer to volume object
5415 * @arg @c VOL_LOCK is held.
5416 * @arg volume object is not on a VLRU queue.
5418 * @post VLRU fields are initialized to indicate that volume object is not
5419 * currently registered with the VLRU subsystem
5423 * @internal volume package interal use only.
5426 VLRU_Init_Node_r(volatile Volume * vp)
5431 assert(queue_IsNotOnQueue(&vp->vlru));
5432 vp->vlru.idx = VLRU_QUEUE_INVALID;
5436 * add a volume object to a VLRU queue.
5438 * @param[in] vp pointer to volume object
5441 * @arg @c VOL_LOCK is held.
5442 * @arg caller MUST hold a lightweight ref on @p vp.
5443 * @arg caller MUST NOT hold exclusive ownership of the VLRU queue.
5445 * @post the volume object is added to the appropriate VLRU queue
5447 * @note if @c vp->vlru.idx contains the index of a valid VLRU queue,
5448 * then the volume is added to that queue. Otherwise, the value
5449 * @c VLRU_QUEUE_NEW is stored into @c vp->vlru.idx and the
5450 * volume is added to the NEW generation queue.
5452 * @note @c VOL_LOCK may be dropped internally
5454 * @note Volume state is temporarily set to @c VOL_STATE_VLRU_ADD
5455 * during the add operation, and is restored to the previous
5456 * state prior to return.
5460 * @internal volume package internal use only.
5463 VLRU_Add_r(volatile Volume * vp)
5466 VolState state_save;
5471 if (queue_IsOnQueue(&vp->vlru))
5474 state_save = VChangeState_r(vp, VOL_STATE_VLRU_ADD);
5477 if ((idx < 0) || (idx >= VLRU_QUEUE_INVALID)) {
5478 idx = VLRU_QUEUE_NEW;
5481 VLRU_Wait_r(&volume_LRU.q[idx]);
5483 /* repeat check since VLRU_Wait_r may have dropped
5485 if (queue_IsNotOnQueue(&vp->vlru)) {
5487 queue_Prepend(&volume_LRU.q[idx], &vp->vlru);
5488 volume_LRU.q[idx].len++;
5489 V_attachFlags(vp) |= VOL_ON_VLRU;
5490 vp->stats.last_promote = FT_ApproxTime();
5493 VChangeState_r(vp, state_save);
5497 * delete a volume object from a VLRU queue.
5499 * @param[in] vp pointer to volume object
5502 * @arg @c VOL_LOCK is held.
5503 * @arg caller MUST hold a lightweight ref on @p vp.
5504 * @arg caller MUST NOT hold exclusive ownership of the VLRU queue.
5506 * @post volume object is removed from the VLRU queue
5508 * @note @c VOL_LOCK may be dropped internally
5512 * @todo We should probably set volume state to something exlcusive
5513 * (as @c VLRU_Add_r does) prior to dropping @c VOL_LOCK.
5515 * @internal volume package internal use only.
5518 VLRU_Delete_r(volatile Volume * vp)
5525 if (queue_IsNotOnQueue(&vp->vlru))
5531 if (idx == VLRU_QUEUE_INVALID)
5533 VLRU_Wait_r(&volume_LRU.q[idx]);
5534 } while (idx != vp->vlru.idx);
5536 /* now remove from the VLRU and update
5537 * the appropriate counter */
5538 queue_Remove(&vp->vlru);
5539 volume_LRU.q[idx].len--;
5540 vp->vlru.idx = VLRU_QUEUE_INVALID;
5541 V_attachFlags(vp) &= ~(VOL_ON_VLRU);
5545 * tell the VLRU subsystem that a volume was just accessed.
5547 * @param[in] vp pointer to volume object
5550 * @arg @c VOL_LOCK is held
5551 * @arg caller MUST hold a lightweight ref on @p vp
5552 * @arg caller MUST NOT hold exclusive ownership of any VLRU queue
5554 * @post volume VLRU access statistics are updated. If the volume was on
5555 * the VLRU soft detach candidate queue, it is moved to the NEW
5558 * @note @c VOL_LOCK may be dropped internally
5562 * @internal volume package internal use only.
5565 VLRU_UpdateAccess_r(volatile Volume * vp)
5567 afs_uint32 live_interval;
5568 Volume * rvp = NULL;
5573 if (queue_IsNotOnQueue(&vp->vlru))
5576 assert(V_attachFlags(vp) & VOL_ON_VLRU);
5578 /* update the access timestamp */
5579 vp->stats.last_get = FT_ApproxTime();
5582 * if the volume is on the soft detach candidate
5583 * list, we need to safely move it back to a
5584 * regular generation. this has to be done
5585 * carefully so we don't race against the scanner
5589 /* if this volume is on the soft detach candidate queue,
5590 * then grab exclusive access to the necessary queues */
5591 if (vp->vlru.idx == VLRU_QUEUE_CANDIDATE) {
5593 VCreateReservation_r(rvp);
5595 VLRU_Wait_r(&volume_LRU.q[VLRU_QUEUE_NEW]);
5596 VLRU_BeginExclusive_r(&volume_LRU.q[VLRU_QUEUE_NEW]);
5597 VLRU_Wait_r(&volume_LRU.q[VLRU_QUEUE_CANDIDATE]);
5598 VLRU_BeginExclusive_r(&volume_LRU.q[VLRU_QUEUE_CANDIDATE]);
5601 /* make sure multiple threads don't race to update */
5602 if (vp->vlru.idx == VLRU_QUEUE_CANDIDATE) {
5603 VLRU_SwitchQueues(vp, VLRU_QUEUE_NEW, 1);
5607 VLRU_EndExclusive_r(&volume_LRU.q[VLRU_QUEUE_CANDIDATE]);
5608 VLRU_EndExclusive_r(&volume_LRU.q[VLRU_QUEUE_NEW]);
5609 VCancelReservation_r(rvp);
5614 * switch a volume between two VLRU queues.
5616 * @param[in] vp pointer to volume object
5617 * @param[in] new_idx index of VLRU queue onto which the volume will be moved
5618 * @param[in] append controls whether the volume will be appended or
5619 * prepended to the queue. A nonzero value means it will
5620 * be appended; zero means it will be prepended.
5622 * @pre The new (and old, if applicable) queue(s) must either be owned
5623 * exclusively by the calling thread for asynchronous manipulation,
5624 * or the queue(s) must be quiescent and VOL_LOCK must be held.
5625 * Please see VLRU_BeginExclusive_r, VLRU_EndExclusive_r and VLRU_Wait_r
5626 * for further details of the queue asynchronous processing mechanism.
5628 * @post If the volume object was already on a VLRU queue, it is
5629 * removed from the queue. Depending on the value of the append
5630 * parameter, the volume object is either appended or prepended
5631 * to the VLRU queue referenced by the new_idx parameter.
5635 * @see VLRU_BeginExclusive_r
5636 * @see VLRU_EndExclusive_r
5639 * @internal volume package internal use only.
5642 VLRU_SwitchQueues(volatile Volume * vp, int new_idx, int append)
5644 if (queue_IsNotOnQueue(&vp->vlru))
5647 queue_Remove(&vp->vlru);
5648 volume_LRU.q[vp->vlru.idx].len--;
5650 /* put the volume back on the correct generational queue */
5652 queue_Append(&volume_LRU.q[new_idx], &vp->vlru);
5654 queue_Prepend(&volume_LRU.q[new_idx], &vp->vlru);
5657 volume_LRU.q[new_idx].len++;
5658 vp->vlru.idx = new_idx;
5662 * VLRU background thread.
5664 * The VLRU Scanner Thread is responsible for periodically scanning through
5665 * each VLRU queue looking for volumes which should be moved to another
5666 * queue, or soft detached.
5668 * @param[in] args unused thread arguments parameter
5670 * @return unused thread return value
5671 * @retval NULL always
5673 * @internal volume package internal use only.
5676 VLRU_ScannerThread(void * args)
5678 afs_uint32 now, min_delay, delay;
5679 afs_uint32 next_scan[VLRU_GENERATIONS];
5680 afs_uint32 next_promotion[VLRU_GENERATIONS];
5681 int i, min_idx, min_op, overdue, state;
5683 /* set t=0 for promotion cycle to be
5684 * fileserver startup */
5685 now = FT_ApproxTime();
5686 for (i=0; i < VLRU_GENERATIONS-1; i++) {
5687 volume_LRU.last_promotion[i] = now;
5690 /* don't start the scanner until VLRU_offline_thresh
5691 * plus a small delay for VInitVolumePackage to finish
5694 sleep(VLRU_offline_thresh + 60);
5696 /* set t=0 for scan cycle to be now */
5697 now = FT_ApproxTime();
5698 for (i=0; i < VLRU_GENERATIONS+1; i++) {
5699 volume_LRU.last_scan[i] = now;
5703 if (volume_LRU.scanner_state == VLRU_SCANNER_STATE_OFFLINE) {
5704 volume_LRU.scanner_state = VLRU_SCANNER_STATE_ONLINE;
5707 while ((state = volume_LRU.scanner_state) != VLRU_SCANNER_STATE_SHUTTING_DOWN) {
5708 /* check to see if we've been asked to pause */
5709 if (volume_LRU.scanner_state == VLRU_SCANNER_STATE_PAUSING) {
5710 volume_LRU.scanner_state = VLRU_SCANNER_STATE_PAUSED;
5711 assert(pthread_cond_broadcast(&volume_LRU.cv) == 0);
5713 VOL_CV_WAIT(&volume_LRU.cv);
5714 } while (volume_LRU.scanner_state == VLRU_SCANNER_STATE_PAUSED);
5717 /* scheduling can happen outside the glock */
5720 /* figure out what is next on the schedule */
5722 /* figure out a potential schedule for the new generation first */
5724 min_delay = volume_LRU.scan_interval[0] + volume_LRU.last_scan[0] - now;
5727 if (min_delay > volume_LRU.scan_interval[0]) {
5728 /* unsigned overflow -- we're overdue to run this scan */
5733 /* if we're not overdue for gen 0, figure out schedule for candidate gen */
5735 i = VLRU_QUEUE_CANDIDATE;
5736 delay = volume_LRU.scan_interval[i] + volume_LRU.last_scan[i] - now;
5737 if (delay < min_delay) {
5741 if (delay > volume_LRU.scan_interval[i]) {
5742 /* unsigned overflow -- we're overdue to run this scan */
5749 /* if we're still not overdue for something, figure out schedules for promotions */
5750 for (i=0; !overdue && i < VLRU_GENERATIONS-1; i++) {
5751 delay = volume_LRU.promotion_interval[i] + volume_LRU.last_promotion[i] - now;
5752 if (delay < min_delay) {
5757 if (delay > volume_LRU.promotion_interval[i]) {
5758 /* unsigned overflow -- we're overdue to run this promotion */
5767 /* sleep as needed */
5772 /* do whatever is next */
5775 VLRU_Promote_r(min_idx);
5776 VLRU_Demote_r(min_idx+1);
5778 VLRU_Scan_r(min_idx);
5780 now = FT_ApproxTime();
5783 Log("VLRU scanner asked to go offline (scanner_state=%d)\n", state);
5785 /* signal that scanner is down */
5786 volume_LRU.scanner_state = VLRU_SCANNER_STATE_OFFLINE;
5787 assert(pthread_cond_broadcast(&volume_LRU.cv) == 0);
5793 * promote volumes from one VLRU generation to the next.
5795 * This routine scans a VLRU generation looking for volumes which are
5796 * eligible to be promoted to the next generation. All volumes which
5797 * meet the eligibility requirement are promoted.
5799 * Promotion eligibility is based upon meeting both of the following
5802 * @arg The volume has been accessed since the last promotion:
5803 * @c (vp->stats.last_get >= vp->stats.last_promote)
5804 * @arg The last promotion occurred at least
5805 * @c volume_LRU.promotion_interval[idx] seconds ago
5807 * As a performance optimization, promotions are "globbed". In other
5808 * words, we promote arbitrarily large contiguous sublists of elements
5811 * @param[in] idx VLRU queue index to scan
5815 * @internal VLRU internal use only.
5818 VLRU_Promote_r(int idx)
5820 int len, chaining, promote;
5821 afs_uint32 now, thresh;
5822 struct rx_queue *qp, *nqp;
5823 Volume * vp, *start, *end;
5825 /* get exclusive access to two chains, and drop the glock */
5826 VLRU_Wait_r(&volume_LRU.q[idx]);
5827 VLRU_BeginExclusive_r(&volume_LRU.q[idx]);
5828 VLRU_Wait_r(&volume_LRU.q[idx+1]);
5829 VLRU_BeginExclusive_r(&volume_LRU.q[idx+1]);
5832 thresh = volume_LRU.promotion_interval[idx];
5833 now = FT_ApproxTime();
5836 for (queue_ScanBackwards(&volume_LRU.q[idx], qp, nqp, rx_queue)) {
5837 vp = (Volume *)((char *)qp - offsetof(Volume, vlru));
5838 promote = (((vp->stats.last_promote + thresh) <= now) &&
5839 (vp->stats.last_get >= vp->stats.last_promote));
5847 /* promote and prepend chain */
5848 queue_MoveChainAfter(&volume_LRU.q[idx+1], &start->vlru, &end->vlru);
5862 /* promote and prepend */
5863 queue_MoveChainAfter(&volume_LRU.q[idx+1], &start->vlru, &end->vlru);
5867 volume_LRU.q[idx].len -= len;
5868 volume_LRU.q[idx+1].len += len;
5871 /* release exclusive access to the two chains */
5873 volume_LRU.last_promotion[idx] = now;
5874 VLRU_EndExclusive_r(&volume_LRU.q[idx+1]);
5875 VLRU_EndExclusive_r(&volume_LRU.q[idx]);
5878 /* run the demotions */
5880 VLRU_Demote_r(int idx)
5883 int len, chaining, demote;
5884 afs_uint32 now, thresh;
5885 struct rx_queue *qp, *nqp;
5886 Volume * vp, *start, *end;
5887 Volume ** salv_flag_vec = NULL;
5888 int salv_vec_offset = 0;
5890 assert(idx == VLRU_QUEUE_MID || idx == VLRU_QUEUE_OLD);
5892 /* get exclusive access to two chains, and drop the glock */
5893 VLRU_Wait_r(&volume_LRU.q[idx-1]);
5894 VLRU_BeginExclusive_r(&volume_LRU.q[idx-1]);
5895 VLRU_Wait_r(&volume_LRU.q[idx]);
5896 VLRU_BeginExclusive_r(&volume_LRU.q[idx]);
5899 /* no big deal if this allocation fails */
5900 if (volume_LRU.q[idx].len) {
5901 salv_flag_vec = (Volume **) malloc(volume_LRU.q[idx].len * sizeof(Volume *));
5904 now = FT_ApproxTime();
5905 thresh = volume_LRU.promotion_interval[idx-1];
5908 for (queue_ScanBackwards(&volume_LRU.q[idx], qp, nqp, rx_queue)) {
5909 vp = (Volume *)((char *)qp - offsetof(Volume, vlru));
5910 demote = (((vp->stats.last_promote + thresh) <= now) &&
5911 (vp->stats.last_get < (now - thresh)));
5913 /* we now do volume update list DONT_SALVAGE flag setting during
5914 * demotion passes */
5915 if (salv_flag_vec &&
5916 !(V_attachFlags(vp) & VOL_HDR_DONTSALV) &&
5918 (vp->updateTime < (now - SALVAGE_INTERVAL)) &&
5919 (V_attachState(vp) == VOL_STATE_ATTACHED)) {
5920 salv_flag_vec[salv_vec_offset++] = vp;
5921 VCreateReservation_r(vp);
5930 /* demote and append chain */
5931 queue_MoveChainBefore(&volume_LRU.q[idx-1], &start->vlru, &end->vlru);
5945 queue_MoveChainBefore(&volume_LRU.q[idx-1], &start->vlru, &end->vlru);
5949 volume_LRU.q[idx].len -= len;
5950 volume_LRU.q[idx-1].len += len;
5953 /* release exclusive access to the two chains */
5955 VLRU_EndExclusive_r(&volume_LRU.q[idx]);
5956 VLRU_EndExclusive_r(&volume_LRU.q[idx-1]);
5958 /* now go back and set the DONT_SALVAGE flags as appropriate */
5959 if (salv_flag_vec) {
5961 for (i = 0; i < salv_vec_offset; i++) {
5962 vp = salv_flag_vec[i];
5963 if (!(V_attachFlags(vp) & VOL_HDR_DONTSALV) &&
5964 (vp->updateTime < (now - SALVAGE_INTERVAL)) &&
5965 (V_attachState(vp) == VOL_STATE_ATTACHED)) {
5968 V_attachFlags(vp) |= VOL_HDR_DONTSALV;
5969 V_dontSalvage(vp) = DONT_SALVAGE;
5970 VUpdateVolume_r(&ec, vp, 0);
5974 VCancelReservation_r(vp);
5976 free(salv_flag_vec);
5980 /* run a pass of the VLRU GC scanner */
5982 VLRU_Scan_r(int idx)
5984 afs_uint32 now, thresh;
5985 struct rx_queue *qp, *nqp;
5986 volatile Volume * vp;
5989 assert(idx == VLRU_QUEUE_NEW || idx == VLRU_QUEUE_CANDIDATE);
5991 /* gain exclusive access to the idx VLRU */
5992 VLRU_Wait_r(&volume_LRU.q[idx]);
5993 VLRU_BeginExclusive_r(&volume_LRU.q[idx]);
5995 if (idx != VLRU_QUEUE_CANDIDATE) {
5996 /* gain exclusive access to the candidate VLRU */
5997 VLRU_Wait_r(&volume_LRU.q[VLRU_QUEUE_CANDIDATE]);
5998 VLRU_BeginExclusive_r(&volume_LRU.q[VLRU_QUEUE_CANDIDATE]);
6001 now = FT_ApproxTime();
6002 thresh = now - VLRU_offline_thresh;
6004 /* perform candidate selection and soft detaching */
6005 if (idx == VLRU_QUEUE_CANDIDATE) {
6006 /* soft detach some volumes from the candidate pool */
6010 for (i=0,queue_ScanBackwards(&volume_LRU.q[idx], qp, nqp, rx_queue)) {
6011 vp = (Volume *)((char *)qp - offsetof(Volume, vlru));
6012 if (i >= VLRU_offline_max) {
6015 /* check timestamp to see if it's a candidate for soft detaching */
6016 if (vp->stats.last_get <= thresh) {
6018 if (VCheckSoftDetach(vp, thresh))
6024 /* scan for volumes to become soft detach candidates */
6025 for (i=1,queue_ScanBackwards(&volume_LRU.q[idx], qp, nqp, rx_queue),i++) {
6026 vp = (Volume *)((char *)qp - offsetof(Volume, vlru));
6028 /* check timestamp to see if it's a candidate for soft detaching */
6029 if (vp->stats.last_get <= thresh) {
6030 VCheckSoftDetachCandidate(vp, thresh);
6033 if (!(i&0x7f)) { /* lock coarsening optimization */
6041 /* relinquish exclusive access to the VLRU chains */
6045 volume_LRU.last_scan[idx] = now;
6046 if (idx != VLRU_QUEUE_CANDIDATE) {
6047 VLRU_EndExclusive_r(&volume_LRU.q[VLRU_QUEUE_CANDIDATE]);
6049 VLRU_EndExclusive_r(&volume_LRU.q[idx]);
6052 /* check whether volume is safe to soft detach
6053 * caller MUST NOT hold a ref count on vp */
6055 VCheckSoftDetach(volatile Volume * vp, afs_uint32 thresh)
6059 if (vp->nUsers || vp->nWaiters)
6062 if (vp->stats.last_get <= thresh) {
6063 ret = VSoftDetachVolume_r(vp, thresh);
6069 /* check whether volume should be made a
6070 * soft detach candidate */
6072 VCheckSoftDetachCandidate(volatile Volume * vp, afs_uint32 thresh)
6075 if (vp->nUsers || vp->nWaiters)
6080 assert(idx == VLRU_QUEUE_NEW);
6082 if (vp->stats.last_get <= thresh) {
6083 /* move to candidate pool */
6084 queue_Remove(&vp->vlru);
6085 volume_LRU.q[VLRU_QUEUE_NEW].len--;
6086 queue_Prepend(&volume_LRU.q[VLRU_QUEUE_CANDIDATE], &vp->vlru);
6087 vp->vlru.idx = VLRU_QUEUE_CANDIDATE;
6088 volume_LRU.q[VLRU_QUEUE_CANDIDATE].len++;
6096 /* begin exclusive access on VLRU */
6098 VLRU_BeginExclusive_r(struct VLRU_q * q)
6100 assert(q->busy == 0);
6104 /* end exclusive access on VLRU */
6106 VLRU_EndExclusive_r(struct VLRU_q * q)
6110 assert(pthread_cond_broadcast(&q->cv) == 0);
6113 /* wait for another thread to end exclusive access on VLRU */
6115 VLRU_Wait_r(struct VLRU_q * q)
6118 VOL_CV_WAIT(&q->cv);
6123 * volume soft detach
6125 * caller MUST NOT hold a ref count on vp */
6127 VSoftDetachVolume_r(volatile Volume * vp, afs_uint32 thresh)
6132 assert(vp->vlru.idx == VLRU_QUEUE_CANDIDATE);
6134 ts_save = vp->stats.last_get;
6135 if (ts_save > thresh)
6138 if (vp->nUsers || vp->nWaiters)
6141 if (VIsExclusiveState(V_attachState(vp))) {
6145 switch (V_attachState(vp)) {
6146 case VOL_STATE_UNATTACHED:
6147 case VOL_STATE_PREATTACHED:
6148 case VOL_STATE_ERROR:
6149 case VOL_STATE_GOING_OFFLINE:
6150 case VOL_STATE_SHUTTING_DOWN:
6151 case VOL_STATE_SALVAGING:
6152 volume_LRU.q[vp->vlru.idx].len--;
6154 /* create and cancel a reservation to
6155 * give the volume an opportunity to
6157 VCreateReservation_r(vp);
6158 queue_Remove(&vp->vlru);
6159 vp->vlru.idx = VLRU_QUEUE_INVALID;
6160 V_attachFlags(vp) &= ~(VOL_ON_VLRU);
6161 VCancelReservation_r(vp);
6165 /* hold the volume and take it offline.
6166 * no need for reservations, as VHold_r
6167 * takes care of that internally. */
6168 if (VHold_r(vp) == 0) {
6169 /* vhold drops the glock, so now we should
6170 * check to make sure we aren't racing against
6171 * other threads. if we are racing, offlining vp
6172 * would be wasteful, and block the scanner for a while
6176 (vp->shuttingDown) ||
6177 (vp->goingOffline) ||
6178 (vp->stats.last_get != ts_save)) {
6179 /* looks like we're racing someone else. bail */
6183 /* pull it off the VLRU */
6184 assert(vp->vlru.idx == VLRU_QUEUE_CANDIDATE);
6185 volume_LRU.q[VLRU_QUEUE_CANDIDATE].len--;
6186 queue_Remove(&vp->vlru);
6187 vp->vlru.idx = VLRU_QUEUE_INVALID;
6188 V_attachFlags(vp) &= ~(VOL_ON_VLRU);
6190 /* take if offline */
6191 VOffline_r(vp, "volume has been soft detached");
6193 /* invalidate the volume header cache */
6194 FreeVolumeHeader(vp);
6197 IncUInt64(&VStats.soft_detaches);
6198 vp->stats.soft_detaches++;
6200 /* put in pre-attached state so demand
6201 * attacher can work on it */
6202 VChangeState_r(vp, VOL_STATE_PREATTACHED);
6208 #endif /* AFS_DEMAND_ATTACH_FS */
6211 /***************************************************/
6212 /* Volume Header Cache routines */
6213 /***************************************************/
6216 * volume header cache.
6218 struct volume_hdr_LRU_t volume_hdr_LRU;
6221 * initialize the volume header cache.
6223 * @param[in] howMany number of header cache entries to preallocate
6225 * @pre VOL_LOCK held. Function has never been called before.
6227 * @post howMany cache entries are allocated, initialized, and added
6228 * to the LRU list. Header cache statistics are initialized.
6230 * @note only applicable to fileServer program type. Should only be
6231 * called once during volume package initialization.
6233 * @internal volume package internal use only.
6236 VInitVolumeHeaderCache(afs_uint32 howMany)
6238 register struct volHeader *hp;
6239 if (programType != fileServer)
6241 queue_Init(&volume_hdr_LRU);
6242 volume_hdr_LRU.stats.free = 0;
6243 volume_hdr_LRU.stats.used = howMany;
6244 volume_hdr_LRU.stats.attached = 0;
6245 hp = (struct volHeader *)(calloc(howMany, sizeof(struct volHeader)));
6249 /* We are using ReleaseVolumeHeader to initialize the values on the header list
6250 * to ensure they have the right values
6252 ReleaseVolumeHeader(hp++);
6256 * get a volume header and attach it to the volume object.
6258 * @param[in] vp pointer to volume object
6260 * @return cache entry status
6261 * @retval 0 volume header was newly attached; cache data is invalid
6262 * @retval 1 volume header was previously attached; cache data is valid
6264 * @pre VOL_LOCK held. For DAFS, lightweight ref must be held on volume object.
6266 * @post volume header attached to volume object. if necessary, header cache
6267 * entry on LRU is synchronized to disk. Header is removed from LRU list.
6269 * @note VOL_LOCK may be dropped
6271 * @warning this interface does not load header data from disk. it merely
6272 * attaches a header object to the volume object, and may sync the old
6273 * header cache data out to disk in the process.
6275 * @internal volume package internal use only.
6278 GetVolumeHeader(register Volume * vp)
6281 register struct volHeader *hd;
6283 static int everLogged = 0;
6285 #ifdef AFS_DEMAND_ATTACH_FS
6286 VolState vp_save, back_save;
6288 /* XXX debug 9/19/05 we've apparently got
6289 * a ref counting bug somewhere that's
6290 * breaking the nUsers == 0 => header on LRU
6292 if (vp->header && queue_IsNotOnQueue(vp->header)) {
6293 Log("nUsers == 0, but header not on LRU\n");
6298 old = (vp->header != NULL); /* old == volume already has a header */
6300 if (programType != fileServer) {
6301 /* for volume utilities, we allocate volHeaders as needed */
6303 hd = (struct volHeader *)calloc(1, sizeof(*vp->header));
6307 #ifdef AFS_DEMAND_ATTACH_FS
6308 V_attachFlags(vp) |= VOL_HDR_ATTACHED;
6312 /* for the fileserver, we keep a volume header cache */
6314 /* the header we previously dropped in the lru is
6315 * still available. pull it off the lru and return */
6318 assert(hd->back == vp);
6320 /* we need to grab a new element off the LRU */
6321 if (queue_IsNotEmpty(&volume_hdr_LRU)) {
6322 /* grab an element and pull off of LRU */
6323 hd = queue_First(&volume_hdr_LRU, volHeader);
6326 /* LRU is empty, so allocate a new volHeader
6327 * this is probably indicative of a leak, so let the user know */
6328 hd = (struct volHeader *)calloc(1, sizeof(struct volHeader));
6331 Log("****Allocated more volume headers, probably leak****\n");
6334 volume_hdr_LRU.stats.free++;
6337 /* this header used to belong to someone else.
6338 * we'll need to check if the header needs to
6339 * be sync'd out to disk */
6341 #ifdef AFS_DEMAND_ATTACH_FS
6342 /* if hd->back were in an exclusive state, then
6343 * its volHeader would not be on the LRU... */
6344 assert(!VIsExclusiveState(V_attachState(hd->back)));
6347 if (hd->diskstuff.inUse) {
6348 /* volume was in use, so we'll need to sync
6349 * its header to disk */
6351 #ifdef AFS_DEMAND_ATTACH_FS
6352 back_save = VChangeState_r(hd->back, VOL_STATE_UPDATING);
6353 vp_save = VChangeState_r(vp, VOL_STATE_HDR_ATTACHING);
6354 VCreateReservation_r(hd->back);
6358 WriteVolumeHeader_r(&error, hd->back);
6359 /* Ignore errors; catch them later */
6361 #ifdef AFS_DEMAND_ATTACH_FS
6366 hd->back->header = NULL;
6367 #ifdef AFS_DEMAND_ATTACH_FS
6368 V_attachFlags(hd->back) &= ~(VOL_HDR_ATTACHED | VOL_HDR_LOADED | VOL_HDR_IN_LRU);
6370 if (hd->diskstuff.inUse) {
6371 VChangeState_r(hd->back, back_save);
6372 VCancelReservation_r(hd->back);
6373 VChangeState_r(vp, vp_save);
6377 volume_hdr_LRU.stats.attached++;
6381 #ifdef AFS_DEMAND_ATTACH_FS
6382 V_attachFlags(vp) |= VOL_HDR_ATTACHED;
6385 volume_hdr_LRU.stats.free--;
6386 volume_hdr_LRU.stats.used++;
6388 IncUInt64(&VStats.hdr_gets);
6389 #ifdef AFS_DEMAND_ATTACH_FS
6390 IncUInt64(&vp->stats.hdr_gets);
6391 vp->stats.last_hdr_get = FT_ApproxTime();
6398 * make sure volume header is attached and contains valid cache data.
6400 * @param[out] ec outbound error code
6401 * @param[in] vp pointer to volume object
6403 * @pre VOL_LOCK held. For DAFS, lightweight ref held on vp.
6405 * @post header cache entry attached, and loaded with valid data, or
6406 * *ec is nonzero, and the header is released back into the LRU.
6408 * @internal volume package internal use only.
6411 LoadVolumeHeader(Error * ec, Volume * vp)
6413 #ifdef AFS_DEMAND_ATTACH_FS
6414 VolState state_save;
6418 if (vp->nUsers == 0 && !GetVolumeHeader(vp)) {
6419 IncUInt64(&VStats.hdr_loads);
6420 state_save = VChangeState_r(vp, VOL_STATE_HDR_LOADING);
6423 ReadHeader(ec, V_diskDataHandle(vp), (char *)&V_disk(vp),
6424 sizeof(V_disk(vp)), VOLUMEINFOMAGIC,
6426 IncUInt64(&vp->stats.hdr_loads);
6427 now = FT_ApproxTime();
6431 V_attachFlags(vp) |= VOL_HDR_LOADED;
6432 vp->stats.last_hdr_load = now;
6434 VChangeState_r(vp, state_save);
6436 #else /* AFS_DEMAND_ATTACH_FS */
6438 if (vp->nUsers == 0 && !GetVolumeHeader(vp)) {
6439 IncUInt64(&VStats.hdr_loads);
6441 ReadHeader(ec, V_diskDataHandle(vp), (char *)&V_disk(vp),
6442 sizeof(V_disk(vp)), VOLUMEINFOMAGIC,
6445 #endif /* AFS_DEMAND_ATTACH_FS */
6447 /* maintain (nUsers==0) => header in LRU invariant */
6448 FreeVolumeHeader(vp);
6453 * release a header cache entry back into the LRU list.
6455 * @param[in] hd pointer to volume header cache object
6457 * @pre VOL_LOCK held.
6459 * @post header cache object appended onto end of LRU list.
6461 * @note only applicable to fileServer program type.
6463 * @note used to place a header cache entry back into the
6464 * LRU pool without invalidating it as a cache entry.
6466 * @internal volume package internal use only.
6469 ReleaseVolumeHeader(register struct volHeader *hd)
6471 if (programType != fileServer)
6473 if (!hd || queue_IsOnQueue(hd)) /* no header, or header already released */
6475 queue_Append(&volume_hdr_LRU, hd);
6476 #ifdef AFS_DEMAND_ATTACH_FS
6478 V_attachFlags(hd->back) |= VOL_HDR_IN_LRU;
6481 volume_hdr_LRU.stats.free++;
6482 volume_hdr_LRU.stats.used--;
6486 * free/invalidate a volume header cache entry.
6488 * @param[in] vp pointer to volume object
6490 * @pre VOL_LOCK is held.
6492 * @post For fileserver, header cache entry is returned to LRU, and it is
6493 * invalidated as a cache entry. For volume utilities, the header
6494 * cache entry is freed.
6496 * @note For fileserver, this should be utilized instead of ReleaseVolumeHeader
6497 * whenever it is necessary to invalidate the header cache entry.
6499 * @see ReleaseVolumeHeader
6501 * @internal volume package internal use only.
6504 FreeVolumeHeader(register Volume * vp)
6506 register struct volHeader *hd = vp->header;
6509 if (programType == fileServer) {
6510 ReleaseVolumeHeader(hd);
6515 #ifdef AFS_DEMAND_ATTACH_FS
6516 V_attachFlags(vp) &= ~(VOL_HDR_ATTACHED | VOL_HDR_IN_LRU | VOL_HDR_LOADED);
6518 volume_hdr_LRU.stats.attached--;
6523 /***************************************************/
6524 /* Volume Hash Table routines */
6525 /***************************************************/
6528 * set size of volume object hash table.
6530 * @param[in] logsize log(2) of desired hash table size
6532 * @return operation status
6534 * @retval -1 failure
6536 * @pre MUST be called prior to VInitVolumePackage
6538 * @post Volume Hash Table will have 2^logsize buckets
6541 VSetVolHashSize(int logsize)
6543 /* 64 to 16384 hash buckets seems like a reasonable range */
6544 if ((logsize < 6 ) || (logsize > 14)) {
6549 VolumeHashTable.Size = 1 << logsize;
6550 VolumeHashTable.Mask = VolumeHashTable.Size - 1;
6552 /* we can't yet support runtime modification of this
6553 * parameter. we'll need a configuration rwlock to
6554 * make runtime modification feasible.... */
6561 * initialize dynamic data structures for volume hash table.
6563 * @post hash table is allocated, and fields are initialized.
6565 * @internal volume package internal use only.
6568 VInitVolumeHash(void)
6572 VolumeHashTable.Table = (VolumeHashChainHead *) calloc(VolumeHashTable.Size,
6573 sizeof(VolumeHashChainHead));
6574 assert(VolumeHashTable.Table != NULL);
6576 for (i=0; i < VolumeHashTable.Size; i++) {
6577 queue_Init(&VolumeHashTable.Table[i]);
6578 #ifdef AFS_DEMAND_ATTACH_FS
6579 assert(pthread_cond_init(&VolumeHashTable.Table[i].chain_busy_cv, NULL) == 0);
6580 #endif /* AFS_DEMAND_ATTACH_FS */
6585 * add a volume object to the hash table.
6587 * @param[in] vp pointer to volume object
6588 * @param[in] hashid hash of volume id
6590 * @pre VOL_LOCK is held. For DAFS, caller must hold a lightweight
6593 * @post volume is added to hash chain.
6595 * @internal volume package internal use only.
6597 * @note For DAFS, VOL_LOCK may be dropped in order to wait for an
6598 * asynchronous hash chain reordering to finish.
6601 AddVolumeToHashTable(register Volume * vp, int hashid)
6603 VolumeHashChainHead * head;
6605 if (queue_IsOnQueue(vp))
6608 head = &VolumeHashTable.Table[VOLUME_HASH(hashid)];
6610 #ifdef AFS_DEMAND_ATTACH_FS
6611 /* wait for the hash chain to become available */
6614 V_attachFlags(vp) |= VOL_IN_HASH;
6615 vp->chainCacheCheck = ++head->cacheCheck;
6616 #endif /* AFS_DEMAND_ATTACH_FS */
6619 vp->hashid = hashid;
6620 queue_Append(head, vp);
6621 vp->vnodeHashOffset = VolumeHashOffset_r();
6625 * delete a volume object from the hash table.
6627 * @param[in] vp pointer to volume object
6629 * @pre VOL_LOCK is held. For DAFS, caller must hold a lightweight
6632 * @post volume is removed from hash chain.
6634 * @internal volume package internal use only.
6636 * @note For DAFS, VOL_LOCK may be dropped in order to wait for an
6637 * asynchronous hash chain reordering to finish.
6640 DeleteVolumeFromHashTable(register Volume * vp)
6642 VolumeHashChainHead * head;
6644 if (!queue_IsOnQueue(vp))
6647 head = &VolumeHashTable.Table[VOLUME_HASH(vp->hashid)];
6649 #ifdef AFS_DEMAND_ATTACH_FS
6650 /* wait for the hash chain to become available */
6653 V_attachFlags(vp) &= ~(VOL_IN_HASH);
6655 #endif /* AFS_DEMAND_ATTACH_FS */
6659 /* do NOT reset hashid to zero, as the online
6660 * salvager package may need to know the volume id
6661 * after the volume is removed from the hash */
6665 * lookup a volume object in the hash table given a volume id.
6667 * @param[out] ec error code return
6668 * @param[in] volumeId volume id
6669 * @param[in] hint volume object which we believe could be the correct
6672 * @return volume object pointer
6673 * @retval NULL no such volume id is registered with the hash table.
6675 * @pre VOL_LOCK is held. For DAFS, caller must hold a lightweight
6678 * @post volume object with the given id is returned. volume object and
6679 * hash chain access statistics are updated. hash chain may have
6682 * @note For DAFS, VOL_LOCK may be dropped in order to wait for an
6683 * asynchronous hash chain reordering operation to finish, or
6684 * in order for us to perform an asynchronous chain reordering.
6686 * @note Hash chain reorderings occur when the access count for the
6687 * volume object being looked up exceeds the sum of the previous
6688 * node's (the node ahead of it in the hash chain linked list)
6689 * access count plus the constant VOLUME_HASH_REORDER_THRESHOLD.
6691 * @note For DAFS, the hint parameter allows us to short-circuit if the
6692 * cacheCheck fields match between the hash chain head and the
6693 * hint volume object.
6696 VLookupVolume_r(Error * ec, VolId volumeId, Volume * hint)
6698 register int looks = 0;
6700 #ifdef AFS_DEMAND_ATTACH_FS
6703 VolumeHashChainHead * head;
6706 head = &VolumeHashTable.Table[VOLUME_HASH(volumeId)];
6708 #ifdef AFS_DEMAND_ATTACH_FS
6709 /* wait for the hash chain to become available */
6712 /* check to see if we can short circuit without walking the hash chain */
6713 if (hint && (hint->chainCacheCheck == head->cacheCheck)) {
6714 IncUInt64(&hint->stats.hash_short_circuits);
6717 #endif /* AFS_DEMAND_ATTACH_FS */
6719 /* someday we need to either do per-chain locks, RWlocks,
6720 * or both for volhash access.
6721 * (and move to a data structure with better cache locality) */
6723 /* search the chain for this volume id */
6724 for(queue_Scan(head, vp, np, Volume)) {
6726 if ((vp->hashid == volumeId)) {
6731 if (queue_IsEnd(head, vp)) {
6735 #ifdef AFS_DEMAND_ATTACH_FS
6736 /* update hash chain statistics */
6739 FillInt64(lks, 0, looks);
6740 AddUInt64(head->looks, lks, &head->looks);
6741 AddUInt64(VStats.hash_looks, lks, &VStats.hash_looks);
6742 IncUInt64(&head->gets);
6747 IncUInt64(&vp->stats.hash_lookups);
6749 /* for demand attach fileserver, we permit occasional hash chain reordering
6750 * so that frequently looked up volumes move towards the head of the chain */
6751 pp = queue_Prev(vp, Volume);
6752 if (!queue_IsEnd(head, pp)) {
6753 FillInt64(thresh, 0, VOLUME_HASH_REORDER_THRESHOLD);
6754 AddUInt64(thresh, pp->stats.hash_lookups, &thresh);
6755 if (GEInt64(vp->stats.hash_lookups, thresh)) {
6756 VReorderHash_r(head, pp, vp);
6760 /* update the short-circuit cache check */
6761 vp->chainCacheCheck = head->cacheCheck;
6763 #endif /* AFS_DEMAND_ATTACH_FS */
6768 #ifdef AFS_DEMAND_ATTACH_FS
6769 /* perform volume hash chain reordering.
6771 * advance a subchain beginning at vp ahead of
6772 * the adjacent subchain ending at pp */
6774 VReorderHash_r(VolumeHashChainHead * head, Volume * pp, Volume * vp)
6776 Volume *tp, *np, *lp;
6777 afs_uint64 move_thresh;
6779 /* this should never be called if the chain is already busy, so
6780 * no need to wait for other exclusive chain ops to finish */
6782 /* this is a rather heavy set of operations,
6783 * so let's set the chain busy flag and drop
6785 VHashBeginExclusive_r(head);
6788 /* scan forward in the chain from vp looking for the last element
6789 * in the chain we want to advance */
6790 FillInt64(move_thresh, 0, VOLUME_HASH_REORDER_CHAIN_THRESH);
6791 AddUInt64(move_thresh, pp->stats.hash_lookups, &move_thresh);
6792 for(queue_ScanFrom(head, vp, tp, np, Volume)) {
6793 if (LTInt64(tp->stats.hash_lookups, move_thresh)) {
6797 lp = queue_Prev(tp, Volume);
6799 /* scan backwards from pp to determine where to splice and
6800 * insert the subchain we're advancing */
6801 for(queue_ScanBackwardsFrom(head, pp, tp, np, Volume)) {
6802 if (GTInt64(tp->stats.hash_lookups, move_thresh)) {
6806 tp = queue_Next(tp, Volume);
6808 /* rebalance chain(vp,...,lp) ahead of chain(tp,...,pp) */
6809 queue_MoveChainBefore(tp,vp,lp);
6812 IncUInt64(&VStats.hash_reorders);
6814 IncUInt64(&head->reorders);
6816 /* wake up any threads waiting for the hash chain */
6817 VHashEndExclusive_r(head);
6821 /* demand-attach fs volume hash
6822 * asynchronous exclusive operations */
6825 * begin an asynchronous exclusive operation on a volume hash chain.
6827 * @param[in] head pointer to volume hash chain head object
6829 * @pre VOL_LOCK held. hash chain is quiescent.
6831 * @post hash chain marked busy.
6833 * @note this interface is used in conjunction with VHashEndExclusive_r and
6834 * VHashWait_r to perform asynchronous (wrt VOL_LOCK) operations on a
6835 * volume hash chain. Its main use case is hash chain reordering, which
6836 * has the potential to be a highly latent operation.
6838 * @see VHashEndExclusive_r
6843 * @internal volume package internal use only.
6846 VHashBeginExclusive_r(VolumeHashChainHead * head)
6848 assert(head->busy == 0);
6853 * relinquish exclusive ownership of a volume hash chain.
6855 * @param[in] head pointer to volume hash chain head object
6857 * @pre VOL_LOCK held. thread owns the hash chain exclusively.
6859 * @post hash chain is marked quiescent. threads awaiting use of
6860 * chain are awakened.
6862 * @see VHashBeginExclusive_r
6867 * @internal volume package internal use only.
6870 VHashEndExclusive_r(VolumeHashChainHead * head)
6874 assert(pthread_cond_broadcast(&head->chain_busy_cv) == 0);
6878 * wait for all asynchronous operations on a hash chain to complete.
6880 * @param[in] head pointer to volume hash chain head object
6882 * @pre VOL_LOCK held.
6884 * @post hash chain object is quiescent.
6886 * @see VHashBeginExclusive_r
6887 * @see VHashEndExclusive_r
6891 * @note This interface should be called before any attempt to
6892 * traverse the hash chain. It is permissible for a thread
6893 * to gain exclusive access to the chain, and then perform
6894 * latent operations on the chain asynchronously wrt the
6897 * @warning if waiting is necessary, VOL_LOCK is dropped
6899 * @internal volume package internal use only.
6902 VHashWait_r(VolumeHashChainHead * head)
6904 while (head->busy) {
6905 VOL_CV_WAIT(&head->chain_busy_cv);
6908 #endif /* AFS_DEMAND_ATTACH_FS */
6911 /***************************************************/
6912 /* Volume by Partition List routines */
6913 /***************************************************/
6916 * demand attach fileserver adds a
6917 * linked list of volumes to each
6918 * partition object, thus allowing
6919 * for quick enumeration of all
6920 * volumes on a partition
6923 #ifdef AFS_DEMAND_ATTACH_FS
6925 * add a volume to its disk partition VByPList.
6927 * @param[in] vp pointer to volume object
6929 * @pre either the disk partition VByPList is owned exclusively
6930 * by the calling thread, or the list is quiescent and
6933 * @post volume is added to disk partition VByPList
6937 * @warning it is the caller's responsibility to ensure list
6940 * @see VVByPListWait_r
6941 * @see VVByPListBeginExclusive_r
6942 * @see VVByPListEndExclusive_r
6944 * @internal volume package internal use only.
6947 AddVolumeToVByPList_r(Volume * vp)
6949 if (queue_IsNotOnQueue(&vp->vol_list)) {
6950 queue_Append(&vp->partition->vol_list, &vp->vol_list);
6951 V_attachFlags(vp) |= VOL_ON_VBYP_LIST;
6952 vp->partition->vol_list.len++;
6957 * delete a volume from its disk partition VByPList.
6959 * @param[in] vp pointer to volume object
6961 * @pre either the disk partition VByPList is owned exclusively
6962 * by the calling thread, or the list is quiescent and
6965 * @post volume is removed from the disk partition VByPList
6969 * @warning it is the caller's responsibility to ensure list
6972 * @see VVByPListWait_r
6973 * @see VVByPListBeginExclusive_r
6974 * @see VVByPListEndExclusive_r
6976 * @internal volume package internal use only.
6979 DeleteVolumeFromVByPList_r(Volume * vp)
6981 if (queue_IsOnQueue(&vp->vol_list)) {
6982 queue_Remove(&vp->vol_list);
6983 V_attachFlags(vp) &= ~(VOL_ON_VBYP_LIST);
6984 vp->partition->vol_list.len--;
6989 * begin an asynchronous exclusive operation on a VByPList.
6991 * @param[in] dp pointer to disk partition object
6993 * @pre VOL_LOCK held. VByPList is quiescent.
6995 * @post VByPList marked busy.
6997 * @note this interface is used in conjunction with VVByPListEndExclusive_r and
6998 * VVByPListWait_r to perform asynchronous (wrt VOL_LOCK) operations on a
7001 * @see VVByPListEndExclusive_r
7002 * @see VVByPListWait_r
7006 * @internal volume package internal use only.
7008 /* take exclusive control over the list */
7010 VVByPListBeginExclusive_r(struct DiskPartition64 * dp)
7012 assert(dp->vol_list.busy == 0);
7013 dp->vol_list.busy = 1;
7017 * relinquish exclusive ownership of a VByPList.
7019 * @param[in] dp pointer to disk partition object
7021 * @pre VOL_LOCK held. thread owns the VByPList exclusively.
7023 * @post VByPList is marked quiescent. threads awaiting use of
7024 * the list are awakened.
7026 * @see VVByPListBeginExclusive_r
7027 * @see VVByPListWait_r
7031 * @internal volume package internal use only.
7034 VVByPListEndExclusive_r(struct DiskPartition64 * dp)
7036 assert(dp->vol_list.busy);
7037 dp->vol_list.busy = 0;
7038 assert(pthread_cond_broadcast(&dp->vol_list.cv) == 0);
7042 * wait for all asynchronous operations on a VByPList to complete.
7044 * @param[in] dp pointer to disk partition object
7046 * @pre VOL_LOCK is held.
7048 * @post disk partition's VByP list is quiescent
7052 * @note This interface should be called before any attempt to
7053 * traverse the VByPList. It is permissible for a thread
7054 * to gain exclusive access to the list, and then perform
7055 * latent operations on the list asynchronously wrt the
7058 * @warning if waiting is necessary, VOL_LOCK is dropped
7060 * @see VVByPListEndExclusive_r
7061 * @see VVByPListBeginExclusive_r
7063 * @internal volume package internal use only.
7066 VVByPListWait_r(struct DiskPartition64 * dp)
7068 while (dp->vol_list.busy) {
7069 VOL_CV_WAIT(&dp->vol_list.cv);
7072 #endif /* AFS_DEMAND_ATTACH_FS */
7074 /***************************************************/
7075 /* Volume Cache Statistics routines */
7076 /***************************************************/
7079 VPrintCacheStats_r(void)
7081 afs_uint32 get_hi, get_lo, load_hi, load_lo;
7082 register struct VnodeClassInfo *vcp;
7083 vcp = &VnodeClassInfo[vLarge];
7084 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);
7085 vcp = &VnodeClassInfo[vSmall];
7086 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);
7087 SplitInt64(VStats.hdr_gets, get_hi, get_lo);
7088 SplitInt64(VStats.hdr_loads, load_hi, load_lo);
7089 Log("Volume header cache, %d entries, %d gets, %d replacements\n",
7090 VStats.hdr_cache_size, get_lo, load_lo);
7094 VPrintCacheStats(void)
7097 VPrintCacheStats_r();
7101 #ifdef AFS_DEMAND_ATTACH_FS
7103 UInt64ToDouble(afs_uint64 * x)
7105 static double c32 = 4.0 * 1.073741824 * 1000000000.0;
7107 SplitInt64(*x, h, l);
7108 return (((double)h) * c32) + ((double) l);
7112 DoubleToPrintable(double x, char * buf, int len)
7114 static double billion = 1000000000.0;
7117 y[0] = (afs_uint32) (x / (billion * billion));
7118 y[1] = (afs_uint32) ((x - (((double)y[0]) * billion * billion)) / billion);
7119 y[2] = (afs_uint32) (x - ((((double)y[0]) * billion * billion) + (((double)y[1]) * billion)));
7122 snprintf(buf, len, "%d%09d%09d", y[0], y[1], y[2]);
7124 snprintf(buf, len, "%d%09d", y[1], y[2]);
7126 snprintf(buf, len, "%d", y[2]);
7132 struct VLRUExtStatsEntry {
7136 struct VLRUExtStats {
7142 } queue_info[VLRU_QUEUE_INVALID];
7143 struct VLRUExtStatsEntry * vec;
7147 * add a 256-entry fudge factor onto the vector in case state changes
7148 * out from under us.
7150 #define VLRU_EXT_STATS_VEC_LEN_FUDGE 256
7153 * collect extended statistics for the VLRU subsystem.
7155 * @param[out] stats pointer to stats structure to be populated
7156 * @param[in] nvols number of volumes currently known to exist
7158 * @pre VOL_LOCK held
7160 * @post stats->vec allocated and populated
7162 * @return operation status
7167 VVLRUExtStats_r(struct VLRUExtStats * stats, afs_uint32 nvols)
7169 afs_uint32 cur, idx, len;
7170 struct rx_queue * qp, * nqp;
7172 struct VLRUExtStatsEntry * vec;
7174 len = nvols + VLRU_EXT_STATS_VEC_LEN_FUDGE;
7175 vec = stats->vec = calloc(len,
7176 sizeof(struct VLRUExtStatsEntry));
7182 for (idx = VLRU_QUEUE_NEW; idx < VLRU_QUEUE_INVALID; idx++) {
7183 VLRU_Wait_r(&volume_LRU.q[idx]);
7184 VLRU_BeginExclusive_r(&volume_LRU.q[idx]);
7187 stats->queue_info[idx].start = cur;
7189 for (queue_Scan(&volume_LRU.q[idx], qp, nqp, rx_queue)) {
7191 /* out of space in vec */
7194 vp = (Volume *)((char *)qp - offsetof(Volume, vlru));
7195 vec[cur].volid = vp->hashid;
7199 stats->queue_info[idx].len = cur - stats->queue_info[idx].start;
7202 VLRU_EndExclusive_r(&volume_LRU.q[idx]);
7210 #define ENUMTOSTRING(en) #en
7211 #define ENUMCASE(en) \
7213 return ENUMTOSTRING(en); \
7217 vlru_idx_to_string(int idx)
7220 ENUMCASE(VLRU_QUEUE_NEW);
7221 ENUMCASE(VLRU_QUEUE_MID);
7222 ENUMCASE(VLRU_QUEUE_OLD);
7223 ENUMCASE(VLRU_QUEUE_CANDIDATE);
7224 ENUMCASE(VLRU_QUEUE_HELD);
7225 ENUMCASE(VLRU_QUEUE_INVALID);
7227 return "**UNKNOWN**";
7232 VPrintExtendedCacheStats_r(int flags)
7235 afs_uint32 vol_sum = 0;
7242 struct stats looks, gets, reorders, len;
7243 struct stats ch_looks, ch_gets, ch_reorders;
7245 VolumeHashChainHead *head;
7247 struct VLRUExtStats vlru_stats;
7249 /* zero out stats */
7250 memset(&looks, 0, sizeof(struct stats));
7251 memset(&gets, 0, sizeof(struct stats));
7252 memset(&reorders, 0, sizeof(struct stats));
7253 memset(&len, 0, sizeof(struct stats));
7254 memset(&ch_looks, 0, sizeof(struct stats));
7255 memset(&ch_gets, 0, sizeof(struct stats));
7256 memset(&ch_reorders, 0, sizeof(struct stats));
7258 for (i = 0; i < VolumeHashTable.Size; i++) {
7259 head = &VolumeHashTable.Table[i];
7262 VHashBeginExclusive_r(head);
7265 ch_looks.sum = UInt64ToDouble(&head->looks);
7266 ch_gets.sum = UInt64ToDouble(&head->gets);
7267 ch_reorders.sum = UInt64ToDouble(&head->reorders);
7269 /* update global statistics */
7271 looks.sum += ch_looks.sum;
7272 gets.sum += ch_gets.sum;
7273 reorders.sum += ch_reorders.sum;
7274 len.sum += (double)head->len;
7275 vol_sum += head->len;
7278 len.min = (double) head->len;
7279 len.max = (double) head->len;
7280 looks.min = ch_looks.sum;
7281 looks.max = ch_looks.sum;
7282 gets.min = ch_gets.sum;
7283 gets.max = ch_gets.sum;
7284 reorders.min = ch_reorders.sum;
7285 reorders.max = ch_reorders.sum;
7287 if (((double)head->len) < len.min)
7288 len.min = (double) head->len;
7289 if (((double)head->len) > len.max)
7290 len.max = (double) head->len;
7291 if (ch_looks.sum < looks.min)
7292 looks.min = ch_looks.sum;
7293 else if (ch_looks.sum > looks.max)
7294 looks.max = ch_looks.sum;
7295 if (ch_gets.sum < gets.min)
7296 gets.min = ch_gets.sum;
7297 else if (ch_gets.sum > gets.max)
7298 gets.max = ch_gets.sum;
7299 if (ch_reorders.sum < reorders.min)
7300 reorders.min = ch_reorders.sum;
7301 else if (ch_reorders.sum > reorders.max)
7302 reorders.max = ch_reorders.sum;
7306 if ((flags & VOL_STATS_PER_CHAIN2) && queue_IsNotEmpty(head)) {
7307 /* compute detailed per-chain stats */
7308 struct stats hdr_loads, hdr_gets;
7309 double v_looks, v_loads, v_gets;
7311 /* initialize stats with data from first element in chain */
7312 vp = queue_First(head, Volume);
7313 v_looks = UInt64ToDouble(&vp->stats.hash_lookups);
7314 v_loads = UInt64ToDouble(&vp->stats.hdr_loads);
7315 v_gets = UInt64ToDouble(&vp->stats.hdr_gets);
7316 ch_gets.min = ch_gets.max = v_looks;
7317 hdr_loads.min = hdr_loads.max = v_loads;
7318 hdr_gets.min = hdr_gets.max = v_gets;
7319 hdr_loads.sum = hdr_gets.sum = 0;
7321 vp = queue_Next(vp, Volume);
7323 /* pull in stats from remaining elements in chain */
7324 for (queue_ScanFrom(head, vp, vp, np, Volume)) {
7325 v_looks = UInt64ToDouble(&vp->stats.hash_lookups);
7326 v_loads = UInt64ToDouble(&vp->stats.hdr_loads);
7327 v_gets = UInt64ToDouble(&vp->stats.hdr_gets);
7329 hdr_loads.sum += v_loads;
7330 hdr_gets.sum += v_gets;
7332 if (v_looks < ch_gets.min)
7333 ch_gets.min = v_looks;
7334 else if (v_looks > ch_gets.max)
7335 ch_gets.max = v_looks;
7337 if (v_loads < hdr_loads.min)
7338 hdr_loads.min = v_loads;
7339 else if (v_loads > hdr_loads.max)
7340 hdr_loads.max = v_loads;
7342 if (v_gets < hdr_gets.min)
7343 hdr_gets.min = v_gets;
7344 else if (v_gets > hdr_gets.max)
7345 hdr_gets.max = v_gets;
7348 /* compute per-chain averages */
7349 ch_gets.avg = ch_gets.sum / ((double)head->len);
7350 hdr_loads.avg = hdr_loads.sum / ((double)head->len);
7351 hdr_gets.avg = hdr_gets.sum / ((double)head->len);
7353 /* dump per-chain stats */
7354 Log("Volume hash chain %d : len=%d, looks=%s, reorders=%s\n",
7356 DoubleToPrintable(ch_looks.sum, pr_buf[0], sizeof(pr_buf[0])),
7357 DoubleToPrintable(ch_reorders.sum, pr_buf[1], sizeof(pr_buf[1])));
7358 Log("\tVolume gets : min=%s, max=%s, avg=%s, total=%s\n",
7359 DoubleToPrintable(ch_gets.min, pr_buf[0], sizeof(pr_buf[0])),
7360 DoubleToPrintable(ch_gets.max, pr_buf[1], sizeof(pr_buf[1])),
7361 DoubleToPrintable(ch_gets.avg, pr_buf[2], sizeof(pr_buf[2])),
7362 DoubleToPrintable(ch_gets.sum, pr_buf[3], sizeof(pr_buf[3])));
7363 Log("\tHDR gets : min=%s, max=%s, avg=%s, total=%s\n",
7364 DoubleToPrintable(hdr_gets.min, pr_buf[0], sizeof(pr_buf[0])),
7365 DoubleToPrintable(hdr_gets.max, pr_buf[1], sizeof(pr_buf[1])),
7366 DoubleToPrintable(hdr_gets.avg, pr_buf[2], sizeof(pr_buf[2])),
7367 DoubleToPrintable(hdr_gets.sum, pr_buf[3], sizeof(pr_buf[3])));
7368 Log("\tHDR loads : min=%s, max=%s, avg=%s, total=%s\n",
7369 DoubleToPrintable(hdr_loads.min, pr_buf[0], sizeof(pr_buf[0])),
7370 DoubleToPrintable(hdr_loads.max, pr_buf[1], sizeof(pr_buf[1])),
7371 DoubleToPrintable(hdr_loads.avg, pr_buf[2], sizeof(pr_buf[2])),
7372 DoubleToPrintable(hdr_loads.sum, pr_buf[3], sizeof(pr_buf[3])));
7373 } else if (flags & VOL_STATS_PER_CHAIN) {
7374 /* dump simple per-chain stats */
7375 Log("Volume hash chain %d : len=%d, looks=%s, gets=%s, reorders=%s\n",
7377 DoubleToPrintable(ch_looks.sum, pr_buf[0], sizeof(pr_buf[0])),
7378 DoubleToPrintable(ch_gets.sum, pr_buf[1], sizeof(pr_buf[1])),
7379 DoubleToPrintable(ch_reorders.sum, pr_buf[2], sizeof(pr_buf[2])));
7383 VHashEndExclusive_r(head);
7388 /* compute global averages */
7389 len.avg = len.sum / ((double)VolumeHashTable.Size);
7390 looks.avg = looks.sum / ((double)VolumeHashTable.Size);
7391 gets.avg = gets.sum / ((double)VolumeHashTable.Size);
7392 reorders.avg = reorders.sum / ((double)VolumeHashTable.Size);
7394 /* dump global stats */
7395 Log("Volume hash summary: %d buckets\n", VolumeHashTable.Size);
7396 Log(" chain length : min=%s, max=%s, avg=%s, total=%s\n",
7397 DoubleToPrintable(len.min, pr_buf[0], sizeof(pr_buf[0])),
7398 DoubleToPrintable(len.max, pr_buf[1], sizeof(pr_buf[1])),
7399 DoubleToPrintable(len.avg, pr_buf[2], sizeof(pr_buf[2])),
7400 DoubleToPrintable(len.sum, pr_buf[3], sizeof(pr_buf[3])));
7401 Log(" looks : min=%s, max=%s, avg=%s, total=%s\n",
7402 DoubleToPrintable(looks.min, pr_buf[0], sizeof(pr_buf[0])),
7403 DoubleToPrintable(looks.max, pr_buf[1], sizeof(pr_buf[1])),
7404 DoubleToPrintable(looks.avg, pr_buf[2], sizeof(pr_buf[2])),
7405 DoubleToPrintable(looks.sum, pr_buf[3], sizeof(pr_buf[3])));
7406 Log(" gets : min=%s, max=%s, avg=%s, total=%s\n",
7407 DoubleToPrintable(gets.min, pr_buf[0], sizeof(pr_buf[0])),
7408 DoubleToPrintable(gets.max, pr_buf[1], sizeof(pr_buf[1])),
7409 DoubleToPrintable(gets.avg, pr_buf[2], sizeof(pr_buf[2])),
7410 DoubleToPrintable(gets.sum, pr_buf[3], sizeof(pr_buf[3])));
7411 Log(" reorders : min=%s, max=%s, avg=%s, total=%s\n",
7412 DoubleToPrintable(reorders.min, pr_buf[0], sizeof(pr_buf[0])),
7413 DoubleToPrintable(reorders.max, pr_buf[1], sizeof(pr_buf[1])),
7414 DoubleToPrintable(reorders.avg, pr_buf[2], sizeof(pr_buf[2])),
7415 DoubleToPrintable(reorders.sum, pr_buf[3], sizeof(pr_buf[3])));
7417 /* print extended disk related statistics */
7419 struct DiskPartition64 * diskP;
7420 afs_uint32 vol_count[VOLMAXPARTS+1];
7421 byte part_exists[VOLMAXPARTS+1];
7425 memset(vol_count, 0, sizeof(vol_count));
7426 memset(part_exists, 0, sizeof(part_exists));
7430 for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
7432 vol_count[id] = diskP->vol_list.len;
7433 part_exists[id] = 1;
7437 for (i = 0; i <= VOLMAXPARTS; i++) {
7438 if (part_exists[i]) {
7439 /* XXX while this is currently safe, it is a violation
7440 * of the VGetPartitionById_r interface contract. */
7441 diskP = VGetPartitionById_r(i, 0);
7443 Log("Partition %s has %d online volumes\n",
7444 VPartitionPath(diskP), diskP->vol_list.len);
7451 /* print extended VLRU statistics */
7452 if (VVLRUExtStats_r(&vlru_stats, vol_sum) == 0) {
7453 afs_uint32 idx, cur, lpos;
7457 Log("VLRU State Dump:\n\n");
7459 for (idx = VLRU_QUEUE_NEW; idx < VLRU_QUEUE_INVALID; idx++) {
7460 Log("\t%s:\n", vlru_idx_to_string(idx));
7463 for (cur = vlru_stats.queue_info[idx].start;
7464 cur < vlru_stats.queue_info[idx].len;
7466 line[lpos++] = vlru_stats.vec[cur].volid;
7468 Log("\t\t%u, %u, %u, %u, %u,\n",
7469 line[0], line[1], line[2], line[3], line[4]);
7478 Log("\t\t%u, %u, %u, %u, %u\n",
7479 line[0], line[1], line[2], line[3], line[4]);
7484 free(vlru_stats.vec);
7491 VPrintExtendedCacheStats(int flags)
7494 VPrintExtendedCacheStats_r(flags);
7497 #endif /* AFS_DEMAND_ATTACH_FS */