down with assert, up with osi_Assert
[openafs.git] / src / vol / partition.c
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) 2003 Apple Computer, Inc.
10  * Portions Copyright (c) 2006 Sine Nomine Associates
11  */
12
13 /*
14
15         System:         VICE-TWO
16         Module:         partition.c
17         Institution:    The Information Technology Center, Carnegie-Mellon University
18
19  */
20
21 #include <afsconfig.h>
22 #include <afs/param.h>
23
24
25 #include <ctype.h>
26 #include <string.h>
27 #ifdef AFS_NT40_ENV
28 #include <windows.h>
29 #include <winbase.h>
30 #include <winioctl.h>
31 #else
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35
36 #if AFS_HAVE_STATVFS || AFS_HAVE_STATVFS64
37 #include <sys/statvfs.h>
38 #endif /* AFS_HAVE_STATVFS */
39 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
40 #include <sys/mount.h>
41 #endif
42
43 #if !defined(AFS_SGI_ENV)
44 #ifdef  AFS_OSF_ENV
45 #include <sys/mount.h>
46 #include <ufs/fs.h>
47 #else /* AFS_OSF_ENV */
48 #ifdef AFS_VFSINCL_ENV
49 #define VFS
50 #ifdef  AFS_SUN5_ENV
51 #include <sys/fs/ufs_fs.h>
52 #else
53 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
54 #include <ufs/ufs/dinode.h>
55 #include <ufs/ffs/fs.h>
56 #else
57 #include <ufs/fs.h>
58 #endif
59 #endif
60 #else /* AFS_VFSINCL_ENV */
61 #if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX22_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
62 #include <sys/fs.h>
63 #endif
64 #endif /* AFS_VFSINCL_ENV */
65 #endif /* AFS_OSF_ENV */
66 #include <errno.h>
67 #include <sys/stat.h>
68 #include <stdio.h>
69 #include <sys/file.h>
70 #ifdef  AFS_AIX_ENV
71 #include <sys/vfs.h>
72 #include <sys/lockf.h>
73 #else
74 #ifdef  AFS_HPUX_ENV
75 #include <sys/vfs.h>
76 #include <unistd.h>
77 #include <fcntl.h>
78 #include <checklist.h>
79 #else
80 #if     defined(AFS_SUN_ENV)
81 #include <sys/vfs.h>
82 #ifndef AFS_SUN5_ENV
83 #include <mntent.h>
84 #endif
85 #endif
86 #ifdef AFS_SUN5_ENV
87 #include <unistd.h>
88 #include <sys/mnttab.h>
89 #include <sys/mntent.h>
90 #else
91 #ifdef AFS_LINUX22_ENV
92 #include <mntent.h>
93 #include <sys/statfs.h>
94 #else
95 #include <fstab.h>
96 #endif
97 #endif
98 #endif
99 #endif
100 #endif /* AFS_SGI_ENV */
101 #endif /* AFS_NT40_ENV */
102 #if defined(AFS_SGI_ENV)
103 #include <sys/errno.h>
104 #include <sys/stat.h>
105 #include <stdio.h>
106 #include <sys/file.h>
107 #include <mntent.h>
108 #endif
109
110 #include <rx/xdr.h>
111 #include <afs/afsint.h>
112 #include "nfs.h"
113 #include <afs/errors.h>
114 #include "lock.h"
115 #include "lwp.h"
116 #include <afs/afssyscalls.h>
117 #include "ihandle.h"
118 #include "common.h"
119 #ifdef AFS_NAMEI_ENV
120 #ifdef AFS_NT40_ENV
121 #include "ntops.h"
122 #else
123 #include "namei_ops.h"
124 #include <dirent.h>
125 #endif /* AFS_NT40_ENV */
126 #endif /* AFS_NAMEI_ENV */
127 #include "vnode.h"
128 #include "volume.h"
129 #include "partition.h"
130 #include <afs/afs_assert.h>
131
132 #if defined(AFS_HPUX_ENV)
133 #include <sys/types.h>
134 #include <sys/privgrp.h>
135 #endif /* defined(AFS_HPUX_ENV) */
136
137 #ifdef AFS_AIX42_ENV
138 #include <jfs/filsys.h>
139 #endif
140
141 #ifdef O_LARGEFILE
142
143 #define afs_stat        stat64
144 #define afs_open        open64
145 #define afs_fopen       fopen64
146 #ifndef AFS_NT40_ENV
147 #if AFS_HAVE_STATVFS64
148 # define afs_statvfs    statvfs64
149 #else
150 # if AFS_HAVE_STATFS64
151 #  define afs_statfs    statfs64
152 #else
153 #  if AFS_HAVE_STATVFS
154 #   define afs_statvfs  statvfs
155 #  else
156 #   define afs_statfs   statfs
157 #  endif /* !AFS_HAVE_STATVFS */
158 # endif /* !AFS_HAVE_STATFS64 */
159 #endif /* !AFS_HAVE_STATVFS64 */
160 #endif /* !AFS_NT40_ENV */
161
162 #else /* !O_LARGEFILE */
163
164 #define afs_stat        stat
165 #define afs_open        open
166 #define afs_fopen       fopen
167 #ifndef AFS_NT40_ENV
168 #if AFS_HAVE_STATVFS
169 #define afs_statvfs     statvfs
170 #else /* !AFS_HAVE_STATVFS */
171 #define afs_statfs      statfs
172 #endif /* !AFS_HAVE_STATVFS */
173 #endif /* !AFS_NT40_ENV */
174
175 #endif /* !O_LARGEFILE */
176
177 int aixlow_water = 8;           /* default 8% */
178 struct DiskPartition64 *DiskPartitionList;
179
180 #ifdef AFS_DEMAND_ATTACH_FS
181 /* file to lock to conceptually "lock" the vol headers on a partition */
182 #define AFS_PARTLOCK_FILE ".volheaders.lock"
183 #define AFS_VOLUMELOCK_FILE ".volume.lock"
184
185 static struct DiskPartition64 *DiskPartitionTable[VOLMAXPARTS+1];
186
187 static struct DiskPartition64 * VLookupPartition_r(char * path);
188 static void AddPartitionToTable_r(struct DiskPartition64 *);
189 #endif /* AFS_DEMAND_ATTACH_FS */
190
191 #ifdef AFS_SGI_XFS_IOPS_ENV
192 /* Verify that the on disk XFS inodes on the partition are large enough to
193  * hold the AFS attribute. Returns -1 if the attribute can't be set or is
194  * too small to fit in the inode. Returns 0 if the attribute does fit in
195  * the XFS inode.
196  */
197 #include <afs/xfsattrs.h>
198 static int
199 VerifyXFSInodeSize(char *part, char *fstype)
200 {
201     afs_xfs_attr_t junk;
202     int length = SIZEOF_XFS_ATTR_T;
203     int fd = 0;
204     int code = -1;
205     struct fsxattr fsx;
206
207     if (strcmp("xfs", fstype))
208         return 0;
209
210     if (attr_set(part, AFS_XFS_ATTR, &junk, length, ATTR_ROOT) == 0) {
211         if (((fd = open(part, O_RDONLY, 0)) != -1)
212             && (fcntl(fd, F_FSGETXATTRA, &fsx) == 0)) {
213
214             if (fsx.fsx_nextents) {
215                 Log("Partition %s: XFS inodes too small, exiting.\n", part);
216                 Log("Run xfs_size_check utility and remake partitions.\n");
217             } else
218                 code = 0;
219         }
220
221         if (fd > 0)
222             close(fd);
223         (void)attr_remove(part, AFS_XFS_ATTR, ATTR_ROOT);
224     }
225     return code;
226 }
227 #endif /* AFS_SGI_XFS_IOPS_ENV */
228
229 int
230 VInitPartitionPackage(void)
231 {
232 #ifdef AFS_DEMAND_ATTACH_ENV
233     memset(&DiskPartitionTable, 0, sizeof(DiskPartitionTable));
234 #endif /* AFS_DEMAND_ATTACH_ENV */
235     return 0;
236 }
237
238 static void
239 VInitPartition_r(char *path, char *devname, Device dev)
240 {
241     struct DiskPartition64 *dp, *op;
242
243     dp = (struct DiskPartition64 *)malloc(sizeof(struct DiskPartition64));
244     /* Add it to the end, to preserve order when we print statistics */
245     for (op = DiskPartitionList; op; op = op->next) {
246         if (!op->next)
247             break;
248     }
249     if (op)
250         op->next = dp;
251     else
252         DiskPartitionList = dp;
253     dp->next = 0;
254     dp->name = (char *)malloc(strlen(path) + 1);
255     strncpy(dp->name, path, strlen(path) + 1);
256     dp->index = volutil_GetPartitionID(path);
257 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
258     /* Create a lockfile for the partition, of the form /vicepa/Lock/vicepa */
259     dp->devName = (char *)malloc(2 * strlen(path) + 6);
260     strcpy(dp->devName, path);
261     strcat(dp->devName, "/");
262     strcat(dp->devName, "Lock");
263     mkdir(dp->devName, 0700);
264     strcat(dp->devName, path);
265     close(afs_open(dp->devName, O_RDWR | O_CREAT, 0600));
266     dp->device = dp->index;
267 #else
268     dp->devName = (char *)malloc(strlen(devname) + 1);
269     strncpy(dp->devName, devname, strlen(devname) + 1);
270     dp->device = dev;
271 #endif
272     dp->lock_fd = INVALID_FD;
273     dp->flags = 0;
274     dp->f_files = 1;            /* just a default value */
275 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
276     if (programType == fileServer)
277         (void)namei_ViceREADME(VPartitionPath(dp));
278 #endif
279     VSetPartitionDiskUsage_r(dp);
280 #ifdef AFS_DEMAND_ATTACH_FS
281     AddPartitionToTable_r(dp);
282     queue_Init(&dp->vol_list.head);
283     CV_INIT(&dp->vol_list.cv, "vol list", CV_DEFAULT, 0);
284     dp->vol_list.len = 0;
285     dp->vol_list.busy = 0;
286     {
287         char lockpath[MAXPATHLEN+1];
288         afs_snprintf(lockpath, MAXPATHLEN, "%s/" AFS_PARTLOCK_FILE, dp->name);
289         lockpath[MAXPATHLEN] = '\0';
290         VLockFileInit(&dp->headerLockFile, lockpath);
291
292         afs_snprintf(lockpath, MAXPATHLEN, "%s/" AFS_VOLUMELOCK_FILE, dp->name);
293         lockpath[MAXPATHLEN] = '\0';
294         VLockFileInit(&dp->volLockFile, lockpath);
295     }
296     VDiskLockInit(&dp->headerLock, &dp->headerLockFile, 1);
297 #endif /* AFS_DEMAND_ATTACH_FS */
298 }
299
300 static void
301 VInitPartition(char *path, char *devname, Device dev)
302 {
303     VOL_LOCK;
304     VInitPartition_r(path, devname, dev);
305     VOL_UNLOCK;
306 }
307
308 #ifndef AFS_NT40_ENV
309 /* VAttachPartitions() finds the vice partitions on this server. Calls
310  * VCheckPartition() to do some basic checks on the partition. If the partition
311  * is a valid vice partition, VCheckPartition will add it to the DiskPartition
312  * list.
313  * Returns the number of errors returned by VCheckPartition. An error in
314  * VCheckPartition means that partition is a valid vice partition but the
315  * fileserver should not start because of the error found on that partition.
316  *
317  * AFS_NAMEI_ENV
318  * No specific user space file system checks, since we don't know what
319  * is being used for vice partitions.
320  *
321  * Use partition name as devname.
322  */
323 int
324 VCheckPartition(char *part, char *devname)
325 {
326     struct afs_stat status;
327 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
328     char AFSIDatPath[MAXPATHLEN];
329 #endif
330
331     /* Only keep track of "/vicepx" partitions since it can get hairy
332      * when NFS mounts are involved.. */
333     if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE)) {
334         return 0;
335     }
336     if (afs_stat(part, &status) < 0) {
337         Log("VInitVnodes: Couldn't find file system %s; ignored\n", part);
338         return 0;
339     }
340 #ifndef AFS_AIX32_ENV
341     if (programType == fileServer) {
342         char salvpath[MAXPATHLEN];
343         strcpy(salvpath, part);
344         strcat(salvpath, "/FORCESALVAGE");
345         if (afs_stat(salvpath, &status) == 0) {
346             Log("VInitVnodes: Found %s; aborting\n", salvpath);
347             return -1;
348         }
349     }
350 #endif
351
352 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
353     strcpy(AFSIDatPath, part);
354     strcat(AFSIDatPath, "/AFSIDat");
355 #ifdef AFS_NAMEI_ENV
356     if (afs_stat(AFSIDatPath, &status) < 0) {
357         DIR *dirp;
358         struct dirent *dp;
359
360         dirp = opendir(part);
361         osi_Assert(dirp);
362         while ((dp = readdir(dirp))) {
363             if (dp->d_name[0] == 'V') {
364                 Log("This program is compiled with AFS_NAMEI_ENV, but partition %s seems to contain volumes which don't use the namei-interface; aborting\n", part);
365                 closedir(dirp);
366                 return -1;
367             }
368         }
369         closedir(dirp);
370     }
371 #else /* AFS_NAMEI_ENV */
372     if (afs_stat(AFSIDatPath, &status) == 0) {
373         Log("This program is compiled without AFS_NAMEI_ENV, but partition %s seems to contain volumes which use the namei-interface; aborting\n", part);
374         return -1;
375     }
376
377 #ifdef AFS_SGI_XFS_IOPS_ENV
378     if (VerifyXFSInodeSize(part, status.st_fstype) < 0)
379         return -1;
380 #endif
381 #endif /* AFS_NAMEI_ENV */
382 #endif /* !AFS_LINUX20_ENV && !AFS_NT40_ENV */
383
384 #if defined(AFS_DUX40_ENV) && !defined(AFS_NAMEI_ENV)
385     if (status.st_ino != ROOTINO) {
386         Log("%s is not a mounted file system; ignored.\n", part);
387         return 0;
388     }
389 #endif
390
391     VInitPartition(part, devname, status.st_dev);
392
393     return 0;
394 }
395
396 /* VIsAlwaysAttach() checks whether a /vicepX directory should always be
397  * attached (return value 1), or only attached when it is a separately
398  * mounted partition (return value 0).  For non-NAMEI environments, it
399  * always returns 0.
400  *
401  * *awouldattach will be set to 1 if the given path at least looks like a vice
402  * partition (that is, if we return 0, the only thing preventing this partition
403  * from being attached is the existence of the AlwaysAttach file), or to 0
404  * otherwise. *awouldattach is set regardless of whether or not the partition
405  * should always be attached or not.
406  */
407 static int
408 VIsAlwaysAttach(char *part, int *awouldattach)
409 {
410 #ifdef AFS_NAMEI_ENV
411     struct afs_stat st;
412     char checkfile[256];
413     int ret;
414 #endif /* AFS_NAMEI_ENV */
415
416     if (awouldattach) {
417         *awouldattach = 0;
418     }
419
420 #ifdef AFS_NAMEI_ENV
421     if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
422         return 0;
423
424     if (awouldattach) {
425         *awouldattach = 1;
426     }
427
428     strncpy(checkfile, part, 100);
429     strcat(checkfile, "/");
430     strcat(checkfile, VICE_ALWAYSATTACH_FILE);
431
432     ret = afs_stat(checkfile, &st);
433     return (ret < 0) ? 0 : 1;
434 #else /* AFS_NAMEI_ENV */
435     return 0;
436 #endif /* AFS_NAMEI_ENV */
437 }
438
439 /* VAttachPartitions2() looks for and attaches /vicepX partitions
440  * where a special file (VICE_ALWAYSATTACH_FILE) exists.  This is
441  * used to attach /vicepX directories which aren't on dedicated
442  * partitions, in the NAMEI fileserver.
443  */
444 void
445 VAttachPartitions2(void)
446 {
447 #ifdef AFS_NAMEI_ENV
448     DIR *dirp;
449     struct dirent *de;
450     char pname[32];
451     int wouldattach;
452
453     dirp = opendir("/");
454     while ((de = readdir(dirp))) {
455         strcpy(pname, "/");
456         strncat(pname, de->d_name, 20);
457         pname[sizeof(pname) - 1] = '\0';
458
459         /* Only keep track of "/vicepx" partitions since automounter
460          * may hose us */
461         if (VIsAlwaysAttach(pname, &wouldattach)) {
462             VCheckPartition(pname, "");
463         } else {
464             struct afs_stat st;
465             if (wouldattach && VGetPartition(pname, 0) == NULL &&
466                 afs_stat(pname, &st) == 0 && S_ISDIR(st.st_mode)) {
467
468                 /* This is a /vicep* dir, and it has not been attached as a
469                  * partition. This probably means that this is a /vicep* dir
470                  * that is not a separate partition, so just give a notice so
471                  * admins are not confused as to why their /vicep* dirs are not
472                  * being attached.
473                  *
474                  * It is possible that the dir _is_ a separate partition and we
475                  * failed to attach it earlier, making this message a bit
476                  * confusing. But that should be rare, and an error message
477                  * about the failure will already be logged right before this,
478                  * so it should be clear enough. */
479
480                 Log("VAttachPartitions: not attaching %s; either it is not a "
481                     "separate partition, or it failed to attach (create the "
482                     "file %s/" VICE_ALWAYSATTACH_FILE " to force attachment)\n",
483                     pname, pname);
484             }
485         }
486     }
487     closedir(dirp);
488 #endif /* AFS_NAMEI_ENV */
489 }
490 #endif /* AFS_NT40_ENV */
491
492 #ifdef AFS_SUN5_ENV
493 int
494 VAttachPartitions(void)
495 {
496     int errors = 0;
497     struct mnttab mnt;
498     FILE *mntfile;
499
500     if (!(mntfile = afs_fopen(MNTTAB, "r"))) {
501         Log("Can't open %s\n", MNTTAB);
502         perror(MNTTAB);
503         exit(-1);
504     }
505     while (!getmntent(mntfile, &mnt)) {
506         /* Ignore non ufs or non read/write partitions */
507         /* but allow zfs too if we're in the NAMEI environment */
508         if (
509 #ifdef AFS_NAMEI_ENV
510             (((strcmp(mnt.mnt_fstype, "ufs") &&
511                 strcmp(mnt.mnt_fstype, "zfs"))))
512 #else
513             (strcmp(mnt.mnt_fstype, "ufs") != 0)
514 #endif
515             || (strncmp(mnt.mnt_mntopts, "ro,ignore", 9) == 0))
516             continue;
517
518         /* If we're going to always attach this partition, do it later. */
519         if (VIsAlwaysAttach(mnt.mnt_mountp, NULL))
520             continue;
521
522 #ifndef AFS_NAMEI_ENV
523         if (hasmntopt(&mnt, "logging") != NULL) {
524             Log("This program is compiled without AFS_NAMEI_ENV, and "
525                 "partition %s is mounted with the 'logging' option. "
526                 "Using the inode fileserver backend with 'logging' UFS "
527                 "partitions causes volume corruption, so please either "
528                 "mount the partition without logging, or use the namei "
529                 "fileserver backend. Aborting...\n", mnt.mnt_mountp);
530             errors++;
531         }
532 #endif /* !AFS_NAMEI_ENV */
533
534         if (VCheckPartition(mnt.mnt_mountp, mnt.mnt_special) < 0)
535             errors++;
536     }
537
538     (void)fclose(mntfile);
539
540     /* Process the always-attach partitions, if any. */
541     VAttachPartitions2();
542
543     return errors;
544 }
545
546 #endif /* AFS_SUN5_ENV */
547 #if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)) || defined(AFS_HPUX_ENV)
548 int
549 VAttachPartitions(void)
550 {
551     int errors = 0;
552     FILE *mfd;
553     struct mntent *mntent;
554
555     if ((mfd = setmntent(MOUNTED, "r")) == NULL) {
556         Log("Problems in getting mount entries(setmntent)\n");
557         exit(-1);
558     }
559     while (mntent = getmntent(mfd)) {
560         if (!hasmntopt(mntent, MNTOPT_RW))
561             continue;
562
563         /* If we're going to always attach this partition, do it later. */
564         if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
565             continue;
566
567         if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0)
568             errors++;
569     }
570
571     endmntent(mfd);
572
573     /* Process the always-attach partitions, if any. */
574     VAttachPartitions2();
575
576     return errors;
577 }
578 #endif
579 #ifdef AFS_AIX_ENV
580 /*
581  * (This function was grabbed from df.c)
582  */
583 int
584 getmount(struct vmount **vmountpp)
585 {
586     int size;
587     struct vmount *vm;
588     int nmounts;
589
590     /* set initial size of mntctl buffer to a MAGIC NUMBER */
591     size = BUFSIZ;
592
593     /* try the operation until ok or a fatal error */
594     while (1) {
595         if ((vm = (struct vmount *)malloc(size)) == NULL) {
596             /* failed getting memory for mount status buf */
597             perror("FATAL ERROR: get_stat malloc failed\n");
598             exit(-1);
599         }
600
601         /*
602          * perform the QUERY mntctl - if it returns > 0, that is the
603          * number of vmount structures in the buffer.  If it returns
604          * -1, an error occured.  If it returned 0, then look in
605          * first word of buffer for needed size.
606          */
607         if ((nmounts = mntctl(MCTL_QUERY, size, (caddr_t) vm)) > 0) {
608             /* OK, got it, now return */
609             *vmountpp = vm;
610             return (nmounts);
611
612         } else if (nmounts == 0) {
613             /* the buffer wasn't big enough .... */
614             /* .... get required buffer size */
615             size = *(int *)vm;
616             free(vm);
617
618         } else {
619             /* some other kind of error occurred */
620             free(vm);
621             return (-1);
622         }
623     }
624 }
625
626 int
627 VAttachPartitions(void)
628 {
629     int errors = 0;
630     int nmounts;
631     struct vmount *vmountp;
632
633     if ((nmounts = getmount(&vmountp)) <= 0) {
634         Log("Problems in getting # of mount entries(getmount)\n");
635         exit(-1);
636     }
637     for (; nmounts;
638          nmounts--, vmountp =
639          (struct vmount *)((int)vmountp + vmountp->vmt_length)) {
640         char *part = vmt2dataptr(vmountp, VMT_STUB);
641
642         if (vmountp->vmt_flags & (MNT_READONLY | MNT_REMOVABLE | MNT_REMOTE))
643             continue;           /* Ignore any "special" partitions */
644
645 #ifdef AFS_AIX42_ENV
646 #ifndef AFS_NAMEI_ENV
647         {
648             struct superblock fs;
649             /* The Log statements are non-sequiters in the SalvageLog and don't
650              * even appear in the VolserLog, so restrict them to the FileLog.
651              */
652             if (ReadSuper(&fs, vmt2dataptr(vmountp, VMT_OBJECT)) < 0) {
653                 if (programType == fileServer)
654                     Log("Can't read superblock for %s, ignoring it.\n", part);
655                 continue;
656             }
657             if (IsBigFilesFileSystem(&fs)) {
658                 if (programType == fileServer)
659                     Log("%s is a big files filesystem, ignoring it.\n", part);
660                 continue;
661             }
662         }
663 #endif
664 #endif
665
666         /* If we're going to always attach this partition, do it later. */
667         if (VIsAlwaysAttach(part, NULL))
668             continue;
669
670         if (VCheckPartition(part, vmt2dataptr(vmountp, VMT_OBJECT)) < 0)
671             errors++;
672     }
673
674     /* Process the always-attach partitions, if any. */
675     VAttachPartitions2();
676
677     return errors;
678 }
679 #endif
680 #if defined(AFS_DUX40_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
681 int
682 VAttachPartitions(void)
683 {
684     int errors = 0;
685     struct fstab *fsent;
686
687     if (setfsent() < 0) {
688         Log("Error listing filesystems.\n");
689         exit(-1);
690     }
691
692     while ((fsent = getfsent())) {
693         if (strcmp(fsent->fs_type, "rw") != 0)
694             continue;
695
696         /* If we're going to always attach this partition, do it later. */
697         if (VIsAlwaysAttach(fsent->fs_file, NULL))
698             continue;
699
700         if (VCheckPartition(fsent->fs_file, fsent->fs_spec) < 0)
701             errors++;
702     }
703     endfsent();
704
705     /* Process the always-attach partitions, if any. */
706     VAttachPartitions2();
707
708     return errors;
709 }
710 #endif
711
712 #ifdef AFS_NT40_ENV
713 #include <string.h>
714 #include <sys/stat.h>
715 /* VValidVPTEntry
716  *
717  * validate names in vptab.
718  *
719  * Return value:
720  * 1 valid entry
721  * 0 invalid entry
722  */
723
724 int
725 VValidVPTEntry(struct vptab *vpe)
726 {
727     int len = strlen(vpe->vp_name);
728     int i;
729
730     if (len < VICE_PREFIX_SIZE + 1 || len > VICE_PREFIX_SIZE + 2)
731         return 0;
732     if (strncmp(vpe->vp_name, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
733         return 0;
734
735     for (i = VICE_PREFIX_SIZE; i < len; i++) {
736         if (vpe->vp_name[i] < 'a' || vpe->vp_name[i] > 'z') {
737             Log("Invalid partition name %s in registry, ignoring it.\n",
738                 vpe->vp_name);
739             return 0;
740         }
741     }
742     if (len == VICE_PREFIX_SIZE + 2) {
743         i = (int)(vpe->vp_name[VICE_PREFIX_SIZE] - 'a') * 26 +
744             (int)(vpe->vp_name[VICE_PREFIX_SIZE + 1] - 'a');
745         if (i > 255) {
746             Log("Invalid partition name %s in registry, ignoring it.\n",
747                 vpe->vp_name);
748             return 0;
749         }
750     }
751
752     len = strlen(vpe->vp_dev);
753     if (len != 2 || vpe->vp_dev[1] != ':' || vpe->vp_dev[0] < 'A'
754         || vpe->vp_dev[0] > 'Z') {
755         Log("Invalid device name %s in registry, ignoring it.\n",
756             vpe->vp_dev);
757         return 0;
758     }
759
760     return 1;
761 }
762
763 int
764 VCheckPartition(char *partName)
765 {
766     char volRoot[4];
767     char volFsType[64];
768     DWORD dwDummy;
769     int err;
770
771     /* partName is presumed to be of the form "X:" */
772     (void)sprintf(volRoot, "%c:\\", *partName);
773
774     if (!GetVolumeInformation(volRoot,  /* volume root directory */
775                               NULL,     /* volume name buffer */
776                               0,        /* volume name size */
777                               NULL,     /* volume serial number */
778                               &dwDummy, /* max component length */
779                               &dwDummy, /* file system flags */
780                               volFsType,        /* file system name */
781                               sizeof(volFsType))) {
782         err = GetLastError();
783         Log("VCheckPartition: Failed to get partition information for %s, ignoring it.\n", partName);
784         return -1;
785     }
786
787     if (strcmp(volFsType, "NTFS")) {
788         Log("VCheckPartition: Partition %s is not an NTFS partition, ignoring it.\n", partName);
789         return -1;
790     }
791
792     return 0;
793 }
794
795
796 int
797 VAttachPartitions(void)
798 {
799     struct DiskPartition64 *partP, *prevP, *nextP;
800     struct vpt_iter iter;
801     struct vptab entry;
802
803     if (vpt_Start(&iter) < 0) {
804         Log("No partitions to attach.\n");
805         return 0;
806     }
807
808     while (0 == vpt_NextEntry(&iter, &entry)) {
809         if (!VValidVPTEntry(&entry)) {
810             continue;
811         }
812
813         /* This test for duplicates relies on the fact that the method
814          * of storing the partition names in the NT registry means the same
815          * partition name will never appear twice in the list.
816          */
817         for (partP = DiskPartitionList; partP; partP = partP->next) {
818             if (*partP->devName == *entry.vp_dev) {
819                 Log("Same drive (%s) used for both partition %s and partition %s, ignoring both.\n", entry.vp_dev, partP->name, entry.vp_name);
820                 partP->flags = PART_DUPLICATE;
821                 break;          /* Only one entry will ever be in this list. */
822             }
823         }
824         if (partP)
825             continue;           /* found a duplicate */
826
827         if (VCheckPartition(entry.vp_dev) < 0)
828             continue;
829         /* This test allows for manually inserting the FORCESALVAGE flag
830          * and thereby invoking the salvager. scandisk obviously won't be
831          * doing this for us.
832          */
833         if (programType == fileServer) {
834             struct afs_stat status;
835             char salvpath[MAXPATHLEN];
836             strcpy(salvpath, entry.vp_dev);
837             strcat(salvpath, "\\FORCESALVAGE");
838             if (afs_stat(salvpath, &status) == 0) {
839                 Log("VAttachPartitions: Found %s; aborting\n", salvpath);
840                 exit(1);
841             }
842         }
843         VInitPartition(entry.vp_name, entry.vp_dev, *entry.vp_dev - 'A');
844     }
845     vpt_Finish(&iter);
846
847     /* Run through partition list and clear out the dupes. */
848     prevP = nextP = NULL;
849     for (partP = DiskPartitionList; partP; partP = nextP) {
850         nextP = partP->next;
851         if (partP->flags == PART_DUPLICATE) {
852             if (prevP)
853                 prevP->next = partP->next;
854             else
855                 DiskPartitionList = partP->next;
856             free(partP);
857         } else
858             prevP = partP;
859     }
860
861     return 0;
862 }
863 #endif
864
865 #ifdef AFS_LINUX22_ENV
866 int
867 VAttachPartitions(void)
868 {
869     int errors = 0;
870     FILE *mfd;
871     struct mntent *mntent;
872
873     if ((mfd = setmntent("/proc/mounts", "r")) == NULL) {
874         if ((mfd = setmntent("/etc/mtab", "r")) == NULL) {
875             Log("Problems in getting mount entries(setmntent)\n");
876             exit(-1);
877         }
878     }
879     while ((mntent = getmntent(mfd))) {
880         /* If we're going to always attach this partition, do it later. */
881         if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
882             continue;
883
884         if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0)
885             errors++;
886     }
887     endmntent(mfd);
888
889     /* Process the always-attach partitions, if any. */
890     VAttachPartitions2();
891
892     return errors;
893 }
894 #endif /* AFS_LINUX22_ENV */
895
896 /* This routine is to be called whenever the actual name of the partition
897  * is required. The canonical name is still in part->name.
898  */
899 char *
900 VPartitionPath(struct DiskPartition64 *part)
901 {
902 #ifdef AFS_NT40_ENV
903     return part->devName;
904 #else
905     return part->name;
906 #endif
907 }
908
909 /* get partition structure, abortp tells us if we should abort on failure */
910 struct DiskPartition64 *
911 VGetPartition_r(char *name, int abortp)
912 {
913     struct DiskPartition64 *dp;
914 #ifdef AFS_DEMAND_ATTACH_FS
915     dp = VLookupPartition_r(name);
916 #else /* AFS_DEMAND_ATTACH_FS */
917     for (dp = DiskPartitionList; dp; dp = dp->next) {
918         if (strcmp(dp->name, name) == 0)
919             break;
920     }
921 #endif /* AFS_DEMAND_ATTACH_FS */
922     if (abortp)
923         osi_Assert(dp != NULL);
924     return dp;
925 }
926
927 struct DiskPartition64 *
928 VGetPartition(char *name, int abortp)
929 {
930     struct DiskPartition64 *retVal;
931     VOL_LOCK;
932     retVal = VGetPartition_r(name, abortp);
933     VOL_UNLOCK;
934     return retVal;
935 }
936
937 #ifdef AFS_NT40_ENV
938 void
939 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
940 {
941     ULARGE_INTEGER free_user, total, free_total;
942     int ufree, tot, tfree;
943
944     if (!GetDiskFreeSpaceEx
945         (VPartitionPath(dp), &free_user, &total, &free_total)) {
946         printf("Failed to get disk space info for %s, error = %d\n", dp->name,
947                GetLastError());
948         return;
949     }
950
951     /* Convert to 1K units. */
952     ufree = (int)Int64ShraMod32(free_user.QuadPart, 10);
953     tot = (int)Int64ShraMod32(total.QuadPart, 10);
954     tfree = (int)Int64ShraMod32(free_total.QuadPart, 10);
955
956     dp->minFree = tfree - ufree;        /* only used in VPrintDiskStats_r */
957     dp->totalUsable = tot;
958     dp->free = tfree;
959 }
960
961 #else
962 void
963 VSetPartitionDiskUsage_r(struct DiskPartition64 *dp)
964 {
965     int bsize, code;
966     afs_int64 totalblks, free, used, availblks;
967     int reserved;
968 #ifdef afs_statvfs
969     struct afs_statvfs statbuf;
970 #else
971     struct afs_statfs statbuf;
972 #endif
973
974     if (dp->flags & PART_DONTUPDATE)
975         return;
976     /* Note:  we don't bother syncing because it's only an estimate, update
977      * is syncing every 30 seconds anyway, we only have to keep the disk
978      * approximately 10% from full--you just can't get the stuff in from
979      * the net fast enough to worry */
980 #ifdef afs_statvfs
981     code = afs_statvfs(dp->name, &statbuf);
982 #else
983     code = afs_statfs(dp->name, &statbuf);
984 #endif
985     if (code < 0) {
986         Log("statfs of %s failed in VSetPartitionDiskUsage (errno = %d)\n",
987             dp->name, errno);
988         return;
989     }
990     if (statbuf.f_blocks == -1) {       /* Undefined; skip stats.. */
991         Log("statfs of %s failed in VSetPartitionDiskUsage\n", dp->name);
992         return;
993     }
994     totalblks = statbuf.f_blocks;
995     free = statbuf.f_bfree;
996     reserved = free - statbuf.f_bavail;
997 #ifdef afs_statvfs
998     bsize = statbuf.f_frsize;
999 #else
1000     bsize = statbuf.f_bsize;
1001 #endif
1002     availblks = totalblks - reserved;
1003     dp->f_files = statbuf.f_files;      /* max # of files in partition */
1004
1005     /* Now free and totalblks are in fragment units, but we want them in
1006      * 1K units.
1007      */
1008     if (bsize >= 1024) {
1009         free *= (bsize / 1024);
1010         totalblks *= (bsize / 1024);
1011         availblks *= (bsize / 1024);
1012         reserved *= (bsize / 1024);
1013     } else {
1014         free /= (1024 / bsize);
1015         totalblks /= (1024 / bsize);
1016         availblks /= (1024 / bsize);
1017         reserved /= (1024 / bsize);
1018     }
1019     /* now compute remaining figures */
1020     used = totalblks - free;
1021
1022     dp->minFree = reserved;     /* only used in VPrintDiskStats_r */
1023     dp->totalUsable = availblks;
1024     dp->free = availblks - used;        /* this is exactly f_bavail */
1025 }
1026 #endif /* AFS_NT40_ENV */
1027
1028 void
1029 VSetPartitionDiskUsage(struct DiskPartition64 *dp)
1030 {
1031     VOL_LOCK;
1032     VSetPartitionDiskUsage_r(dp);
1033     VOL_UNLOCK;
1034 }
1035
1036 void
1037 VResetDiskUsage_r(void)
1038 {
1039     struct DiskPartition64 *dp;
1040     for (dp = DiskPartitionList; dp; dp = dp->next) {
1041         VSetPartitionDiskUsage_r(dp);
1042 #ifndef AFS_PTHREAD_ENV
1043         IOMGR_Poll();
1044 #endif /* !AFS_PTHREAD_ENV */
1045     }
1046 }
1047
1048 void
1049 VResetDiskUsage(void)
1050 {
1051     VOL_LOCK;
1052     VResetDiskUsage_r();
1053     VOL_UNLOCK;
1054 }
1055
1056 void
1057 VAdjustDiskUsage_r(Error * ec, Volume * vp, afs_sfsize_t blocks,
1058                    afs_sfsize_t checkBlocks)
1059 {
1060     *ec = 0;
1061     /* why blocks instead of checkBlocks in the check below?  Otherwise, any check
1062      * for less than BlocksSpare would skip the error-checking path, and we
1063      * could grow existing files forever, not just for another BlocksSpare
1064      * blocks. */
1065     if (blocks > 0) {
1066 #ifdef  AFS_AIX32_ENV
1067         afs_int32 rem, minavail;
1068
1069         if ((rem = vp->partition->free - checkBlocks) < (minavail =
1070                                                          (vp->partition->
1071                                                           totalUsable *
1072                                                           aixlow_water) /
1073                                                          100))
1074 #else
1075         if (vp->partition->free - checkBlocks < 0)
1076 #endif
1077             *ec = VDISKFULL;
1078         else if (V_maxquota(vp)
1079                  && V_diskused(vp) + checkBlocks > V_maxquota(vp))
1080             *ec = VOVERQUOTA;
1081     }
1082     vp->partition->free -= blocks;
1083     V_diskused(vp) += blocks;
1084 }
1085
1086 void
1087 VAdjustDiskUsage(Error * ec, Volume * vp, afs_sfsize_t blocks,
1088                  afs_sfsize_t checkBlocks)
1089 {
1090     VOL_LOCK;
1091     VAdjustDiskUsage_r(ec, vp, blocks, checkBlocks);
1092     VOL_UNLOCK;
1093 }
1094
1095 int
1096 VDiskUsage_r(Volume * vp, afs_sfsize_t blocks)
1097 {
1098     if (blocks > 0) {
1099 #ifdef  AFS_AIX32_ENV
1100         afs_int32 rem, minavail;
1101
1102         if ((rem = vp->partition->free - blocks) < (minavail =
1103                                                     (vp->partition->
1104                                                      totalUsable *
1105                                                      aixlow_water) / 100))
1106 #else
1107         if (vp->partition->free - blocks < 0)
1108 #endif
1109             return (VDISKFULL);
1110     }
1111     vp->partition->free -= blocks;
1112     return 0;
1113 }
1114
1115 int
1116 VDiskUsage(Volume * vp, afs_sfsize_t blocks)
1117 {
1118     int retVal;
1119     VOL_LOCK;
1120     retVal = VDiskUsage_r(vp, blocks);
1121     VOL_UNLOCK;
1122     return retVal;
1123 }
1124
1125 void
1126 VPrintDiskStats_r(void)
1127 {
1128     struct DiskPartition64 *dp;
1129     for (dp = DiskPartitionList; dp; dp = dp->next) {
1130         if (dp->free < 0) {
1131             Log("Partition %s: %"AFS_INT64_FMT
1132                 " available 1K blocks (minfree=%"AFS_INT64_FMT"), "
1133                 "overallocated by %"AFS_INT64_FMT" blocks\n", dp->name,
1134                 dp->totalUsable, dp->minFree, -dp->free);
1135         } else {
1136             Log("Partition %s: %"AFS_INT64_FMT
1137                 " available 1K blocks (minfree=%"AFS_INT64_FMT"), "
1138                 "%"AFS_INT64_FMT" free blocks\n", dp->name,
1139                 dp->totalUsable, dp->minFree, dp->free);
1140         }
1141     }
1142 }
1143
1144 void
1145 VPrintDiskStats(void)
1146 {
1147     VOL_LOCK;
1148     VPrintDiskStats_r();
1149     VOL_UNLOCK;
1150 }
1151
1152 #ifdef AFS_NT40_ENV
1153 /* Need a separate lock file on NT, since NT only has mandatory file locks. */
1154 #define LOCKFILE "LOCKFILE"
1155 void
1156 VLockPartition_r(char *name)
1157 {
1158     struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1159     OVERLAPPED lap;
1160
1161     if (!dp)
1162         return;
1163     if (dp->lock_fd == INVALID_FD) {
1164         char path[64];
1165         int rc;
1166         (void)sprintf(path, "%s\\%s", VPartitionPath(dp), LOCKFILE);
1167         dp->lock_fd =
1168             (FD_t)CreateFile(path, GENERIC_WRITE,
1169                             FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1170                             CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
1171         osi_Assert(dp->lock_fd != INVALID_FD);
1172
1173         memset(&lap, 0, sizeof(lap));
1174         rc = LockFileEx((HANDLE) dp->lock_fd, LOCKFILE_EXCLUSIVE_LOCK, 0, 1,
1175                         0, &lap);
1176         osi_Assert(rc);
1177     }
1178 }
1179
1180 void
1181 VUnlockPartition_r(char *name)
1182 {
1183     struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1184     OVERLAPPED lap;
1185
1186     if (!dp)
1187         return;                 /* no partition, will fail later */
1188     memset(&lap, 0, sizeof(lap));
1189
1190     UnlockFileEx((HANDLE) dp->lock_fd, 0, 1, 0, &lap);
1191     CloseHandle((HANDLE) dp->lock_fd);
1192     dp->lock_fd = INVALID_FD;
1193 }
1194 #else /* AFS_NT40_ENV */
1195
1196 #if defined(AFS_HPUX_ENV)
1197 #define BITS_PER_CHAR   (8)
1198 #define BITS(type)      (sizeof(type) * BITS_PER_CHAR)
1199
1200 #define LOCKRDONLY_OFFSET       ((PRIV_LOCKRDONLY - 1) / BITS(int))
1201 #endif /* defined(AFS_HPUX_ENV) */
1202
1203 void
1204 VLockPartition_r(char *name)
1205 {
1206     struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1207     char *partitionName;
1208     int retries, code;
1209     struct timeval pausing;
1210 #if defined(AFS_HPUX_ENV)
1211     int lockfRtn;
1212     struct privgrp_map privGrpList[PRIV_MAXGRPS];
1213     unsigned int *globalMask;
1214     int globalMaskIndex;
1215 #endif /* defined(AFS_HPUX_ENV) */
1216 #if defined(AFS_DARWIN_ENV)
1217     char lockfile[MAXPATHLEN];
1218 #endif /* defined(AFS_DARWIN_ENV) */
1219 #ifdef AFS_NAMEI_ENV
1220 #ifdef AFS_AIX42_ENV
1221     char LockFileName[MAXPATHLEN + 1];
1222
1223     sprintf((char *)&LockFileName, "%s/AFSINODE_FSLock", name);
1224     partitionName = (char *)&LockFileName;
1225 #endif
1226 #endif
1227
1228     if (!dp)
1229         return;                 /* no partition, will fail later */
1230     if (dp->lock_fd != -1)
1231         return;
1232
1233 #if    defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
1234 #if !defined(AFS_AIX42_ENV) || !defined(AFS_NAMEI_ENV)
1235     partitionName = dp->devName;
1236 #endif
1237     code = O_RDWR;
1238 #elif defined(AFS_DARWIN_ENV)
1239     strlcpy((partitionName = lockfile), dp->name, sizeof(lockfile));
1240     strlcat(lockfile, "/.lock.afs", sizeof(lockfile));
1241     code = O_RDONLY | O_CREAT;
1242 #else
1243     partitionName = dp->name;
1244     code = O_RDONLY;
1245 #endif
1246
1247     for (retries = 25; retries; retries--) {
1248         if (code & O_CREAT)
1249             dp->lock_fd = afs_open(partitionName, code, 0644);
1250         else
1251             dp->lock_fd = afs_open(partitionName, code);
1252
1253         if (dp->lock_fd != -1)
1254             break;
1255         if (errno == ENOENT)
1256             code |= O_CREAT;
1257         pausing.tv_sec = 0;
1258         pausing.tv_usec = 500000;
1259         select(0, NULL, NULL, NULL, &pausing);
1260     }
1261     osi_Assert(retries != 0);
1262
1263 #if defined (AFS_HPUX_ENV)
1264
1265     osi_Assert(getprivgrp(privGrpList) == 0);
1266
1267     /*
1268      * In general, it will difficult and time-consuming ,if not impossible,
1269      * to try to find the privgroup to which this process belongs that has the
1270      * smallest membership, to minimise the security hole.  So, we use the privgrp
1271      * to which everybody belongs.
1272      */
1273     /* first, we have to find the global mask */
1274     for (globalMaskIndex = 0; globalMaskIndex < PRIV_MAXGRPS;
1275          globalMaskIndex++) {
1276         if (privGrpList[globalMaskIndex].priv_groupno == PRIV_GLOBAL) {
1277             globalMask =
1278                 &(privGrpList[globalMaskIndex].priv_mask[LOCKRDONLY_OFFSET]);
1279             break;
1280         }
1281     }
1282
1283     if (((*globalMask) & privmask(PRIV_LOCKRDONLY)) == 0) {
1284         /* allow everybody to set a lock on a read-only file descriptor */
1285         (*globalMask) |= privmask(PRIV_LOCKRDONLY);
1286         osi_Assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1287                == 0);
1288
1289         lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1290
1291         /* remove the privilege granted to everybody to lock a read-only fd */
1292         (*globalMask) &= ~(privmask(PRIV_LOCKRDONLY));
1293         osi_Assert(setprivgrp(PRIV_GLOBAL, privGrpList[globalMaskIndex].priv_mask)
1294                == 0);
1295     } else {
1296         /* in this case, we should be able to do this with impunity, anyway */
1297         lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
1298     }
1299
1300     osi_Assert(lockfRtn != -1);
1301 #else
1302 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
1303     osi_Assert(lockf(dp->lock_fd, F_LOCK, 0) != -1);
1304 #else
1305     osi_Assert(flock(dp->lock_fd, LOCK_EX) == 0);
1306 #endif /* defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) */
1307 #endif
1308 }
1309
1310 void
1311 VUnlockPartition_r(char *name)
1312 {
1313     struct DiskPartition64 *dp = VGetPartition_r(name, 0);
1314     if (!dp)
1315         return;                 /* no partition, will fail later */
1316     close(dp->lock_fd);
1317     dp->lock_fd = -1;
1318 }
1319
1320 #endif /* AFS_NT40_ENV */
1321
1322 void
1323 VLockPartition(char *name)
1324 {
1325     VOL_LOCK;
1326     VLockPartition_r(name);
1327     VOL_UNLOCK;
1328 }
1329
1330 void
1331 VUnlockPartition(char *name)
1332 {
1333     VOL_LOCK;
1334     VUnlockPartition_r(name);
1335     VOL_UNLOCK;
1336 }
1337
1338 #ifdef AFS_DEMAND_ATTACH_FS
1339
1340 /* new-style partition locks; these are only to have some mutual exclusion
1341  * between the VGC scanner and volume utilies creating/altering vol headers
1342  */
1343
1344 /**
1345  * lock a partition's vol headers.
1346  *
1347  * @param[in] dp       the partition to lock
1348  * @param[in] locktype READ_LOCK or WRITE_LOCK
1349  *
1350  * @return operation status
1351  *  @retval 0 success
1352  */
1353 int
1354 VPartHeaderLock(struct DiskPartition64 *dp, int locktype)
1355 {
1356     int code;
1357
1358     /* block on acquiring the lock */
1359     int nonblock = 0;
1360
1361     code = VGetDiskLock(&dp->headerLock, locktype, nonblock);
1362     if (code) {
1363         Log("VPartHeaderLock: error %d locking partititon %s\n", code,
1364             VPartitionPath(dp));
1365     }
1366     return code;
1367 }
1368
1369 /**
1370  * unlock a partition's vol headers.
1371  *
1372  * @param[in] dp       the partition to unlock
1373  * @param[in] locktype READ_LOCK or WRITE_LOCK
1374  */
1375 void
1376 VPartHeaderUnlock(struct DiskPartition64 *dp, int locktype)
1377 {
1378     VReleaseDiskLock(&dp->headerLock, locktype);
1379 }
1380
1381 /* XXX not sure this will work on AFS_NT40_ENV
1382  * needs to be tested!
1383  */
1384
1385 /**
1386  * lookup a disk partition object by its index number.
1387  *
1388  * @param[in] id      partition index number
1389  * @param[in] abortp  see abortp usage note below
1390  *
1391  * @return disk partition object
1392  *   @retval NULL no such disk partition
1393  *
1394  * @note when abortp is non-zero, lookups which would return
1395  *       NULL will result in an assertion failure
1396  *
1397  * @pre VOL_LOCK must be held
1398  *
1399  * @internal volume package internal use only
1400  */
1401
1402 struct DiskPartition64 *
1403 VGetPartitionById_r(afs_int32 id, int abortp)
1404 {
1405     struct DiskPartition64 *dp = NULL;
1406
1407     if ((id >= 0) && (id <= VOLMAXPARTS)) {
1408         dp = DiskPartitionTable[id];
1409     }
1410
1411     if (abortp) {
1412         osi_Assert(dp != NULL);
1413     }
1414     return dp;
1415 }
1416
1417 /**
1418  * lookup a disk partition object by its index number.
1419  *
1420  * @param[in] id      partition index number
1421  * @param[in] abortp  see abortp usage note below
1422  *
1423  * @return disk partition object
1424  *   @retval NULL no such disk partition
1425  *
1426  * @note when abortp is non-zero, lookups which would return
1427  *       NULL will result in an assertion failure
1428  */
1429
1430 struct DiskPartition64 *
1431 VGetPartitionById(afs_int32 id, int abortp)
1432 {
1433     struct DiskPartition64 * dp;
1434
1435     VOL_LOCK;
1436     dp = VGetPartitionById_r(id, abortp);
1437     VOL_UNLOCK;
1438
1439     return dp;
1440 }
1441
1442 static struct DiskPartition64 *
1443 VLookupPartition_r(char * path)
1444 {
1445     afs_int32 id = volutil_GetPartitionID(path);
1446
1447     if (id < 0 || id > VOLMAXPARTS)
1448         return NULL;
1449
1450     return DiskPartitionTable[id];
1451 }
1452
1453 static void
1454 AddPartitionToTable_r(struct DiskPartition64 *dp)
1455 {
1456     osi_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1457     DiskPartitionTable[dp->index] = dp;
1458 }
1459
1460 #if 0
1461 static void
1462 DeletePartitionFromTable_r(struct DiskPartition64 *dp)
1463 {
1464     osi_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1465     DiskPartitionTable[dp->index] = NULL;
1466 }
1467 #endif
1468 #endif /* AFS_DEMAND_ATTACH_FS */