2 * Copyright 2009-2010, Sine Nomine Associates 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
12 * volume group membership cache
13 * asynchronous partition scanner
16 #include <afsconfig.h>
17 #include <afs/param.h>
19 #ifdef AFS_DEMAND_ATTACH_FS
27 #include <afs/assert.h>
30 #include <sys/param.h>
32 #if defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
35 #include <afs/afsutil.h>
38 #include <afs/afsint.h>
42 #include "viceinode.h"
44 #include "partition.h"
45 #include <afs/errors.h>
47 #define __VOL_VG_CACHE_IMPL 1
50 #include "vg_cache_impl.h"
53 #define afs_open open64
54 #else /* !O_LARGEFILE */
56 #endif /* !O_LARGEFILE */
58 static int _VVGC_scan_table_init(VVGCache_scan_table_t * tbl);
59 static int _VVGC_scan_table_add(VVGCache_scan_table_t * tbl,
60 struct DiskPartition64 * dp,
63 static int _VVGC_scan_table_flush(VVGCache_scan_table_t * tbl,
64 struct DiskPartition64 * dp);
65 static void * _VVGC_scanner_thread(void *);
66 static int _VVGC_scan_partition(struct DiskPartition64 * part);
67 static VVGCache_dlist_entry_t * _VVGC_dlist_lookup_r(struct DiskPartition64 *dp,
70 static void _VVGC_flush_dlist(struct DiskPartition64 *dp);
73 * init a thread-local scan table.
75 * @param[in] tbl scan table
77 * @return operation status
83 _VVGC_scan_table_init(VVGCache_scan_table_t * tbl)
85 memset(tbl, 0, sizeof(*tbl));
91 * add an entry to the thread-local scan table.
93 * @param[in] tbl scan table
94 * @param[in] dp disk partition object
95 * @param[in] volid volume id
96 * @param[in] parent parent volume id
98 * @pre VOL_LOCK is NOT held
100 * @note if the table is full, this routine will acquire
101 * VOL_LOCK and flush the table to the global one.
103 * @return operation status
105 * @retval nonzero a VVGCache_entry_add_r operation failed during a
106 * flush of the thread-local table
111 _VVGC_scan_table_add(VVGCache_scan_table_t * tbl,
112 struct DiskPartition64 * dp,
118 if (tbl->idx == VVGC_SCAN_TBL_LEN) {
119 code = _VVGC_scan_table_flush(tbl, dp);
122 tbl->entries[tbl->idx].volid = volid;
123 tbl->entries[tbl->idx].parent = parent;
130 * flush thread-local scan table to the global VG cache.
132 * @param[in] tbl scan table
133 * @param[in] dp disk partition object
135 * @pre VOL_LOCK is NOT held
137 * @return operation status
139 * @retval nonzero a VVGCache_entry_add_r operation failed during a
140 * flush of the thread-local table
145 _VVGC_scan_table_flush(VVGCache_scan_table_t * tbl,
146 struct DiskPartition64 * dp)
148 int code = 0, res, i;
150 unsigned long newvols, newvgs;
152 newvols = tbl->newvols;
153 newvgs = tbl->newvgs;
157 for (i = 0; i < tbl->idx; i++) {
159 * We need to check the 'to-delete' list and prevent adding any entries
160 * that are on it. The volser could potentially create a volume in one
161 * VG, then delete it and put it on another VG. If we are doing a scan
162 * when that happens, tbl->entries could have the entries for trying to
163 * put the vol on both VGs, though at least one of them will also be on
164 * the dlist. If we put everything in tbl->entries on the VGC then try
165 * to delete afterwards, putting one entry on the VGC cause an error,
166 * and we'll fail to add it. So instead, avoid adding any new VGC
167 * entries if it is on the dlist.
169 if (_VVGC_dlist_lookup_r(dp, tbl->entries[i].parent,
170 tbl->entries[i].volid)) {
173 res = VVGCache_entry_add_r(dp,
174 tbl->entries[i].parent,
175 tbl->entries[i].volid,
185 /* flush the to-delete list while we're here. We don't need to preserve
186 * the list across the entire scan, and flushing it each time we flush
187 * a scan table will keep the size of the dlist down */
188 _VVGC_flush_dlist(dp);
192 ViceLog(125, ("VVGC_scan_table_flush: flushed %d entries from "
193 "scan table to global VG cache\n", tbl->idx));
194 ViceLog(125, ("VVGC_scan_table_flush: %s total: %lu vols, %lu groups\n",
195 VPartitionPath(dp), newvols, newvgs));
197 res = _VVGC_scan_table_init(tbl);
202 tbl->newvols = newvols;
203 tbl->newvgs = newvgs;
209 * record a volume header found by VWalkVolumeHeaders in a VGC scan table.
211 * @param[in] dp the disk partition
212 * @param[in] name full path to the .vol header (unused)
213 * @param[in] hdr the header data
214 * @param[in] last whether this is the last try or not (unused)
215 * @param[in] rock actually a VVGCache_scan_table_t* to add the volume to
217 * @return operation status
219 * @retval -1 fatal error adding vol to the scan table
222 _VVGC_RecordHeader(struct DiskPartition64 *dp, const char *name,
223 struct VolumeDiskHeader *hdr, int last, void *rock)
226 VVGCache_scan_table_t *tbl;
227 tbl = (VVGCache_scan_table_t *)rock;
229 code = _VVGC_scan_table_add(tbl, dp, hdr->id, hdr->parent);
231 ViceLog(0, ("VVGC_scan_partition: error %d adding volume %s to scan table\n",
239 * unlink a faulty volume header found by VWalkVolumeHeaders.
241 * @param[in] dp the disk partition (unused)
242 * @param[in] name the full path to the .vol header
243 * @param[in] hdr the header data (unused)
244 * @param[in] rock unused
247 _VVGC_UnlinkHeader(struct DiskPartition64 *dp, const char *name,
248 struct VolumeDiskHeader *hdr, void *rock)
250 ViceLog(0, ("%s is not a legitimate volume header file; deleted\n", name));
252 ViceLog(0, ("Unable to unlink %s (errno = %d)\n",
258 * scan a disk partition for .vol files
260 * @param[in] part disk partition object
262 * @pre VOL_LOCK is NOT held
264 * @return operation status
266 * @retval -1 invalid disk partition object
267 * @retval -2 failed to flush stale entries for this partition
272 _VVGC_scan_partition(struct DiskPartition64 * part)
276 VVGCache_scan_table_t tbl;
277 char *part_path = NULL;
279 code = _VVGC_scan_table_init(&tbl);
281 ViceLog(0, ("VVGC_scan_partition: could not init scan table; error = %d\n",
285 part_path = VPartitionPath(part);
286 if (part_path == NULL) {
287 ViceLog(0, ("VVGC_scan_partition: invalid partition object given; aborting scan\n"));
293 res = _VVGC_flush_part_r(part);
295 ViceLog(0, ("VVGC_scan_partition: error flushing partition %s; error = %d\n",
296 VPartitionPath(part), res));
304 dirp = opendir(part_path);
306 ViceLog(0, ("VVGC_scan_partition: could not open %s, aborting scan; error = %d\n",
312 ViceLog(5, ("VVGC_scan_partition: scanning partition %s for VG cache\n",
315 code = VWalkVolumeHeaders(part, part_path, _VVGC_RecordHeader,
316 _VVGC_UnlinkHeader, &tbl);
321 _VVGC_scan_table_flush(&tbl, part);
329 ViceLog(0, ("VVGC_scan_partition: error %d while scanning %s\n",
332 ViceLog(0, ("VVGC_scan_partition: finished scanning %s: %lu volumes in %lu groups\n",
333 part_path, tbl.newvols, tbl.newvgs));
338 _VVGC_flush_dlist(part);
339 free(VVGCache.part[part->index].dlist_hash_buckets);
340 VVGCache.part[part->index].dlist_hash_buckets = NULL;
343 _VVGC_state_change(part, VVGC_PART_STATE_INVALID);
345 _VVGC_state_change(part, VVGC_PART_STATE_VALID);
357 _VVGC_scanner_thread(void * args)
359 struct DiskPartition64 *part = args;
362 code = _VVGC_scan_partition(part);
364 ViceLog(0, ("Error: _VVGC_scan_partition failed with code %d for partition %s\n",
365 code, VPartitionPath(part)));
372 * start a background scan.
374 * @param[in] dp disk partition object
376 * @return operation status
378 * @retval -1 internal error
379 * @retval -3 racing against another thread
384 _VVGC_scan_start(struct DiskPartition64 * dp)
388 pthread_attr_t attrs;
391 if (_VVGC_state_change(dp,
392 VVGC_PART_STATE_UPDATING)
393 == VVGC_PART_STATE_UPDATING) {
395 ViceLog(0, ("VVGC_scan_partition: race detected; aborting scanning partition %s\n",
396 VPartitionPath(dp)));
401 /* initialize partition's to-delete list */
402 VVGCache.part[dp->index].dlist_hash_buckets =
403 malloc(VolumeHashTable.Size * sizeof(struct rx_queue));
404 if (!VVGCache.part[dp->index].dlist_hash_buckets) {
408 for (i = 0; i < VolumeHashTable.Size; i++) {
409 queue_Init(&VVGCache.part[dp->index].dlist_hash_buckets[i]);
412 code = pthread_attr_init(&attrs);
417 code = pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);
422 code = pthread_create(&tid, &attrs, &_VVGC_scanner_thread, dp);
425 VVGCache_part_state_t old_state;
427 ViceLog(0, ("_VVGC_scan_start: pthread_create failed with %d\n", code));
429 old_state = _VVGC_state_change(dp, VVGC_PART_STATE_INVALID);
430 assert(old_state == VVGC_PART_STATE_UPDATING);
435 ViceLog(0, ("_VVGC_scan_start failed with code %d for partition %s\n",
436 code, VPartitionPath(dp)));
437 if (VVGCache.part[dp->index].dlist_hash_buckets) {
438 free(VVGCache.part[dp->index].dlist_hash_buckets);
439 VVGCache.part[dp->index].dlist_hash_buckets = NULL;
447 * looks up an entry on the to-delete list, if it exists.
449 * @param[in] dp the partition whose dlist we are looking at
450 * @param[in] parent the parent volume ID we're looking for
451 * @param[in] child the child volume ID we're looking for
453 * @return a pointer to the entry in the dlist for that entry
454 * @retval NULL the requested entry does not exist in the dlist
456 static VVGCache_dlist_entry_t *
457 _VVGC_dlist_lookup_r(struct DiskPartition64 *dp, VolumeId parent,
460 int bucket = VVGC_HASH(child);
461 VVGCache_dlist_entry_t *ent, *nent;
463 for (queue_Scan(&VVGCache.part[dp->index].dlist_hash_buckets[bucket],
465 VVGCache_dlist_entry)) {
467 if (ent->child == child && ent->parent == parent) {
476 * delete all of the entries in the dlist from the VGC.
478 * Traverses the to-delete list for the specified partition, and deletes
479 * the specified entries from the global VGC. Also deletes the entries from
480 * the dlist itself as it goes along.
482 * @param[in] dp the partition whose dlist we are flushing
485 _VVGC_flush_dlist(struct DiskPartition64 *dp)
488 VVGCache_dlist_entry_t *ent, *nent;
490 for (i = 0; i < VolumeHashTable.Size; i++) {
491 for (queue_Scan(&VVGCache.part[dp->index].dlist_hash_buckets[i],
493 VVGCache_dlist_entry)) {
495 _VVGC_entry_purge_r(dp, ent->parent, ent->child);
503 * add a VGC entry to the partition's to-delete list.
505 * This adds a VGC entry (a parent/child pair) to a list of VGC entries to
506 * be deleted from the VGC at the end of a VGC scan. This is necessary,
507 * while a VGC scan is ocurring, volumes may be deleted. Since a VGC scan
508 * scans a partition in VVGC_SCAN_TBL_LEN chunks, a VGC delete operation
509 * may delete a volume, only for it to be added again when the VGC scan's
510 * table adds it to the VGC. So when a VGC entry is deleted and a VGC scan
511 * is running, this function must be called to ensure it does not come
514 * @param[in] dp the partition to whose dlist we are adding
515 * @param[in] parent the parent volumeID of the VGC entry
516 * @param[in] child the child volumeID of the VGC entry
518 * @return operation status
520 * @retval ENOMEM memory allocation error
522 * @pre VVGCache.part[dp->index].state == VVGC_PART_STATE_UPDATING
524 * @internal VGC use only
527 _VVGC_dlist_add_r(struct DiskPartition64 *dp, VolumeId parent,
530 int bucket = VVGC_HASH(child);
531 VVGCache_dlist_entry_t *entry;
533 entry = malloc(sizeof(*entry));
538 entry->child = child;
539 entry->parent = parent;
541 queue_Append(&VVGCache.part[dp->index].dlist_hash_buckets[bucket],
547 * delete a VGC entry from the partition's to-delete list.
549 * When a VGC scan is ocurring, and a volume is removed, but then created
550 * again, we need to ensure that it does not get deleted from being on the
551 * dlist. Call this function whenever adding a new entry to the VGC during
552 * a VGC scan to ensure it doesn't get deleted later.
554 * @param[in] dp the partition from whose dlist we are deleting
555 * @param[in] parent the parent volumeID of the VGC entry
556 * @param[in] child the child volumeID of the VGC entry
558 * @return operation status
560 * @retval ENOENT the specified VGC entry is not on the dlist
562 * @pre VVGCache.part[dp->index].state == VVGC_PART_STATE_UPDATING
564 * @internal VGC use only
566 * @see _VVGC_dlist_add_r
569 _VVGC_dlist_del_r(struct DiskPartition64 *dp, VolumeId parent,
572 VVGCache_dlist_entry_t *ent;
574 ent = _VVGC_dlist_lookup_r(dp, parent, child);
585 #endif /* AFS_DEMAND_ATTACH_FS */