Add opr/lock.h and tidy locking macros
[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 <afs/opr.h>
100 #ifdef AFS_PTHREAD_ENV
101 # include <opr/lock.h>
102 #endif
103 #include <afs/afsint.h>
104 #include <rx/rx_queue.h>
105 #include "nfs.h"
106 #include <afs/errors.h>
107 #include "lock.h"
108 #include "lwp.h"
109 #include <afs/afssyscalls.h>
110 #include "ihandle.h"
111 #include "common.h"
112 #ifdef AFS_NAMEI_ENV
113 #ifdef AFS_NT40_ENV
114 #include "ntops.h"
115 #else
116 #include "namei_ops.h"
117 #endif /* AFS_NT40_ENV */
118 #endif /* AFS_NAMEI_ENV */
119 #include "vnode.h"
120 #include "volume.h"
121 #include "partition.h"
122
123 #if defined(AFS_HPUX_ENV)
124 #include <sys/privgrp.h>
125 #endif /* defined(AFS_HPUX_ENV) */
126
127 #ifdef AFS_AIX42_ENV
128 #include <jfs/filsys.h>
129 #endif
130
131 #ifdef AFS_NT40_ENV
132 extern int VValidVPTEntry(struct vptab *vptp);
133 #endif
134
135 int aixlow_water = 8;           /* default 8% */
136 struct DiskPartition64 *DiskPartitionList;
137
138 #ifdef AFS_DEMAND_ATTACH_FS
139 /* file to lock to conceptually "lock" the vol headers on a partition */
140 #define AFS_PARTLOCK_FILE ".volheaders.lock"
141 #define AFS_VOLUMELOCK_FILE ".volume.lock"
142
143 static struct DiskPartition64 *DiskPartitionTable[VOLMAXPARTS+1];
144
145 static struct DiskPartition64 * VLookupPartition_r(char * path);
146 static void AddPartitionToTable_r(struct DiskPartition64 *);
147 #endif /* AFS_DEMAND_ATTACH_FS */
148
149 #ifdef AFS_SGI_XFS_IOPS_ENV
150 /* Verify that the on disk XFS inodes on the partition are large enough to
151  * hold the AFS attribute. Returns -1 if the attribute can't be set or is
152  * too small to fit in the inode. Returns 0 if the attribute does fit in
153  * the XFS inode.
154  */
155 #include <afs/xfsattrs.h>
156 static int
157 VerifyXFSInodeSize(char *part, char *fstype)
158 {
159     afs_xfs_attr_t junk;
160     int length = SIZEOF_XFS_ATTR_T;
161     int fd = 0;
162     int code = -1;
163     struct fsxattr fsx;
164
165     if (strcmp("xfs", fstype))
166         return 0;
167
168     if (attr_set(part, AFS_XFS_ATTR, &junk, length, ATTR_ROOT) == 0) {
169         if (((fd = open(part, O_RDONLY, 0)) != -1)
170             && (fcntl(fd, F_FSGETXATTRA, &fsx) == 0)) {
171
172             if (fsx.fsx_nextents) {
173                 Log("Partition %s: XFS inodes too small, exiting.\n", part);
174                 Log("Run xfs_size_check utility and remake partitions.\n");
175             } else
176                 code = 0;
177         }
178
179         if (fd > 0)
180             close(fd);
181         (void)attr_remove(part, AFS_XFS_ATTR, ATTR_ROOT);
182     }
183     return code;
184 }
185 #endif /* AFS_SGI_XFS_IOPS_ENV */
186
187 int
188 VInitPartitionPackage(void)
189 {
190 #ifdef AFS_DEMAND_ATTACH_FS
191     memset(&DiskPartitionTable, 0, sizeof(DiskPartitionTable));
192 #endif /* AFS_DEMAND_ATTACH_FS */
193     return 0;
194 }
195
196 static void
197 VInitPartition_r(char *path, char *devname, Device dev)
198 {
199     struct DiskPartition64 *dp, *op;
200
201     dp = malloc(sizeof(struct DiskPartition64));
202     /* Add it to the end, to preserve order when we print statistics */
203     for (op = DiskPartitionList; op; op = op->next) {
204         if (!op->next)
205             break;
206     }
207     if (op)
208         op->next = dp;
209     else
210         DiskPartitionList = dp;
211     dp->next = 0;
212     dp->name = strdup(path);
213     dp->index = volutil_GetPartitionID(path);
214 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
215     /* Create a lockfile for the partition, of the form /vicepa/Lock/vicepa */
216     dp->devName = malloc(2 * strlen(path) + 6);
217     strcpy(dp->devName, path);
218     strcat(dp->devName, OS_DIRSEP);
219     strcat(dp->devName, "Lock");
220     mkdir(dp->devName, 0700);
221     strcat(dp->devName, path);
222     close(afs_open(dp->devName, O_RDWR | O_CREAT, 0600));
223     dp->device = dp->index;
224 #else
225     dp->devName = strdup(devname);
226     dp->device = dev;
227 #endif
228     dp->lock_fd = INVALID_FD;
229     dp->flags = 0;
230     dp->f_files = 1;            /* just a default value */
231 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
232     if (programType == fileServer)
233         (void)namei_ViceREADME(VPartitionPath(dp));
234 #endif
235     VSetPartitionDiskUsage_r(dp);
236 #ifdef AFS_DEMAND_ATTACH_FS
237     AddPartitionToTable_r(dp);
238     queue_Init(&dp->vol_list.head);
239     CV_INIT(&dp->vol_list.cv, "vol list", CV_DEFAULT, 0);
240     dp->vol_list.len = 0;
241     dp->vol_list.busy = 0;
242     {
243         char lockpath[MAXPATHLEN+1];
244         snprintf(lockpath, MAXPATHLEN, "%s/" AFS_PARTLOCK_FILE, dp->name);
245         lockpath[MAXPATHLEN] = '\0';
246         VLockFileInit(&dp->headerLockFile, lockpath);
247
248         snprintf(lockpath, MAXPATHLEN, "%s/" AFS_VOLUMELOCK_FILE, dp->name);
249         lockpath[MAXPATHLEN] = '\0';
250         VLockFileInit(&dp->volLockFile, lockpath);
251     }
252     VDiskLockInit(&dp->headerLock, &dp->headerLockFile, 1);
253 #endif /* AFS_DEMAND_ATTACH_FS */
254 }
255
256 static void
257 VInitPartition(char *path, char *devname, Device dev)
258 {
259     VOL_LOCK;
260     VInitPartition_r(path, devname, dev);
261     VOL_UNLOCK;
262 }
263
264 #ifndef AFS_NT40_ENV
265 /* VAttachPartitions() finds the vice partitions on this server. Calls
266  * VCheckPartition() to do some basic checks on the partition. If the partition
267  * is a valid vice partition, VCheckPartition will add it to the DiskPartition
268  * list.
269  * Returns the number of errors returned by VCheckPartition. An error in
270  * VCheckPartition means that partition is a valid vice partition but the
271  * fileserver should not start because of the error found on that partition.
272  *
273  * AFS_NAMEI_ENV
274  * No specific user space file system checks, since we don't know what
275  * is being used for vice partitions.
276  *
277  * Use partition name as devname.
278  */
279 static int
280 VCheckPartition(char *part, char *devname, int logging)
281 {
282     struct afs_stat_st status;
283 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
284     char AFSIDatPath[MAXPATHLEN];
285 #endif
286
287     /* Only keep track of "/vicepx" partitions since it can get hairy
288      * when NFS mounts are involved.. */
289     if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE)) {
290         return 0;
291     }
292     if (afs_stat(part, &status) < 0) {
293         Log("VInitVnodes: Couldn't find file system %s; ignored\n", part);
294         return 0;
295     }
296     if (logging) {
297         Log("This program is compiled without AFS_NAMEI_ENV, and "
298             "partition %s is mounted with the 'logging' option. "
299             "Using the inode fileserver backend with 'logging' UFS "
300             "partitions causes volume corruption, so please either "
301             "mount the partition without logging, or use the namei "
302             "fileserver backend. Aborting...\n", part);
303         return -1;
304     }
305 #ifndef AFS_AIX32_ENV
306     if (programType == fileServer) {
307         char salvpath[MAXPATHLEN];
308         strcpy(salvpath, part);
309         strcat(salvpath, "/FORCESALVAGE");
310         if (afs_stat(salvpath, &status) == 0) {
311             Log("VInitVnodes: Found %s; aborting\n", salvpath);
312             return -1;
313         }
314     }
315 #endif
316
317 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
318     strcpy(AFSIDatPath, part);
319     strcat(AFSIDatPath, "/AFSIDat");
320 #ifdef AFS_NAMEI_ENV
321     if (afs_stat(AFSIDatPath, &status) < 0) {
322         DIR *dirp;
323         struct dirent *dp;
324
325         dirp = opendir(part);
326         opr_Assert(dirp);
327         while ((dp = readdir(dirp))) {
328             if (dp->d_name[0] == 'V') {
329                 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);
330                 closedir(dirp);
331                 return -1;
332             }
333         }
334         closedir(dirp);
335     }
336 #else /* AFS_NAMEI_ENV */
337     if (afs_stat(AFSIDatPath, &status) == 0) {
338         Log("This program is compiled without AFS_NAMEI_ENV, but partition %s seems to contain volumes which use the namei-interface; aborting\n", part);
339         return -1;
340     }
341
342 #ifdef AFS_SGI_XFS_IOPS_ENV
343     if (VerifyXFSInodeSize(part, status.st_fstype) < 0)
344         return -1;
345 #endif
346 #endif /* AFS_NAMEI_ENV */
347 #endif /* !AFS_LINUX20_ENV && !AFS_NT40_ENV */
348
349 #if defined(AFS_DUX40_ENV) && !defined(AFS_NAMEI_ENV)
350     if (status.st_ino != ROOTINO) {
351         Log("%s is not a mounted file system; ignored.\n", part);
352         return 0;
353     }
354 #endif
355
356     VInitPartition(part, devname, status.st_dev);
357
358     return 0;
359 }
360
361 /* VIsAlwaysAttach() checks whether a /vicepX directory should always be
362  * attached (return value 1), or only attached when it is a separately
363  * mounted partition (return value 0).  For non-NAMEI environments, it
364  * always returns 0.
365  *
366  * *awouldattach will be set to 1 if the given path at least looks like a vice
367  * partition (that is, if we return 0, the only thing preventing this partition
368  * from being attached is the existence of the AlwaysAttach file), or to 0
369  * otherwise. *awouldattach is set regardless of whether or not the partition
370  * should always be attached or not.
371  */
372 static int
373 VIsAlwaysAttach(char *part, int *awouldattach)
374 {
375 #ifdef AFS_NAMEI_ENV
376     struct afs_stat_st st;
377     char checkfile[256];
378     int ret;
379 #endif /* AFS_NAMEI_ENV */
380
381     if (awouldattach) {
382         *awouldattach = 0;
383     }
384
385 #ifdef AFS_NAMEI_ENV
386     if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
387         return 0;
388
389     if (awouldattach) {
390         *awouldattach = 1;
391     }
392
393     strncpy(checkfile, part, 100);
394     strcat(checkfile, OS_DIRSEP);
395     strcat(checkfile, VICE_ALWAYSATTACH_FILE);
396
397     ret = afs_stat(checkfile, &st);
398     return (ret < 0) ? 0 : 1;
399 #else /* AFS_NAMEI_ENV */
400     return 0;
401 #endif /* AFS_NAMEI_ENV */
402 }
403
404 /* VIsNeverAttach() checks whether a /vicepX directory should never be
405  * attached (return value 1), or follow the normal mounting logic. The
406  * Always Attach flag may override the NeverAttach flag.
407  */
408 static int
409 VIsNeverAttach(char *part)
410 {
411     struct afs_stat_st st;
412     char checkfile[256];
413     int ret;
414
415     if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
416         return 0;
417
418     strncpy(checkfile, part, 100);
419     strcat(checkfile, OS_DIRSEP);
420     strcat(checkfile, VICE_NEVERATTACH_FILE);
421
422     ret = afs_stat(checkfile, &st);
423     return (ret < 0) ? 0 : 1;
424 }
425
426 /* VAttachPartitions2() looks for and attaches /vicepX partitions
427  * where a special file (VICE_ALWAYSATTACH_FILE) exists.  This is
428  * used to attach /vicepX directories which aren't on dedicated
429  * partitions, in the NAMEI fileserver.
430  */
431 static void
432 VAttachPartitions2(void)
433 {
434 #ifdef AFS_NAMEI_ENV
435     DIR *dirp;
436     struct dirent *de;
437     char pname[32];
438     int wouldattach;
439
440     dirp = opendir(OS_DIRSEP);
441     while ((de = readdir(dirp))) {
442         strcpy(pname, OS_DIRSEP);
443         strncat(pname, de->d_name, 20);
444         pname[sizeof(pname) - 1] = '\0';
445
446         /* Only keep track of "/vicepx" partitions since automounter
447          * may hose us */
448         if (VIsAlwaysAttach(pname, &wouldattach)) {
449             VCheckPartition(pname, "", 0);
450         } else {
451             struct afs_stat_st st;
452             if (wouldattach && VGetPartition(pname, 0) == NULL &&
453                 afs_stat(pname, &st) == 0 && S_ISDIR(st.st_mode)) {
454
455                 /* This is a /vicep* dir, and it has not been attached as a
456                  * partition. This probably means that this is a /vicep* dir
457                  * that is not a separate partition, so just give a notice so
458                  * admins are not confused as to why their /vicep* dirs are not
459                  * being attached.
460                  *
461                  * It is possible that the dir _is_ a separate partition and we
462                  * failed to attach it earlier, making this message a bit
463                  * confusing. But that should be rare, and an error message
464                  * about the failure will already be logged right before this,
465                  * so it should be clear enough. */
466
467                 Log("VAttachPartitions: not attaching %s; either it is not a "
468                     "separate partition, or it failed to attach (create the "
469                     "file %s/" VICE_ALWAYSATTACH_FILE " to force attachment)\n",
470                     pname, pname);
471             }
472         }
473     }
474     closedir(dirp);
475 #endif /* AFS_NAMEI_ENV */
476 }
477 #endif /* AFS_NT40_ENV */
478
479 #ifdef AFS_SUN5_ENV
480 int
481 VAttachPartitions(void)
482 {
483     int errors = 0;
484     struct mnttab mnt;
485     FILE *mntfile;
486
487     if (!(mntfile = afs_fopen(MNTTAB, "r"))) {
488         Log("Can't open %s\n", MNTTAB);
489         perror(MNTTAB);
490         exit(-1);
491     }
492     while (!getmntent(mntfile, &mnt)) {
493         int logging = 0;
494         /* Ignore non ufs or non read/write partitions */
495         /* but allow zfs too if we're in the NAMEI environment */
496         if (
497 #ifdef AFS_NAMEI_ENV
498             (((strcmp(mnt.mnt_fstype, "ufs") &&
499                 strcmp(mnt.mnt_fstype, "zfs"))))
500 #else
501             (strcmp(mnt.mnt_fstype, "ufs") != 0)
502 #endif
503             || (strncmp(mnt.mnt_mntopts, "ro,ignore", 9) == 0))
504             continue;
505
506         /* Skip this Partition? */
507         if (VIsNeverAttach(mnt.mnt_mountp))
508             continue;
509
510         /* If we're going to always attach this partition, do it later. */
511         if (VIsAlwaysAttach(mnt.mnt_mountp, NULL))
512             continue;
513
514 #ifndef AFS_NAMEI_ENV
515         if (hasmntopt(&mnt, "logging") != NULL) {
516             logging = 1;
517         }
518 #endif /* !AFS_NAMEI_ENV */
519
520         if (VCheckPartition(mnt.mnt_mountp, mnt.mnt_special, logging) < 0)
521             errors++;
522     }
523
524     (void)fclose(mntfile);
525
526     /* Process the always-attach partitions, if any. */
527     VAttachPartitions2();
528
529     return errors;
530 }
531
532 #endif /* AFS_SUN5_ENV */
533 #if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)) || defined(AFS_HPUX_ENV)
534 int
535 VAttachPartitions(void)
536 {
537     int errors = 0;
538     FILE *mfd;
539     struct mntent *mntent;
540
541     if ((mfd = setmntent(MOUNTED, "r")) == NULL) {
542         Log("Problems in getting mount entries(setmntent)\n");
543         exit(-1);
544     }
545     while (mntent = getmntent(mfd)) {
546         if (!hasmntopt(mntent, MNTOPT_RW))
547             continue;
548
549         /* Skip this Partition? */
550         if (VIsNeverAttach(mntent->mnt_dir))
551             continue;
552
553         /* If we're going to always attach this partition, do it later. */
554         if (VIsAlwaysAttach(mntent->mnt_dir, NULL))
555             continue;
556
557         if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname, 0) < 0)
558             errors++;
559     }
560
561     endmntent(mfd);
562
563     /* Process the always-attach partitions, if any. */
564     VAttachPartitions2();
565
566     return errors;
567 }
568 #endif
569 #ifdef AFS_AIX_ENV
570 /*
571  * (This function was grabbed from df.c)
572  */
573 int
574 getmount(struct vmount **vmountpp)
575 {
576     int size;
577     struct vmount *vm;
578     int nmounts;
579
580     /* set initial size of mntctl buffer to a MAGIC NUMBER */
581     size = BUFSIZ;
582
583     /* try the operation until ok or a fatal error */
584     while (1) {
585         if ((vm = malloc(size)) == NULL) {
586             /* failed getting memory for mount status buf */
587             perror("FATAL ERROR: get_stat malloc failed\n");
588             exit(-1);
589         }
590
591         /*
592          * perform the QUERY mntctl - if it returns > 0, that is the
593          * number of vmount structures in the buffer.  If it returns
594          * -1, an error occured.  If it returned 0, then look in
595          * first word of buffer for needed size.
596          */
597         if ((nmounts = mntctl(MCTL_QUERY, size, (caddr_t) vm)) > 0) {
598             /* OK, got it, now return */
599             *vmountpp = vm;
600             return (nmounts);
601
602         } else if (nmounts == 0) {
603             /* the buffer wasn't big enough .... */
604             /* .... get required buffer size */
605             size = *(int *)vm;
606             free(vm);
607
608         } else {
609             /* some other kind of error occurred */
610             free(vm);
611             return (-1);
612         }
613     }
614 }
615
616 int
617 VAttachPartitions(void)
618 {
619     int errors = 0;
620     int nmounts;
621     struct vmount *vmountp;
622
623     if ((nmounts = getmount(&vmountp)) <= 0) {
624         Log("Problems in getting # of mount entries(getmount)\n");
625         exit(-1);
626     }
627     for (; nmounts;
628          nmounts--, vmountp =
629          (struct vmount *)((int)vmountp + vmountp->vmt_length)) {
630         char *part = vmt2dataptr(vmountp, VMT_STUB);
631
632         if (vmountp->vmt_flags & (MNT_READONLY | MNT_REMOVABLE | MNT_REMOTE))
633             continue;           /* Ignore any "special" partitions */
634
635 #ifdef AFS_AIX42_ENV
636 #ifndef AFS_NAMEI_ENV
637         {
638             struct superblock fs;
639             /* The Log statements are non-sequiters in the SalvageLog and don't
640              * even appear in the VolserLog, so restrict them to the FileLog.
641              */
642             if (ReadSuper(&fs, vmt2dataptr(vmountp, VMT_OBJECT)) < 0) {
643                 if (programType == fileServer)
644                     Log("Can't read superblock for %s, ignoring it.\n", part);
645                 continue;
646             }
647             if (IsBigFilesFileSystem(&fs)) {
648                 if (programType == fileServer)
649                     Log("%s is a big files filesystem, ignoring it.\n", part);
650                 continue;
651             }
652         }
653 #endif
654 #endif
655
656         /* Skip this Partition? */
657         if (VIsNeverAttach(part))
658             continue;
659
660         /* If we're going to always attach this partition, do it later. */
661         if (VIsAlwaysAttach(part, NULL))
662             continue;
663
664         if (VCheckPartition(part, vmt2dataptr(vmountp, VMT_OBJECT), 0) < 0)
665             errors++;
666     }
667
668     /* Process the always-attach partitions, if any. */
669     VAttachPartitions2();
670
671     return errors;
672 }
673 #endif
674 #if defined(AFS_DUX40_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
675 int
676 VAttachPartitions(void)
677 {
678     int errors = 0;
679     struct fstab *fsent;
680
681     if (setfsent() < 0) {
682         Log("Error listing filesystems.\n");
683         exit(-1);
684     }
685
686     while ((fsent = getfsent())) {
687         if (strcmp(fsent->fs_type, "rw") != 0)
688             continue;
689
690         /* Skip this Partition? */
691         if (VIsNeverAttach(fsent->fs_file))
692             continue;
693
694         /* If we're going to always attach this partition, do it later. */
695         if (VIsAlwaysAttach(fsent->fs_file, NULL))
696             continue;
697
698         if (VCheckPartition(fsent->fs_file, fsent->fs_spec, 0) < 0)
699             errors++;
700     }
701     endfsent();
702
703     /* Process the always-attach partitions, if any. */
704     VAttachPartitions2();
705
706     return errors;
707 }
708 #endif
709
710 #ifdef AFS_NT40_ENV
711 /* VValidVPTEntry
712  *
713  * validate names in vptab.
714  *
715  * Return value:
716  * 1 valid entry
717  * 0 invalid entry
718  */
719
720 static int
721 VValidVPTEntry(struct vptab *vpe)
722 {
723     int len = strlen(vpe->vp_name);
724     int i;
725
726     if (len < VICE_PREFIX_SIZE + 1 || len > VICE_PREFIX_SIZE + 2)
727         return 0;
728     if (strncmp(vpe->vp_name, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
729         return 0;
730
731     for (i = VICE_PREFIX_SIZE; i < len; i++) {
732         if (vpe->vp_name[i] < 'a' || vpe->vp_name[i] > 'z') {
733             Log("Invalid partition name %s in registry, ignoring it.\n",
734                 vpe->vp_name);
735             return 0;
736         }
737     }
738     if (len == VICE_PREFIX_SIZE + 2) {
739         i = (int)(vpe->vp_name[VICE_PREFIX_SIZE] - 'a') * 26 +
740             (int)(vpe->vp_name[VICE_PREFIX_SIZE + 1] - 'a');
741         if (i > 255) {
742             Log("Invalid partition name %s in registry, ignoring it.\n",
743                 vpe->vp_name);
744             return 0;
745         }
746     }
747
748     len = strlen(vpe->vp_dev);
749     if (len != 2 || vpe->vp_dev[1] != ':' || vpe->vp_dev[0] < 'A'
750         || vpe->vp_dev[0] > 'Z') {
751         Log("Invalid device name %s in registry, ignoring it.\n",
752             vpe->vp_dev);
753         return 0;
754     }
755
756     return 1;
757 }
758
759 static int
760 VCheckPartition(char *partName)
761 {
762     char volRoot[4];
763     char volFsType[64];
764     DWORD dwDummy;
765     int err;
766
767     /* partName is presumed to be of the form "X:" */
768     (void)sprintf(volRoot, "%c:\\", *partName);
769
770     if (!GetVolumeInformation(volRoot,  /* volume root directory */
771                               NULL,     /* volume name buffer */
772                               0,        /* volume name size */
773                               NULL,     /* volume serial number */
774                               &dwDummy, /* max component length */
775                               &dwDummy, /* file system flags */
776                               volFsType,        /* file system name */
777                               sizeof(volFsType))) {
778         err = GetLastError();
779         Log("VCheckPartition: Failed to get partition information for %s, ignoring it.\n", partName);
780         return -1;
781     }
782
783     if (strcmp(volFsType, "NTFS")) {
784         Log("VCheckPartition: Partition %s is not an NTFS partition, ignoring it.\n", partName);
785         return -1;
786     }
787
788     return 0;
789 }
790
791
792 int
793 VAttachPartitions(void)
794 {
795     struct DiskPartition64 *partP, *prevP, *nextP;
796     struct vpt_iter iter;
797     struct vptab entry;
798
799     if (vpt_Start(&iter) < 0) {
800         Log("No partitions to attach.\n");
801         return 0;
802     }
803
804     while (0 == vpt_NextEntry(&iter, &entry)) {
805         if (!VValidVPTEntry(&entry)) {
806             continue;
807         }
808
809         /* This test for duplicates relies on the fact that the method
810          * of storing the partition names in the NT registry means the same
811          * partition name will never appear twice in the list.
812          */
813         for (partP = DiskPartitionList; partP; partP = partP->next) {
814             if (*partP->devName == *entry.vp_dev) {
815                 Log("Same drive (%s) used for both partition %s and partition %s, ignoring both.\n", entry.vp_dev, partP->name, entry.vp_name);
816                 partP->flags = PART_DUPLICATE;
817                 break;          /* Only one entry will ever be in this list. */
818             }
819         }
820         if (partP)
821             continue;           /* found a duplicate */
822
823         if (VCheckPartition(entry.vp_dev) < 0)
824             continue;
825         /* This test allows for manually inserting the FORCESALVAGE flag
826          * and thereby invoking the salvager. scandisk obviously won't be
827          * doing this for us.
828          */
829         if (programType == fileServer) {
830             struct afs_stat_st status;
831             char salvpath[MAXPATHLEN];
832             strcpy(salvpath, entry.vp_dev);
833             strcat(salvpath, "\\FORCESALVAGE");
834             if (afs_stat(salvpath, &status) == 0) {
835                 Log("VAttachPartitions: Found %s; aborting\n", salvpath);
836                 exit(1);
837             }
838         }
839         VInitPartition(entry.vp_name, entry.vp_dev, *entry.vp_dev - 'A');
840     }
841     vpt_Finish(&iter);
842
843     /* Run through partition list and clear out the dupes. */
844     prevP = nextP = NULL;
845     for (partP = DiskPartitionList; partP; partP = nextP) {
846         nextP = partP->next;
847         if (partP->flags == PART_DUPLICATE) {
848             if (prevP)
849                 prevP->next = partP->next;
850             else
851                 DiskPartitionList = partP->next;
852             free(partP);
853         } else
854             prevP = partP;
855     }
856
857     return 0;
858 }
859 #endif
860
861 #ifdef AFS_LINUX22_ENV
862 int
863 VAttachPartitions(void)
864 {
865     int errors = 0;
866     FILE *mfd;
867     struct mntent *mntent;
868
869     if ((mfd = setmntent("/proc/mounts", "r")) == NULL) {
870         if ((mfd = setmntent("/etc/mtab", "r")) == NULL) {
871             Log("Problems in getting mount entries(setmntent)\n");
872             exit(-1);
873         }
874     }
875     while ((mntent = getmntent(mfd))) {
876         /* Skip this Partition? */
877         if (VIsNeverAttach(mntent->mnt_dir))
878             continue;
879
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) < 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         opr_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: %lld "
1132                 " available 1K blocks (minfree=%lld), "
1133                 "overallocated by %lld blocks\n", dp->name,
1134                 dp->totalUsable, dp->minFree, -dp->free);
1135         } else {
1136             Log("Partition %s: %lld"
1137                 " available 1K blocks (minfree=%lld), "
1138                 "%lld 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         opr_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         opr_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 != INVALID_FD)
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 != INVALID_FD)
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     opr_Assert(retries != 0);
1262
1263 #if defined (AFS_HPUX_ENV)
1264
1265     opr_Verify(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         opr_Verify(setprivgrp(PRIV_GLOBAL,
1287                               privGrpList[globalMaskIndex].priv_mask) == 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         opr_Verify(setprivgrp(PRIV_GLOBAL,
1294                               privGrpList[globalMaskIndex].priv_mask) == 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     opr_Assert(lockfRtn != -1);
1301 #else
1302 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
1303     opr_Verify(lockf(dp->lock_fd, F_LOCK, 0) != -1);
1304 #else
1305     opr_Verify(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 = INVALID_FD;
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         opr_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     opr_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     opr_Assert(dp->index >= 0 && dp->index <= VOLMAXPARTS);
1465     DiskPartitionTable[dp->index] = NULL;
1466 }
1467 #endif
1468 #endif /* AFS_DEMAND_ATTACH_FS */