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>
21 #ifdef HAVE_SYS_FILE_H
25 #ifdef AFS_DEMAND_ATTACH_FS
28 #include <afs/afsutil.h>
31 #include <afs/afsint.h>
35 #include "viceinode.h"
37 #include "partition.h"
38 #include <afs/errors.h>
40 #define __VOL_VG_CACHE_IMPL 1
43 #include "vg_cache_impl.h"
45 static int _VVGC_scan_table_init(VVGCache_scan_table_t * tbl);
46 static int _VVGC_scan_table_add(VVGCache_scan_table_t * tbl,
47 struct DiskPartition64 * dp,
50 static int _VVGC_scan_table_flush(VVGCache_scan_table_t * tbl,
51 struct DiskPartition64 * dp);
52 static void * _VVGC_scanner_thread(void *);
53 static int _VVGC_scan_partition(struct DiskPartition64 * part);
54 static VVGCache_dlist_entry_t * _VVGC_dlist_lookup_r(struct DiskPartition64 *dp,
57 static void _VVGC_flush_dlist(struct DiskPartition64 *dp);
60 * init a thread-local scan table.
62 * @param[in] tbl scan table
64 * @return operation status
70 _VVGC_scan_table_init(VVGCache_scan_table_t * tbl)
72 memset(tbl, 0, sizeof(*tbl));
78 * add an entry to the thread-local scan table.
80 * @param[in] tbl scan table
81 * @param[in] dp disk partition object
82 * @param[in] volid volume id
83 * @param[in] parent parent volume id
85 * @pre VOL_LOCK is NOT held
87 * @note if the table is full, this routine will acquire
88 * VOL_LOCK and flush the table to the global one.
90 * @return operation status
92 * @retval nonzero a VVGCache_entry_add_r operation failed during a
93 * flush of the thread-local table
98 _VVGC_scan_table_add(VVGCache_scan_table_t * tbl,
99 struct DiskPartition64 * dp,
105 if (tbl->idx == VVGC_SCAN_TBL_LEN) {
106 code = _VVGC_scan_table_flush(tbl, dp);
109 tbl->entries[tbl->idx].volid = volid;
110 tbl->entries[tbl->idx].parent = parent;
117 * flush thread-local scan table to the global VG cache.
119 * @param[in] tbl scan table
120 * @param[in] dp disk partition object
122 * @pre VOL_LOCK is NOT held
124 * @return operation status
126 * @retval nonzero a VVGCache_entry_add_r operation failed during a
127 * flush of the thread-local table
132 _VVGC_scan_table_flush(VVGCache_scan_table_t * tbl,
133 struct DiskPartition64 * dp)
135 int code = 0, res, i;
137 unsigned long newvols, newvgs;
139 newvols = tbl->newvols;
140 newvgs = tbl->newvgs;
144 for (i = 0; i < tbl->idx; i++) {
146 * We need to check the 'to-delete' list and prevent adding any entries
147 * that are on it. The volser could potentially create a volume in one
148 * VG, then delete it and put it on another VG. If we are doing a scan
149 * when that happens, tbl->entries could have the entries for trying to
150 * put the vol on both VGs, though at least one of them will also be on
151 * the dlist. If we put everything in tbl->entries on the VGC then try
152 * to delete afterwards, putting one entry on the VGC cause an error,
153 * and we'll fail to add it. So instead, avoid adding any new VGC
154 * entries if it is on the dlist.
156 if (_VVGC_dlist_lookup_r(dp, tbl->entries[i].parent,
157 tbl->entries[i].volid)) {
160 res = VVGCache_entry_add_r(dp,
161 tbl->entries[i].parent,
162 tbl->entries[i].volid,
172 /* flush the to-delete list while we're here. We don't need to preserve
173 * the list across the entire scan, and flushing it each time we flush
174 * a scan table will keep the size of the dlist down */
175 _VVGC_flush_dlist(dp);
179 ViceLog(125, ("VVGC_scan_table_flush: flushed %d entries from "
180 "scan table to global VG cache\n", tbl->idx));
181 ViceLog(125, ("VVGC_scan_table_flush: %s total: %lu vols, %lu groups\n",
182 VPartitionPath(dp), newvols, newvgs));
184 res = _VVGC_scan_table_init(tbl);
189 tbl->newvols = newvols;
190 tbl->newvgs = newvgs;
196 * record a volume header found by VWalkVolumeHeaders in a VGC scan table.
198 * @param[in] dp the disk partition
199 * @param[in] name full path to the .vol header (unused)
200 * @param[in] hdr the header data
201 * @param[in] last whether this is the last try or not (unused)
202 * @param[in] rock actually a VVGCache_scan_table_t* to add the volume to
204 * @return operation status
206 * @retval -1 fatal error adding vol to the scan table
209 _VVGC_RecordHeader(struct DiskPartition64 *dp, const char *name,
210 struct VolumeDiskHeader *hdr, int last, void *rock)
213 VVGCache_scan_table_t *tbl;
214 tbl = (VVGCache_scan_table_t *)rock;
216 code = _VVGC_scan_table_add(tbl, dp, hdr->id, hdr->parent);
218 ViceLog(0, ("VVGC_scan_partition: error %d adding volume %s to scan table\n",
226 * unlink a faulty volume header found by VWalkVolumeHeaders.
228 * @param[in] dp the disk partition (unused)
229 * @param[in] name the full path to the .vol header
230 * @param[in] hdr the header data (unused)
231 * @param[in] rock unused
234 _VVGC_UnlinkHeader(struct DiskPartition64 *dp, const char *name,
235 struct VolumeDiskHeader *hdr, void *rock)
237 ViceLog(0, ("%s is not a legitimate volume header file; deleted\n", name));
239 ViceLog(0, ("Unable to unlink %s (errno = %d)\n",
245 * scan a disk partition for .vol files
247 * @param[in] part disk partition object
249 * @pre VOL_LOCK is NOT held
251 * @return operation status
253 * @retval -1 invalid disk partition object
254 * @retval -2 failed to flush stale entries for this partition
259 _VVGC_scan_partition(struct DiskPartition64 * part)
263 VVGCache_scan_table_t tbl;
264 char *part_path = NULL;
266 code = _VVGC_scan_table_init(&tbl);
268 ViceLog(0, ("VVGC_scan_partition: could not init scan table; error = %d\n",
272 part_path = VPartitionPath(part);
273 if (part_path == NULL) {
274 ViceLog(0, ("VVGC_scan_partition: invalid partition object given; aborting scan\n"));
280 res = _VVGC_flush_part_r(part);
282 ViceLog(0, ("VVGC_scan_partition: error flushing partition %s; error = %d\n",
283 VPartitionPath(part), res));
291 dirp = opendir(part_path);
293 ViceLog(0, ("VVGC_scan_partition: could not open %s, aborting scan; error = %d\n",
299 ViceLog(5, ("VVGC_scan_partition: scanning partition %s for VG cache\n",
302 code = VWalkVolumeHeaders(part, part_path, _VVGC_RecordHeader,
303 _VVGC_UnlinkHeader, &tbl);
308 _VVGC_scan_table_flush(&tbl, part);
316 ViceLog(0, ("VVGC_scan_partition: error %d while scanning %s\n",
319 ViceLog(0, ("VVGC_scan_partition: finished scanning %s: %lu volumes in %lu groups\n",
320 part_path, tbl.newvols, tbl.newvgs));
325 _VVGC_flush_dlist(part);
326 free(VVGCache.part[part->index].dlist_hash_buckets);
327 VVGCache.part[part->index].dlist_hash_buckets = NULL;
330 _VVGC_state_change(part, VVGC_PART_STATE_INVALID);
332 _VVGC_state_change(part, VVGC_PART_STATE_VALID);
344 _VVGC_scanner_thread(void * args)
346 struct DiskPartition64 *part = args;
349 code = _VVGC_scan_partition(part);
351 ViceLog(0, ("Error: _VVGC_scan_partition failed with code %d for partition %s\n",
352 code, VPartitionPath(part)));
359 * start a background scan.
361 * @param[in] dp disk partition object
363 * @return operation status
365 * @retval -1 internal error
366 * @retval -3 racing against another thread
371 _VVGC_scan_start(struct DiskPartition64 * dp)
375 pthread_attr_t attrs;
378 if (_VVGC_state_change(dp,
379 VVGC_PART_STATE_UPDATING)
380 == VVGC_PART_STATE_UPDATING) {
382 ViceLog(0, ("VVGC_scan_partition: race detected; aborting scanning partition %s\n",
383 VPartitionPath(dp)));
388 /* initialize partition's to-delete list */
389 VVGCache.part[dp->index].dlist_hash_buckets =
390 malloc(VolumeHashTable.Size * sizeof(struct rx_queue));
391 if (!VVGCache.part[dp->index].dlist_hash_buckets) {
395 for (i = 0; i < VolumeHashTable.Size; i++) {
396 queue_Init(&VVGCache.part[dp->index].dlist_hash_buckets[i]);
399 code = pthread_attr_init(&attrs);
404 code = pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);
409 code = pthread_create(&tid, &attrs, &_VVGC_scanner_thread, dp);
412 VVGCache_part_state_t old_state;
414 ViceLog(0, ("_VVGC_scan_start: pthread_create failed with %d\n", code));
416 old_state = _VVGC_state_change(dp, VVGC_PART_STATE_INVALID);
417 osi_Assert(old_state == VVGC_PART_STATE_UPDATING);
422 ViceLog(0, ("_VVGC_scan_start failed with code %d for partition %s\n",
423 code, VPartitionPath(dp)));
424 if (VVGCache.part[dp->index].dlist_hash_buckets) {
425 free(VVGCache.part[dp->index].dlist_hash_buckets);
426 VVGCache.part[dp->index].dlist_hash_buckets = NULL;
434 * looks up an entry on the to-delete list, if it exists.
436 * @param[in] dp the partition whose dlist we are looking at
437 * @param[in] parent the parent volume ID we're looking for
438 * @param[in] child the child volume ID we're looking for
440 * @return a pointer to the entry in the dlist for that entry
441 * @retval NULL the requested entry does not exist in the dlist
443 static VVGCache_dlist_entry_t *
444 _VVGC_dlist_lookup_r(struct DiskPartition64 *dp, VolumeId parent,
447 int bucket = VVGC_HASH(child);
448 VVGCache_dlist_entry_t *ent, *nent;
450 for (queue_Scan(&VVGCache.part[dp->index].dlist_hash_buckets[bucket],
452 VVGCache_dlist_entry)) {
454 if (ent->child == child && ent->parent == parent) {
463 * delete all of the entries in the dlist from the VGC.
465 * Traverses the to-delete list for the specified partition, and deletes
466 * the specified entries from the global VGC. Also deletes the entries from
467 * the dlist itself as it goes along.
469 * @param[in] dp the partition whose dlist we are flushing
472 _VVGC_flush_dlist(struct DiskPartition64 *dp)
475 VVGCache_dlist_entry_t *ent, *nent;
477 for (i = 0; i < VolumeHashTable.Size; i++) {
478 for (queue_Scan(&VVGCache.part[dp->index].dlist_hash_buckets[i],
480 VVGCache_dlist_entry)) {
482 _VVGC_entry_purge_r(dp, ent->parent, ent->child);
490 * add a VGC entry to the partition's to-delete list.
492 * This adds a VGC entry (a parent/child pair) to a list of VGC entries to
493 * be deleted from the VGC at the end of a VGC scan. This is necessary,
494 * while a VGC scan is ocurring, volumes may be deleted. Since a VGC scan
495 * scans a partition in VVGC_SCAN_TBL_LEN chunks, a VGC delete operation
496 * may delete a volume, only for it to be added again when the VGC scan's
497 * table adds it to the VGC. So when a VGC entry is deleted and a VGC scan
498 * is running, this function must be called to ensure it does not come
501 * @param[in] dp the partition to whose dlist we are adding
502 * @param[in] parent the parent volumeID of the VGC entry
503 * @param[in] child the child volumeID of the VGC entry
505 * @return operation status
507 * @retval ENOMEM memory allocation error
509 * @pre VVGCache.part[dp->index].state == VVGC_PART_STATE_UPDATING
511 * @internal VGC use only
514 _VVGC_dlist_add_r(struct DiskPartition64 *dp, VolumeId parent,
517 int bucket = VVGC_HASH(child);
518 VVGCache_dlist_entry_t *entry;
520 entry = malloc(sizeof(*entry));
525 entry->child = child;
526 entry->parent = parent;
528 queue_Append(&VVGCache.part[dp->index].dlist_hash_buckets[bucket],
534 * delete a VGC entry from the partition's to-delete list.
536 * When a VGC scan is ocurring, and a volume is removed, but then created
537 * again, we need to ensure that it does not get deleted from being on the
538 * dlist. Call this function whenever adding a new entry to the VGC during
539 * a VGC scan to ensure it doesn't get deleted later.
541 * @param[in] dp the partition from whose dlist we are deleting
542 * @param[in] parent the parent volumeID of the VGC entry
543 * @param[in] child the child volumeID of the VGC entry
545 * @return operation status
547 * @retval ENOENT the specified VGC entry is not on the dlist
549 * @pre VVGCache.part[dp->index].state == VVGC_PART_STATE_UPDATING
551 * @internal VGC use only
553 * @see _VVGC_dlist_add_r
556 _VVGC_dlist_del_r(struct DiskPartition64 *dp, VolumeId parent,
559 VVGCache_dlist_entry_t *ent;
561 ent = _VVGC_dlist_lookup_r(dp, parent, child);
572 #endif /* AFS_DEMAND_ATTACH_FS */