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