volser-preclude-alternate-partition-clones-20080404
[openafs.git] / src / vol / volume.h
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  *
9  * Portions Copyright (c) 2006-2008 Sine Nomine Associates
10  */
11
12 /*
13         System:         VICE-TWO
14         Module:         volume.h
15         Institution:    The Information Technology Center, Carnegie-Mellon University
16
17  */
18
19 #ifndef __volume_h
20 #define __volume_h  1
21
22 #include <afs/afssyscalls.h>
23 #include "voldefs.h"
24 #include "ihandle.h"
25 #define VolumeWriteable(vp)             (V_type(vp)==readwriteVolume)
26 #define VolumeWriteable2(vol)           (vol.type == readwriteVolume)
27 typedef bit32 FileOffset;       /* Offset in this file */
28 #define Date afs_uint32
29 #include "daemon_com.h"
30 #include "fssync.h"
31
32 #if 0
33 /** turn this on if you suspect a volume package locking bug */
34 #define VOL_LOCK_DEBUG 1
35 #endif
36
37 #ifdef VOL_LOCK_DEBUG
38 #define VOL_LOCK_ASSERT_HELD \
39     assert(vol_glock_holder == pthread_self())
40 #define VOL_LOCK_ASSERT_UNHELD \
41     assert(vol_glock_holder == 0)
42 #define _VOL_LOCK_SET_HELD \
43     vol_glock_holder = pthread_self()
44 #define _VOL_LOCK_SET_UNHELD \
45     vol_glock_holder = 0
46 #define VOL_LOCK_DBG_CV_WAIT_END \
47     do { \
48         VOL_LOCK_ASSERT_UNHELD; \
49         _VOL_LOCK_SET_HELD; \
50     } while(0)
51 #define VOL_LOCK_DBG_CV_WAIT_BEGIN \
52     do { \
53          VOL_LOCK_ASSERT_HELD; \
54          _VOL_LOCK_SET_UNHELD; \
55     } while(0)
56 #else
57 #define VOL_LOCK_ASSERT_HELD
58 #define VOL_LOCK_ASSERT_UNHELD
59 #define VOL_LOCK_DBG_CV_WAIT_BEGIN
60 #define VOL_LOCK_DBG_CV_WAIT_END
61 #endif
62
63
64 #ifdef AFS_PTHREAD_ENV
65 #include <assert.h>
66 #include <pthread.h>
67 extern pthread_mutex_t vol_glock_mutex;
68 extern pthread_mutex_t vol_trans_mutex;
69 extern pthread_cond_t vol_put_volume_cond;
70 extern pthread_cond_t vol_sleep_cond;
71 extern int vol_attach_threads;
72 #ifdef VOL_LOCK_DEBUG
73 extern pthread_t vol_glock_holder;
74 #define VOL_LOCK \
75     do { \
76         assert(pthread_mutex_lock(&vol_glock_mutex) == 0); \
77         assert(vol_glock_holder == 0); \
78         vol_glock_holder = pthread_self(); \
79     } while (0)
80 #define VOL_UNLOCK \
81     do { \
82         VOL_LOCK_ASSERT_HELD; \
83         vol_glock_holder = 0; \
84         assert(pthread_mutex_unlock(&vol_glock_mutex) == 0); \
85     } while (0)
86 #define VOL_CV_WAIT(cv) \
87     do { \
88         VOL_LOCK_DBG_CV_WAIT_BEGIN; \
89         assert(pthread_cond_wait((cv), &vol_glock_mutex) == 0); \
90         VOL_LOCK_DBG_CV_WAIT_END; \
91     } while (0)
92 #else /* !VOL_LOCK_DEBUG */
93 #define VOL_LOCK \
94     assert(pthread_mutex_lock(&vol_glock_mutex) == 0)
95 #define VOL_UNLOCK \
96     assert(pthread_mutex_unlock(&vol_glock_mutex) == 0)
97 #define VOL_CV_WAIT(cv) assert(pthread_cond_wait((cv), &vol_glock_mutex) == 0)
98 #endif /* !VOL_LOCK_DEBUG */
99 #define VSALVSYNC_LOCK \
100     assert(pthread_mutex_lock(&vol_salvsync_mutex) == 0)
101 #define VSALVSYNC_UNLOCK \
102     assert(pthread_mutex_unlock(&vol_salvsync_mutex) == 0)
103 #define VTRANS_LOCK \
104     assert(pthread_mutex_lock(&vol_trans_mutex) == 0)
105 #define VTRANS_UNLOCK \
106     assert(pthread_mutex_unlock(&vol_trans_mutex) == 0)
107 #else /* AFS_PTHREAD_ENV */
108 #define VOL_LOCK
109 #define VOL_UNLOCK
110 #define VSALVSYNC_LOCK
111 #define VSALVSYNC_UNLOCK
112 #define VTRANS_LOCK
113 #define VTRANS_UNLOCK
114 #endif /* AFS_PTHREAD_ENV */
115
116 /**
117  * volume package program type enumeration.
118  */
119 typedef enum {
120     fileServer          = 1,    /**< the fileserver process */
121     volumeUtility       = 2,    /**< volserver, or a 
122                                  *   single volume salvager (non-dafs) */
123     salvager            = 3,    /**< standalone whole-partition salvager */
124     salvageServer       = 4,    /**< dafs online salvager */
125     debugUtility        = 5     /**< fssync-debug or similar utility */
126 } ProgramType;
127 extern ProgramType programType; /* The type of program using the package */
128
129 /* Some initialization parameters for the volume package */
130 /* Add new initialization parameters here */
131 extern int (*V_BreakVolumeCallbacks) ();
132 extern int (*vol_PollProc) ();
133 #define DOPOLL  ((vol_PollProc)? (*vol_PollProc)() : 0)
134
135 #ifdef AFS_DEMAND_ATTACH_FS
136 /**
137  * variable error return code based upon programType and DAFS presence
138  */
139 #define DAFS_VSALVAGE   ((programType == fileServer) ? VSALVAGING : VSALVAGE)
140 #else
141 #define DAFS_VSALVAGE   (VSALVAGE)
142 #endif
143
144 struct versionStamp {           /* Version stamp for critical volume files */
145     bit32 magic;                /* Magic number */
146     bit32 version;              /* Version number of this file, or software
147                                  * that created this file */
148 };
149
150 #ifdef AFS_DEMAND_ATTACH_FS
151 /**
152  * demand attach volume state enumeration.
153  *
154  * @note values must be contiguous in order for VIsValidState() to work correctly
155  */
156 typedef enum {
157     VOL_STATE_UNATTACHED        = 0,    /**< volume is unattached */
158     VOL_STATE_PREATTACHED       = 1,    /**< volume has been pre-attached */
159     VOL_STATE_ATTACHING         = 2,    /**< volume is transitioning to fully attached */
160     VOL_STATE_ATTACHED          = 3,    /**< volume has been fully attached */
161     VOL_STATE_UPDATING          = 4,    /**< volume is updating on-disk structures */
162     VOL_STATE_GET_BITMAP        = 5,    /**< volume is getting bitmap entries */
163     VOL_STATE_HDR_LOADING       = 6,    /**< volume is loading disk header */
164     VOL_STATE_HDR_ATTACHING     = 7,    /**< volume is getting a header from the LRU */
165     VOL_STATE_SHUTTING_DOWN     = 8,    /**< volume is shutting down */
166     VOL_STATE_GOING_OFFLINE     = 9,    /**< volume is going offline */
167     VOL_STATE_OFFLINING         = 10,   /**< volume is transitioning to offline */
168     VOL_STATE_DETACHING         = 11,   /**< volume is transitioning to detached */
169     VOL_STATE_SALVSYNC_REQ      = 12,   /**< volume is blocked on a salvsync request */
170     VOL_STATE_SALVAGING         = 13,   /**< volume is being salvaged */
171     VOL_STATE_ERROR             = 14,   /**< volume is in an error state */
172     VOL_STATE_VNODE_ALLOC       = 15,   /**< volume is busy allocating a new vnode */
173     VOL_STATE_VNODE_GET         = 16,   /**< volume is busy getting vnode disk data */
174     VOL_STATE_VNODE_CLOSE       = 17,   /**< volume is busy closing vnodes */
175     VOL_STATE_VNODE_RELEASE     = 18,   /**< volume is busy releasing vnodes */
176     VOL_STATE_VLRU_ADD          = 19,   /**< volume is busy being added to a VLRU queue */
177     /* please add new states directly above this line */
178     VOL_STATE_FREED             = 20,   /**< debugging aid */
179     VOL_STATE_COUNT             = 21,   /**< total number of valid states */
180 } VolState;
181
182 /**
183  * V_attachFlags bits.
184  */
185 enum VolFlags {
186     VOL_HDR_ATTACHED      = 0x1,     /**< volume header is attached to Volume struct */
187     VOL_HDR_LOADED        = 0x2,     /**< volume header contents are valid */
188     VOL_HDR_IN_LRU        = 0x4,     /**< volume header is in LRU */
189     VOL_IN_HASH           = 0x8,     /**< volume is in hash table */
190     VOL_ON_VBYP_LIST      = 0x10,    /**< volume is on VByP list */
191     VOL_IS_BUSY           = 0x20,    /**< volume is not to be free()d */
192     VOL_ON_VLRU           = 0x40,    /**< volume is on the VLRU */
193     VOL_HDR_DONTSALV      = 0x80,    /**< volume header DONTSALVAGE flag is set */
194 };
195
196 /* VPrintExtendedCacheStats flags */
197 #define VOL_STATS_PER_CHAIN   0x1  /**< compute simple per-chain stats */
198 #define VOL_STATS_PER_CHAIN2  0x2  /**< compute per-chain stats that require scanning
199                                     *   every element of the chain */
200
201 /* VLRU_SetOptions options */
202 #define VLRU_SET_THRESH       1
203 #define VLRU_SET_INTERVAL     2
204 #define VLRU_SET_MAX          3
205 #define VLRU_SET_ENABLED      4
206
207 /**
208  * VLRU queue names.
209  */
210 typedef enum {
211     VLRU_QUEUE_NEW        = 0,  /**< LRU queue for new volumes */
212     VLRU_QUEUE_MID        = 1,  /**< survivor generation */
213     VLRU_QUEUE_OLD        = 2,  /**< old generation */
214     VLRU_QUEUE_CANDIDATE  = 3,  /**< soft detach candidate pool */
215     VLRU_QUEUE_HELD       = 4,  /*   volumes which are not allowed
216                                  *   to be soft detached */
217     VLRU_QUEUE_INVALID    = 5,  /**< invalid queue id */
218 } VLRUQueueName;
219
220 /* default scanner timing parameters */
221 #define VLRU_DEFAULT_OFFLINE_THRESH (60*60*2) /* 2 hours */
222 #define VLRU_DEFAULT_OFFLINE_INTERVAL (60*2) /* 2 minutes */
223 #define VLRU_DEFAULT_OFFLINE_MAX 8 /* 8 volumes */
224
225 #endif /* AFS_DEMAND_ATTACH_FS */
226
227
228 /* Magic numbers and version stamps for each type of file */
229 #define VOLUMEHEADERMAGIC       ((bit32)0x88a1bb3c)
230 #define VOLUMEINFOMAGIC         ((bit32)0x78a1b2c5)
231 #define SMALLINDEXMAGIC         0x99776655
232 #define LARGEINDEXMAGIC         0x88664433
233 #define MOUNTMAGIC              0x9a8b7c6d
234 #define ACLMAGIC                0x88877712
235 #define LINKTABLEMAGIC          0x99877712
236
237 #define VOLUMEHEADERVERSION     1
238 #define VOLUMEINFOVERSION       1
239 #define SMALLINDEXVERSION       1
240 #define LARGEINDEXVERSION       1
241 #define MOUNTVERSION            1
242 #define ACLVERSION              1
243 #define LINKTABLEVERSION        1
244
245 /*
246  * Define whether we are keeping detailed statistics on volume dealings.
247  */
248 #define OPENAFS_VOL_STATS       1
249
250 #if OPENAFS_VOL_STATS
251 /*
252  * Define various indices and counts used in keeping volume-level statistics.
253  */
254 #define VOL_STATS_NUM_RWINFO_FIELDS 4
255
256 #define VOL_STATS_SAME_NET      0       /*Within same site (total) */
257 #define VOL_STATS_SAME_NET_AUTH 1       /*Within same site (authenticated);
258                                          * (must be 1 more than above) */
259 #define VOL_STATS_DIFF_NET      2       /*From external site (total) */
260 #define VOL_STATS_DIFF_NET_AUTH 3       /*From external site (authenticated)
261                                          * (must be 1 more than above) */
262
263 #define VOL_STATS_NUM_TIME_RANGES 6
264
265 #define VOL_STATS_TIME_CAP_0        60  /*60 seconds */
266 #define VOL_STATS_TIME_CAP_1       600  /*10 minutes, in seconds */
267 #define VOL_STATS_TIME_CAP_2      3600  /*1 hour, in seconds */
268 #define VOL_STATS_TIME_CAP_3     86400  /*1 day, in seconds */
269 #define VOL_STATS_TIME_CAP_4    604800  /*1 week, in seconds */
270
271 #define VOL_STATS_NUM_TIME_FIELDS 6
272
273 #define VOL_STATS_TIME_IDX_0    0       /*0 secs to 60 secs */
274 #define VOL_STATS_TIME_IDX_1    1       /*1 min to 10 mins */
275 #define VOL_STATS_TIME_IDX_2    2       /*10 mins to 60 mins */
276 #define VOL_STATS_TIME_IDX_3    3       /*1 hr to 24 hrs */
277 #define VOL_STATS_TIME_IDX_4    4       /*1 day to 7 days */
278 #define VOL_STATS_TIME_IDX_5    5       /*Greater than 1 week */
279 #endif /* OPENAFS_VOL_STATS */
280
281 /* Volume header.  This is the contents of the named file representing
282  * the volume.  Read-only by the file server!
283  */
284 typedef struct VolumeHeader {
285     struct versionStamp stamp;  /* Must be first field */
286     VolumeId id;                /* Volume number */
287     VolumeId parent;            /* Read-write volume number (or this volume
288                                  * number if this is a read-write volume) */
289     Inode volumeInfo;
290     Inode smallVnodeIndex;
291     Inode largeVnodeIndex;
292     Inode volumeAcl;
293     Inode volumeMountTable;
294     Inode linkTable;
295 } VolumeHeader_t;
296
297
298 typedef struct VolumeDiskHeader {
299     struct versionStamp stamp;  /* Must be first field */
300     VolumeId id;                /* Volume number */
301     VolumeId parent;            /* Read-write volume number (or this volume
302                                  * number if this is a read-write volume) */
303     afs_int32 volumeInfo_lo;
304     afs_int32 smallVnodeIndex_lo;
305     afs_int32 largeVnodeIndex_lo;
306     afs_int32 volumeAcl_lo;
307     afs_int32 volumeMountTable_lo;
308     afs_int32 volumeInfo_hi;
309     afs_int32 smallVnodeIndex_hi;
310     afs_int32 largeVnodeIndex_hi;
311     afs_int32 volumeAcl_hi;
312     afs_int32 volumeMountTable_hi;
313     afs_int32 linkTable_lo;
314     afs_int32 linkTable_hi;
315     /* If you add fields, add them before here and reduce the size of  array */
316     bit32 reserved[3];
317 } VolumeDiskHeader_t;
318
319 /* A vnode index file header */
320 struct IndexFileHeader {
321     struct versionStamp stamp;
322 };
323
324
325 /******************************************************************************/
326 /* Volume Data which is stored on disk and can also be maintained in memory.  */
327 /******************************************************************************/
328 typedef struct VolumeDiskData {
329     struct versionStamp stamp;  /* Must be first field */
330     VolumeId id;                /* Volume id--unique over all systems */
331 #define VNAMESIZE 32            /* including 0 byte */
332     char name[VNAMESIZE];       /* Unofficial name for the volume */
333     byte inUse;                 /* Volume is being used (perhaps it is online),
334                                  * or the system crashed while it was used */
335     byte inService;             /* Volume in service, not necessarily on line
336                                  * This bit is set by an operator/system
337                                  * programmer.  Manually taking a volume offline
338                                  * always clears the inService bit. Taking
339                                  * it out of service also takes it offline */
340     byte blessed;               /* Volume is administratively blessed with
341                                  * the ability to go on line.  Set by a system
342                                  * administrator. Clearing this bit will
343                                  * take the volume offline */
344     byte needsSalvaged;         /* Volume needs salvaged--an unrecoverable
345                                  * error occured to the volume.  Note:  a volume
346                                  * may still require salvage even if this
347                                  * flag isn't set--e.g. if a system crash
348                                  * occurred while the volume was on line. */
349     bit32 uniquifier;           /* Next vnode uniquifier for this volume */
350     int type;                   /* */
351     VolId parentId;             /* Id of parent, if type==readonly */
352     VolId cloneId;              /* Latest read-only clone, if type==readwrite,
353                                  * 0 if the volume has never been cloned.  Note: the
354                                  * indicated volume does not necessarily exist (it
355                                  * may have been deleted since cloning). */
356     VolId backupId;             /* Latest backup copy of this read write volume */
357     VolId restoredFromId;       /* The id in the dump this volume was restored from--used simply
358                                  * to make sure that an incremental dump is not restored on top
359                                  * of something inappropriate:  Note:  this field itself is NEVER
360                                  * dumped!!! */
361     byte needsCallback;         /* Set by the salvager if anything was changed
362                                  * about the volume.  Note:  this is not set by
363                                  * clone/makebackups when setting the copy-on-write
364                                  * flag in directories; this flag is not seen by
365                                  * the clients. */
366 #define DESTROY_ME      0xD3
367     byte destroyMe;             /* If this is set to DESTROY_ME, then the salvager should destroy
368                                  * this volume; it is bogus (left over from an aborted  volume move,
369                                  * for example).  Note:  if this flag is on, then inService should
370                                  * be OFF--only the salvager checks this flag */
371 #ifdef ALPHA_DUX40_ENV
372 #define DONT_SALVAGE    0xE6
373 #else                           /* ALPHA_DUX40_ENV */
374 #define DONT_SALVAGE    0xE5
375 #endif                          /* ALPHA_DUX40_ENV */
376     byte dontSalvage;           /* If this is on, then don't bother salvaging this volume */
377     byte reserveb3;
378
379     bit32 reserved1[6];
380
381
382     /* Administrative stuff */
383     int maxquota;               /* Quota maximum, 1K blocks */
384     int minquota;               /* Quota minimum, 1K blocks */
385     int maxfiles;               /* Maximum number of files (i.e. inodes) */
386     bit32 accountNumber;        /* Uninterpreted account number */
387     bit32 owner;                /* The person administratively responsible
388                                  * for this volume */
389     int reserved2[8];           /* Other administrative constraints */
390
391     /* Resource usage & statistics */
392     int filecount;              /* Actual number of files */
393     int diskused;               /* Actual disk space used, 1K blocks */
394     int dayUse;                 /* Metric for today's usage of this volume so far */
395     int weekUse[7];             /* Usage of the volume for the last week.
396                                  * weekUse[0] is for most recent complete 24 hour period
397                                  * of measurement; week[6] is 7 days ago */
398     Date dayUseDate;            /* Date the dayUse statistics refer to; the week use stats
399                                  * are the preceding 7 days */
400     unsigned int volUpdateCounter; /*incremented at every update of volume*/
401     int reserved3[10];          /* Other stats here */
402
403     /* Server supplied dates */
404     Date creationDate;          /* Creation date for a read/write
405                                  * volume; cloning date for original copy of
406                                  * a readonly volume (replicated volumes have
407                                  * the same creation date) */
408     Date accessDate;            /* Last access time by a user, large granularity */
409     Date updateDate;            /* Last modification by user */
410     Date expirationDate;        /* 0 if it never expires */
411     Date backupDate;            /* last time a backup clone was taken */
412
413     /* Time that this copy of this volume was made.  NEVER backed up.  This field is only
414      * set when the copy is created */
415     Date copyDate;
416
417 #if OPENAFS_VOL_STATS
418     bit32 stat_initialized;     /*Are the stat fields below set up? */
419     bit32 reserved4[7];
420 #else
421     bit32 reserved4[8];
422 #endif                          /* OPENAFS_VOL_STATS */
423
424     /* messages */
425 #define VMSGSIZE 128
426     char offlineMessage[VMSGSIZE];      /* Why the volume is offline */
427 #if OPENAFS_VOL_STATS
428 #define VOL_STATS_BYTES 128
429     /*
430      * Keep per-volume aggregate statistics on type and distance of access,
431      * along with authorship info.
432      */
433     bit32 stat_reads[VOL_STATS_NUM_RWINFO_FIELDS];
434     bit32 stat_writes[VOL_STATS_NUM_RWINFO_FIELDS];
435     bit32 stat_fileSameAuthor[VOL_STATS_NUM_TIME_FIELDS];
436     bit32 stat_fileDiffAuthor[VOL_STATS_NUM_TIME_FIELDS];
437     bit32 stat_dirSameAuthor[VOL_STATS_NUM_TIME_FIELDS];
438     bit32 stat_dirDiffAuthor[VOL_STATS_NUM_TIME_FIELDS];
439 #else
440     char motd[VMSGSIZE];        /* Volume "message of the day" */
441 #endif                          /* OPENAFS_VOL_STATS */
442
443 } VolumeDiskData;
444
445
446 /**************************************/
447 /* Memory resident volume information */
448 /**************************************/
449
450 /** 
451  * global volume package stats.
452  */
453 typedef struct VolPkgStats {
454 #ifdef AFS_DEMAND_ATTACH_FS
455     /*
456      * demand attach fs
457      * extended volume package statistics
458      */
459
460     /* levels */
461     afs_uint32 state_levels[VOL_STATE_COUNT]; /**< volume state transition counters */
462
463     /* counters */
464     afs_uint64 hash_looks;           /**< number of hash chain element traversals */
465     afs_uint64 hash_reorders;        /**< number of hash chain reorders */
466     afs_uint64 salvages;             /**< online salvages since fileserver start */
467     afs_uint64 vol_ops;              /**< volume operations since fileserver start */
468 #endif /* AFS_DEMAND_ATTACH_FS */
469
470     afs_uint64 hdr_loads;            /**< header loads from disk */
471     afs_uint64 hdr_gets;             /**< header pulls out of LRU */
472     afs_uint64 attaches;             /**< volume attaches since fileserver start */
473     afs_uint64 soft_detaches;        /**< soft detach ops since fileserver start */
474
475     /* configuration parameters */
476     afs_uint32 hdr_cache_size;       /**< size of volume header cache */
477 } VolPkgStats;
478 extern VolPkgStats VStats;
479
480 /*
481  * volume header cache supporting structures
482  */
483 struct volume_hdr_LRU_stats {
484     afs_uint32 free;
485     afs_uint32 used;
486     afs_uint32 attached;
487 };
488
489 struct volume_hdr_LRU_t {
490     struct rx_queue lru;
491     struct volume_hdr_LRU_stats stats;
492 };
493 extern struct volume_hdr_LRU_t volume_hdr_LRU;
494
495 /*
496  * volume hash chain supporting structures
497  */
498 typedef struct VolumeHashChainHead {
499     struct rx_queue queue;
500     int len;
501     /* someday we could put a per-chain lock here... */
502 #ifdef AFS_DEMAND_ATTACH_FS
503     int busy;
504     int cacheCheck;
505
506     /* per-chain statistics */
507     afs_uint64 looks;
508     afs_uint64 gets;
509     afs_uint64 reorders;
510
511     pthread_cond_t chain_busy_cv;
512 #endif /* AFS_DEMAND_ATTACH_FS */
513 } VolumeHashChainHead;
514
515 typedef struct VolumeHashTable {
516     int Size;
517     int Mask;
518     VolumeHashChainHead * Table;
519 } VolumeHashTable_t;
520 extern VolumeHashTable_t VolumeHashTable;
521
522 struct VolumeHashChainStats {
523     afs_int32 table_size;
524     afs_int32 chain_len;
525 #ifdef AFS_DEMAND_ATTACH_FS
526     afs_int32 chain_cacheCheck;
527     afs_int32 chain_busy;
528     afs_uint64 chain_looks;
529     afs_uint64 chain_gets;
530     afs_uint64 chain_reorders;
531 #endif
532 };
533
534
535 #ifdef AFS_DEMAND_ATTACH_FS
536 /**
537  * DAFS extended per-volume statistics.
538  *
539  * @note this data lives across the entire
540  *       lifetime of the fileserver process
541  */
542 typedef struct VolumeStats {
543     /* counters */
544     afs_uint64 hash_lookups;         /**< hash table lookups */
545     afs_uint64 hash_short_circuits;  /**< short circuited hash lookups (due to cacheCheck) */
546     afs_uint64 hdr_loads;            /**< header loads from disk */
547     afs_uint64 hdr_gets;             /**< header pulls out of LRU */
548     afs_uint16 attaches;             /**< attaches of this volume since fileserver start */
549     afs_uint16 soft_detaches;        /**< soft detaches of this volume */
550     afs_uint16 salvages;             /**< online salvages since fileserver start */
551     afs_uint16 vol_ops;              /**< volume operations since fileserver start */
552
553     /* timestamps */
554     afs_uint32 last_attach;      /**< unix timestamp of last VAttach */
555     afs_uint32 last_get;         /**< unix timestamp of last VGet/VHold */
556     afs_uint32 last_promote;     /**< unix timestamp of last VLRU promote/demote */
557     afs_uint32 last_hdr_get;     /**< unix timestamp of last GetVolumeHeader() */
558     afs_uint32 last_hdr_load;    /**< unix timestamp of last LoadVolumeHeader() */
559     afs_uint32 last_salvage;     /**< unix timestamp of last initiation of an online salvage */
560     afs_uint32 last_salvage_req; /**< unix timestamp of last SALVSYNC request */
561     afs_uint32 last_vol_op;      /**< unix timestamp of last volume operation */
562 } VolumeStats;
563
564 /**
565  * DAFS online salvager state.
566  */
567 typedef struct VolumeOnlineSalvage {
568     afs_uint32 prio;            /**< number of VGetVolume's since salvage requested */
569     int reason;                 /**< reason for requesting online salvage */
570     byte requested;             /**< flag specifying that salvage should be scheduled */
571     byte scheduled;             /**< flag specifying whether online salvage scheduled */
572     byte reserved[2];           /**< padding */
573 } VolumeOnlineSalvage;
574
575 /**
576  * DAFS Volume LRU state.
577  */
578 typedef struct VolumeVLRUState {
579     struct rx_queue lru;        /**< VLRU queue for this generation */
580     VLRUQueueName idx;          /**< VLRU generation index */
581 } VolumeVLRUState;
582 #endif /* AFS_DEMAND_ATTACH_FS */
583
584 typedef struct Volume {
585     struct rx_queue q;          /* Volume hash chain pointers */
586     VolumeId hashid;            /* Volume number -- for hash table lookup */
587     struct volHeader *header;   /* Cached disk data */
588     Device device;              /* Unix device for the volume */
589     struct DiskPartition64
590      *partition;                /* Information about the Unix partition */
591     struct vnodeIndex {
592         IHandle_t *handle;      /* Unix inode holding this index */
593         byte *bitmap;           /* Index bitmap */
594         afs_uint32 bitmapSize;  /* length of bitmap, in bytes */
595         afs_uint32 bitmapOffset;        /* Which byte address of the first long to
596                                          * start search from in bitmap */
597     } vnodeIndex[nVNODECLASSES];
598     IHandle_t *linkHandle;
599     Unique nextVnodeUnique;     /* Derived originally from volume uniquifier.
600                                  * This is the actual next version number to
601                                  * assign; the uniquifier is bumped by 200 and
602                                  * and written to disk every 200 file creates
603                                  * If the volume is shutdown gracefully, the
604                                  * uniquifier should be rewritten with the
605                                  * value nextVnodeVersion */
606     IHandle_t *diskDataHandle;  /* Unix inode holding general volume info */
607     bit16 vnodeHashOffset;      /* Computed by HashOffset function in vnode.h.
608                                  * Assigned to the volume when initialized. 
609                                  * Added to vnode number for hash table index */
610     byte shuttingDown;          /* This volume is going to be detached */
611     byte goingOffline;          /* This volume is going offline */
612     bit32 cacheCheck;           /* Online sequence number to be used to invalidate vnode cache entries
613                                  * that stayed around while a volume was offline */
614     short nUsers;               /* Number of users of this volume header */
615     byte needsPutBack;          /* For a volume utility, this flag is set if we need
616                                  * to give the volume back when we detach it.  The server has
617                                  * certain modes where it doesn't detach the volume, and
618                                  * if we give it back spuriously, the server aborts.  This field
619                                  * is meaningless on the file server */
620     byte specialStatus;         /* An error code to return on VGetVolume: the
621                                  * volume is unavailable for the reason quoted,
622                                  * currently VBUSY or VMOVED */
623     afs_uint32 checkoutMode;    /* for volume utilities, mode number for current checkout */
624     afs_uint32 updateTime;      /* Time that this volume was put on the updated
625                                  * volume list--the list of volumes that will be
626                                  * salvaged should the file server crash */
627     struct rx_queue vnode_list; /**< linked list of cached vnodes for this volume */
628 #ifdef AFS_DEMAND_ATTACH_FS
629     VolState attach_state;      /* what stage of attachment has been completed */
630     afs_uint32 attach_flags;    /* flags related to attachment state */
631     pthread_cond_t attach_cv;   /* state change condition variable */
632     short nWaiters;             /* volume package internal ref count */
633     int chainCacheCheck;        /* Volume hash chain cache check */
634     struct rx_queue vol_list;   /* per-partition volume list (VByPList) */
635
636     VolumeOnlineSalvage salvage;  /* online salvager state */
637     VolumeStats stats;            /* per-volume statistics */
638     VolumeVLRUState vlru;         /* state specific to the VLRU */
639     FSSYNC_VolOp_info * pending_vol_op;  /* fssync command info for any pending vol ops */
640 #endif /* AFS_DEMAND_ATTACH_FS */
641 } Volume;
642
643 struct volHeader {
644     struct rx_queue lru;
645     VolumeDiskData diskstuff;   /* General volume info read from disk */
646     Volume *back;               /* back pointer to current volume structure */
647 };
648
649 /* These macros are used to export fields within the volume header.  This was added
650    to facilitate changing the actual representation */
651
652 #define V_device(vp)            ((vp)->device)
653 #define V_partition(vp)         ((vp)->partition)
654 #define V_diskDataHandle(vp)    ((vp)->diskDataHandle)
655 #define V_vnodeIndex(vp)        ((vp)->vnodeIndex)
656 #define V_nextVnodeUnique(vp)   ((vp)->nextVnodeUnique)
657 #define V_linkHandle(vp)        ((vp)->linkHandle)
658 #define V_checkoutMode(vp)      ((vp)->checkoutMode)
659 #ifdef AFS_DEMAND_ATTACH_FS
660 #define V_attachState(vp)       ((vp)->attach_state)
661 #define V_attachFlags(vp)       ((vp)->attach_flags)
662 #define V_attachCV(vp)          ((vp)->attach_cv)
663 #endif /* AFS_DEMAND_ATTACH_FS */
664
665 /* N.B. V_id must be this, rather than vp->id, or some programs will break, probably */
666 #define V_stamp(vp)             ((vp)->header->diskstuff.stamp)
667 #define V_id(vp)                ((vp)->header->diskstuff.id)
668 #define V_name(vp)              ((vp)->header->diskstuff.name)
669 #define V_inUse(vp)             ((vp)->header->diskstuff.inUse)
670 #define V_inService(vp)         ((vp)->header->diskstuff.inService)
671 #define V_blessed(vp)           ((vp)->header->diskstuff.blessed)
672 #define V_needsSalvaged(vp)     ((vp)->header->diskstuff.needsSalvaged)
673 #define V_uniquifier(vp)        ((vp)->header->diskstuff.uniquifier)
674 #define V_type(vp)              ((vp)->header->diskstuff.type)
675 #define V_parentId(vp)          ((vp)->header->diskstuff.parentId)
676 #define V_cloneId(vp)           ((vp)->header->diskstuff.cloneId)
677 #define V_backupId(vp)          ((vp)->header->diskstuff.backupId)
678 #define V_restoredFromId(vp)    ((vp)->header->diskstuff.restoredFromId)
679 #define V_needsCallback(vp)     ((vp)->header->diskstuff.needsCallback)
680 #define V_destroyMe(vp)         ((vp)->header->diskstuff.destroyMe)
681 #define V_dontSalvage(vp)       ((vp)->header->diskstuff.dontSalvage)
682 #define V_maxquota(vp)          ((vp)->header->diskstuff.maxquota)
683 #define V_minquota(vp)          ((vp)->header->diskstuff.minquota)
684 #define V_maxfiles(vp)          ((vp)->header->diskstuff.maxfiles)
685 #define V_accountNumber(vp)     ((vp)->header->diskstuff.accountNumber)
686 #define V_owner(vp)             ((vp)->header->diskstuff.owner)
687 #define V_filecount(vp)         ((vp)->header->diskstuff.filecount)
688 #define V_diskused(vp)          ((vp)->header->diskstuff.diskused)
689 #define V_dayUse(vp)            ((vp)->header->diskstuff.dayUse)
690 #define V_weekUse(vp)           ((vp)->header->diskstuff.weekUse)
691 #define V_dayUseDate(vp)        ((vp)->header->diskstuff.dayUseDate)
692 #define V_creationDate(vp)      ((vp)->header->diskstuff.creationDate)
693 #define V_accessDate(vp)        ((vp)->header->diskstuff.accessDate)
694 #define V_updateDate(vp)        ((vp)->header->diskstuff.updateDate)
695 #define V_expirationDate(vp)    ((vp)->header->diskstuff.expirationDate)
696 #define V_backupDate(vp)        ((vp)->header->diskstuff.backupDate)
697 #define V_copyDate(vp)          ((vp)->header->diskstuff.copyDate)
698 #define V_offlineMessage(vp)    ((vp)->header->diskstuff.offlineMessage)
699 #define V_disk(vp)              ((vp)->header->diskstuff)
700 #define V_motd(vp)              ((vp)->header->diskstuff.motd)
701 #if OPENAFS_VOL_STATS
702 #define V_stat_initialized(vp)  ((vp)->header->diskstuff.stat_initialized)
703 #define V_stat_area(vp)         (((vp)->header->diskstuff.stat_reads))
704 #define V_stat_reads(vp, idx)   (((vp)->header->diskstuff.stat_reads)[idx])
705 #define V_stat_writes(vp, idx)  (((vp)->header->diskstuff.stat_writes)[idx])
706 #define V_stat_fileSameAuthor(vp, idx) (((vp)->header->diskstuff.stat_fileSameAuthor)[idx])
707 #define V_stat_fileDiffAuthor(vp, idx) (((vp)->header->diskstuff.stat_fileDiffAuthor)[idx])
708 #define V_stat_dirSameAuthor(vp, idx)  (((vp)->header->diskstuff.stat_dirSameAuthor)[idx])
709 #define V_stat_dirDiffAuthor(vp, idx)  (((vp)->header->diskstuff.stat_dirDiffAuthor)[idx])
710 #endif /* OPENAFS_VOL_STATS */
711 #define V_volUpCounter(vp)              ((vp)->header->diskstuff.volUpdateCounter)
712
713 /* File offset computations.  The offset values in the volume header are
714    computed with these macros -- when the file is written only!! */
715 #define VOLUME_MOUNT_TABLE_OFFSET(Volume)       (sizeof (VolumeDiskData))
716 #define VOLUME_BITMAP_OFFSET(Volume)    \
717         (sizeof (VolumeDiskData) + (Volume)->disk.mountTableSize)
718
719
720 extern char *VSalvageMessage;   /* Canonical message when a volume is forced
721                                  * offline */
722 extern Volume *VGetVolume(Error * ec, Error * client_ec, VolId volumeId);
723 extern Volume *VGetVolume_r(Error * ec, VolId volumeId);
724 extern void VPutVolume(Volume *);
725 extern void VPutVolume_r(Volume *);
726 extern void VOffline(Volume * vp, char *message);
727 extern void VOffline_r(Volume * vp, char *message);
728 extern int VConnectFS(void);
729 extern int VConnectFS_r(void);
730 extern void VDisconnectFS(void);
731 extern void VDisconnectFS_r(void);
732 extern int VChildProcReconnectFS(void);
733 extern Volume *VAttachVolume(Error * ec, VolumeId volumeId, int mode);
734 extern Volume *VAttachVolume_r(Error * ec, VolumeId volumeId, int mode);
735 extern Volume *VCreateVolume(Error * ec, char *partname, VolId volumeId,
736                              VolId parentId);
737 extern Volume *VCreateVolume_r(Error * ec, char *partname, VolId volumeId,
738                                VolId parentId);
739 extern VnodeId VAllocBitmapEntry(Error * ec, Volume * vp,
740                                  struct vnodeIndex *index);
741 extern VnodeId VAllocBitmapEntry_r(Error * ec, Volume * vp,
742                                    struct vnodeIndex *index, int flags);
743 extern void VFreeBitMapEntry(Error * ec, register struct vnodeIndex *index,
744                              unsigned bitNumber);
745 extern void VFreeBitMapEntry_r(Error * ec, register struct vnodeIndex *index,
746                                unsigned bitNumber);
747 extern int VolumeNumber(char *name);
748 extern char *VolumeExternalName(VolumeId volumeId);
749 extern Volume *VAttachVolumeByName(Error * ec, char *partition, char *name,
750                                    int mode);
751 extern Volume *VAttachVolumeByName_r(Error * ec, char *partition, char *name,
752                                      int mode);
753 extern void VShutdown(void);
754 extern void VUpdateVolume(Error * ec, Volume * vp);
755 extern void VUpdateVolume_r(Error * ec, Volume * vp, int flags);
756 extern void VAddToVolumeUpdateList(Error * ec, Volume * vp);
757 extern void VAddToVolumeUpdateList_r(Error * ec, Volume * vp);
758 extern void VDetachVolume(Error * ec, Volume * vp);
759 extern void VDetachVolume_r(Error * ec, Volume * vp);
760 extern void VForceOffline(Volume * vp);
761 extern void VForceOffline_r(Volume * vp, int flags);
762 extern void VBumpVolumeUsage(register Volume * vp);
763 extern void VBumpVolumeUsage_r(register Volume * vp);
764 extern void VSetDiskUsage(void);
765 extern void VPrintCacheStats(void);
766 extern void VReleaseVnodeFiles_r(Volume * vp);
767 extern void VCloseVnodeFiles_r(Volume * vp);
768 extern struct DiskPartition64 *VGetPartition(char *name, int abortp);
769 extern struct DiskPartition64 *VGetPartition_r(char *name, int abortp);
770 extern int VInitVolumePackage(ProgramType pt, afs_uint32 nLargeVnodes,
771                               afs_uint32 nSmallVnodes, int connect, afs_uint32 volcache);
772 extern void DiskToVolumeHeader(VolumeHeader_t * h, VolumeDiskHeader_t * dh);
773 extern void VolumeHeaderToDisk(VolumeDiskHeader_t * dh, VolumeHeader_t * h);
774 extern void VTakeOffline_r(register Volume * vp);
775 extern void VTakeOffline(register Volume * vp);
776 extern Volume * VLookupVolume_r(Error * ec, VolId volumeId, Volume * hint);
777 extern void VGetVolumePath(Error * ec, VolId volumeId, char **partitionp,
778                            char **namep);
779 #ifdef AFS_DEMAND_ATTACH_FS
780 extern Volume *VPreAttachVolumeByName(Error * ec, char *partition, char *name);
781 extern Volume *VPreAttachVolumeByName_r(Error * ec, char *partition, char *name);
782 extern Volume *VPreAttachVolumeById_r(Error * ec, char * partition, 
783                                       VolId volumeId);
784 extern Volume *VPreAttachVolumeByVp_r(Error * ec, struct DiskPartition64 * partp, 
785                                       Volume * vp, VolId volume_id);
786 extern Volume *VGetVolumeByVp_r(Error * ec, Volume * vp);
787 extern int VShutdownByPartition_r(struct DiskPartition64 * dp);
788 extern int VShutdownVolume_r(Volume * vp);
789 extern int VConnectSALV(void);
790 extern int VConnectSALV_r(void);
791 extern int VReconnectSALV(void);
792 extern int VReconnectSALV_r(void);
793 extern int VDisconnectSALV(void);
794 extern int VDisconnectSALV_r(void);
795 extern void VPrintExtendedCacheStats(int flags);
796 extern void VPrintExtendedCacheStats_r(int flags);
797 extern void VLRU_SetOptions(int option, afs_uint32 val);
798 extern int VSetVolHashSize(int logsize);
799 extern int VRequestSalvage_r(Error * ec, Volume * vp, int reason, int flags);
800 extern int VRegisterVolOp_r(Volume * vp, FSSYNC_VolOp_info * vopinfo);
801 extern int VDeregisterVolOp_r(Volume * vp);
802 extern void VCancelReservation_r(Volume * vp);
803 #endif /* AFS_DEMAND_ATTACH_FS */
804 extern int VVolOpLeaveOnline_r(Volume * vp, FSSYNC_VolOp_info * vopinfo);
805 extern int VVolOpSetVBusy_r(Volume * vp, FSSYNC_VolOp_info * vopinfo);
806
807
808 /* Naive formula relating number of file size to number of 1K blocks in file */
809 /* Note:  we charge 1 block for 0 length files so the user can't store
810    an inifite number of them; for most files, we give him the inode, vnode,
811    and indirect block overhead, for FREE! */
812 #define nBlocks(bytes) ((afs_sfsize_t)((bytes) == 0? 1: (((afs_sfsize_t)(bytes))+1023)/1024))
813
814 /* Client process id -- file server sends a Check volumes signal back to the client at this pid */
815 #define CLIENTPID       "/vice/vol/clientpid"
816
817 /* Modes of attachment, for VAttachVolume[ByName] to convey to the file server */
818 #define V_READONLY 1            /* Absolutely no updates will be done to the volume */
819 #define V_CLONE    2            /* Cloning the volume:  if it is read/write, then directory
820                                  * version numbers will change.  Header will be updated.  If
821                                  * the volume is read-only, the file server may continue to
822                                  * server it; it may also continue to server it in read/write
823                                  * mode if the writes are deferred */
824 #define V_VOLUPD   3            /* General update or volume purge is possible.  Volume must
825                                  * go offline */
826 #define V_DUMP     4            /* A dump of the volume is requested; the volume can be served
827                                  * read-only during this time */
828 #define V_SECRETLY 5            /* Secret attach of the volume.  This is used to attach a volume
829                                  * which the file server doesn't know about--and which it shouldn't
830                                  * know about yet, since the volume has just been created and
831                                  * is somewhat bogus.  Required to make sure that a file server
832                                  * never knows about more than one copy of the same volume--when
833                                  * a volume is moved from one partition to another on a single
834                                  * server */
835 #define V_PEEK     6            /* "Peek" at the volume without telling the fileserver.  This is
836                                  * similar to V_SECRETLY, but read-only.  It is used in cases where
837                                  * not impacting fileserver performance is more important than
838                                  * getting the most recent data. */
839
840
841
842 /* VUpdateVolume_r flags */
843 #define VOL_UPDATE_WAIT          0x1  /* for demand attach, wait for other exclusive ops to end */
844 #define VOL_UPDATE_NOFORCEOFF    0x2  /* don't force offline on failure. this is to prevent
845                                        * infinite recursion between vupdate and vforceoff */
846
847 /* VForceOffline_r flags */
848 #define VOL_FORCEOFF_NOUPDATE    0x1  /* don't force update on forceoff. this is to prevent
849                                        * infinite recursion between vupdate and vforceoff */
850
851 /* VSyncVolume_r flags */
852 #define VOL_SYNC_WAIT            0x1  /* for demand attach, wait for other exclusive ops to end */
853
854 /* VAllocBitmapEntry_r flags */
855 #define VOL_ALLOC_BITMAP_WAIT    0x1  /* for demand attach, wait for other exclusive ops to end */
856
857 /* VRequestSalvage_r flags */
858 #define VOL_SALVAGE_INVALIDATE_HEADER 0x1 /* for demand attach fs, invalidate volume header cache */
859
860
861 #if     defined(NEARINODE_HINT)
862 #define V_pref(vp,nearInode)  nearInodeHash(V_id(vp),(nearInode)); (nearInode) %= V_partition(vp)->f_files
863 #else
864 #define V_pref(vp,nearInode)   nearInode = 0
865 #endif /* NEARINODE_HINT */
866
867 #endif /* __volume_h */