afsconfig-and-rcsid-all-around-20010705
[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
10 /*
11
12         System:         VICE-TWO
13         Module:         partition.c
14         Institution:    The Information Technology Center, Carnegie-Mellon University
15
16  */
17
18 #include <afs/param.h>
19 #include <afsconfig.h>
20
21 RCSID("$Header$");
22
23 #include <ctype.h>
24 #ifdef AFS_NT40_ENV
25 #include <windows.h>
26 #include <winbase.h>
27 #include <winioctl.h>
28 #else
29 #include <sys/param.h>
30 #include <sys/types.h>
31  
32 #if AFS_HAVE_STATVFS
33 #include <sys/statvfs.h>
34 #endif /* AFS_HAVE_STATVFS */
35 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
36 #include <sys/mount.h>
37 #endif
38
39 #if !defined(AFS_SGI_ENV)
40 #ifdef  AFS_OSF_ENV
41 #include <sys/mount.h>
42 #include <ufs/fs.h>
43 #else   /* AFS_OSF_ENV */
44 #ifdef AFS_VFSINCL_ENV
45 #define VFS
46 #ifdef  AFS_SUN5_ENV
47 #include <sys/fs/ufs_fs.h>
48 #else
49 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
50 #include <ufs/ufs/dinode.h>
51 #include <ufs/ffs/fs.h>
52 #else
53 #include <ufs/fs.h>
54 #endif
55 #endif
56 #else /* AFS_VFSINCL_ENV */
57 #if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX22_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_FBSD_ENV)
58 #include <sys/fs.h>
59 #endif
60 #endif /* AFS_VFSINCL_ENV */
61 #endif  /* AFS_OSF_ENV */
62 #include <sys/errno.h>
63 #include <sys/stat.h>
64 #include <stdio.h>
65 #include <sys/file.h>
66 #ifdef  AFS_AIX_ENV
67 #include <sys/vfs.h>
68 #include <sys/lockf.h>
69 #else
70 #ifdef  AFS_HPUX_ENV
71 #include <sys/vfs.h>
72 #include <unistd.h>
73 #include <fcntl.h>
74 #include <checklist.h>
75 #else
76 #if     defined(AFS_SUN_ENV)
77 #include <sys/vfs.h>
78 #endif
79 #ifdef AFS_SUN5_ENV
80 #include <unistd.h>
81 #include <sys/mnttab.h>
82 #include <sys/mntent.h>
83 #else
84 #ifdef AFS_LINUX22_ENV
85 #include <mntent.h>
86 #include <sys/statfs.h>
87 #else
88 #include <fstab.h>
89 #endif
90 #endif
91 #endif
92 #endif
93 #endif  /* AFS_SGI_ENV */
94 #endif /* AFS_NT40_ENV */
95 #if defined(AFS_SGI_ENV)
96 #include <sys/errno.h>
97 #include <sys/stat.h>
98 #include <stdio.h>
99 #include <sys/file.h>
100 #include <mntent.h>
101 #endif
102
103 #include <rx/xdr.h>
104 #include <afs/afsint.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 #ifdef AFS_NAMEI_ENV
112 #ifdef AFS_NT40_ENV
113 #include "ntops.h"
114 #else
115 #include "namei_ops.h"
116 #if defined(AFS_SGI_ENV)
117 #include <sys/dir.h>
118 #else
119 #include <dirent.h>
120 #endif /* AFS_SGI_ENV */
121 #endif /* AFS_NT40_ENV */
122 #endif /* AFS_NAMEI_ENV */
123 #include "vnode.h"
124 #include "volume.h"
125 #include "partition.h"
126 #ifdef AFS_PTHREAD_ENV
127 #include <assert.h>
128 #else /* AFS_PTHREAD_ENV */
129 #include <afs/assert.h>
130 #endif /* AFS_PTHREAD_ENV */
131
132 #if defined(AFS_HPUX_ENV)
133 #include <sys/types.h>
134 #include <sys/privgrp.h>
135 #endif /* defined(AFS_HPUX_ENV) */
136
137 #ifdef AFS_AIX42_ENV
138 #include <jfs/filsys.h>
139 #endif
140
141 int aixlow_water = 8;   /* default 8% */
142 struct DiskPartition *DiskPartitionList;
143
144 #ifdef AFS_SGI_XFS_IOPS_ENV
145 /* Verify that the on disk XFS inodes on the partition are large enough to
146  * hold the AFS attribute. Returns -1 if the attribute can't be set or is
147  * too small to fit in the inode. Returns 0 if the attribute does fit in
148  * the XFS inode.
149  */
150 #include <afs/xfsattrs.h>
151 static int VerifyXFSInodeSize(char *part, char *fstype)
152 {
153     afs_xfs_attr_t junk;
154     int length = SIZEOF_XFS_ATTR_T;
155     int fd = 0;
156     int code = -1;
157     struct fsxattr fsx;
158
159     if (strcmp("xfs", fstype))
160         return 0;
161
162     if (attr_set(part, AFS_XFS_ATTR, &junk, length, ATTR_ROOT) == 0) {
163         if (((fd=open(part, O_RDONLY, 0)) != -1)
164             && (fcntl(fd, F_FSGETXATTRA, &fsx) == 0)) {
165         
166             if (fsx.fsx_nextents) {
167                 Log("Partition %s: XFS inodes too small, exiting.\n", part);
168                 Log("Run xfs_size_check utility and remake partitions.\n");
169             }
170             else
171                 code = 0;
172         }
173
174         if (fd > 0)
175             close(fd);
176         (void) attr_remove(part, AFS_XFS_ATTR, ATTR_ROOT);
177     }
178     return code;
179 }
180 #endif
181
182
183 static void VInitPartition_r(char *path, char *devname, Device dev)
184 {
185     struct DiskPartition *dp, *op;
186     dp = (struct DiskPartition *) malloc(sizeof (struct DiskPartition));
187     /* Add it to the end, to preserve order when we print statistics */
188     for (op = DiskPartitionList; op; op = op->next) {
189         if (!op->next)
190             break;
191     }
192     if (op)
193         op->next = dp;
194     else
195         DiskPartitionList = dp;
196     dp->next = 0;
197     strcpy(dp->name, path);
198 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
199 #ifdef AFS_SUN5_ENV
200     strcpy(dp->devName, devname);
201 #else /* AFS_SUN5_ENV */
202     strcpy(dp->devName, path);
203 #endif
204     dp->device = volutil_GetPartitionID(path);
205 #else
206     strcpy(dp->devName, devname);
207     dp->device = dev;
208 #endif
209     dp->lock_fd = -1;
210     dp->flags = 0;
211     dp->f_files = 1;    /* just a default value */
212 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
213     if (programType == fileServer)
214         (void) namei_ViceREADME(VPartitionPath(dp));
215 #endif
216     VSetPartitionDiskUsage_r(dp);
217 }
218
219 static void VInitPartition(char *path, char *devname, Device dev)
220 {
221     VOL_LOCK
222     VInitPartition_r(path, devname, dev);
223     VOL_UNLOCK
224 }
225
226 #ifndef AFS_NT40_ENV
227 /* VAttachPartitions() finds the vice partitions on this server. Calls
228  * VCheckPartition() to do some basic checks on the partition. If the partition
229  * is a valid vice partition, VCheckPartition will add it to the DiskPartition
230  * list.
231  * Returns the number of errors returned by VCheckPartition. An error in
232  * VCheckPartition means that partition is a valid vice partition but the
233  * fileserver should not start because of the error found on that partition.
234  *
235  * AFS_NAMEI_ENV
236  * No specific user space file system checks, since we don't know what
237  * is being used for vice partitions.
238  *
239  * Use partition name as devname.
240  */
241 int VCheckPartition(part, devname)
242      char *part;
243      char *devname;
244 {
245     struct stat status;
246 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
247     char AFSIDatPath[MAXPATHLEN];
248 #endif
249
250     /* Only keep track of "/vicepx" partitions since it can get hairy
251      * when NFS mounts are involved.. */
252     if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE)) {
253         return 0;
254     }
255     if (stat(part, &status) < 0) {
256         Log("VInitVnodes: Couldn't find file system %s; ignored\n", part);
257         return 0;
258     }
259     
260 #ifndef AFS_AIX32_ENV
261     if (programType == fileServer) {
262         char salvpath[MAXPATHLEN];
263         strcpy(salvpath, part);
264         strcat(salvpath, "/FORCESALVAGE");
265         if (stat(salvpath, &status) == 0) {
266             Log("VInitVnodes: Found %s; aborting\n", salvpath);
267             return -1;
268         }
269     }
270 #endif
271
272 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
273     strcpy(AFSIDatPath, part);
274     strcat(AFSIDatPath, "/AFSIDat");
275 #ifdef AFS_NAMEI_ENV
276     if (stat(AFSIDatPath, &status) < 0) {
277         DIR *dirp;
278         struct dirent *dp;
279
280         dirp = opendir(part);
281         assert(dirp);
282         while (dp = readdir(dirp)) {
283             if (dp->d_name[0] == 'V') {
284                 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);
285                 closedir(dirp);
286                 return -1;
287             }
288         }
289         closedir(dirp);
290     }
291 #else /* AFS_NAMEI_ENV */
292     if (stat(AFSIDatPath, &status) == 0) {
293         Log("This program is compiled without AFS_NAMEI_ENV, but partition %s seems to contain volumes which use the namei-interface; aborting\n", part);
294         return -1;
295     }
296 #endif /* AFS_NAMEI_ENV */
297 #endif
298
299 #ifdef AFS_SGI_XFS_IOPS_ENV
300     if (VerifyXFSInodeSize(part, status.st_fstype) < 0)
301         return -1;
302 #endif
303
304 #ifdef AFS_DUX40_ENV
305     if (status.st_ino != ROOTINO) {
306         Log("%s is not a mounted file system; ignored.\n", part);
307         return 0;
308     }
309 #endif
310
311     VInitPartition(part, devname, status.st_dev);
312
313     return 0;
314 }
315 #endif /* AFS_NT40_ENV */
316
317 #ifdef AFS_SUN5_ENV
318 int VAttachPartitions(void)
319 {
320     int errors = 0;
321     struct mnttab mnt;
322     FILE *mntfile;
323
324     if (!(mntfile = fopen(MNTTAB, "r"))) {
325         Log("Can't open %s\n", MNTTAB);
326         perror(MNTTAB);
327         exit(-1);
328     }
329     while (!getmntent(mntfile, &mnt)) {
330         /* Ignore non ufs or non read/write partitions */
331         if ((strcmp(mnt.mnt_fstype, "ufs") !=0) ||
332             (strncmp(mnt.mnt_mntopts, "ro,ignore",9) ==0)) 
333             continue; 
334
335         if (VCheckPartition(mnt.mnt_mountp, mnt.mnt_special) < 0 )
336             errors ++;
337     }
338
339    (void) fclose(mntfile);
340
341     return errors ;
342 }
343
344 #endif /* AFS_SUN5_ENV */
345 #if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)) || defined(AFS_HPUX_ENV)
346 int VAttachPartitions(void)
347 {
348     int errors = 0;
349     FILE *mfd;
350     struct mntent *mntent;
351     
352     if ((mfd = setmntent(MOUNTED, "r")) == NULL) {
353         Log("Problems in getting mount entries(setmntent)\n");
354         exit(-1);
355     }
356     while (mntent = getmntent(mfd)) {
357         if (!hasmntopt(mntent, MNTOPT_RW)) continue;
358         
359         if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0 )
360             errors ++;
361     }
362
363     endmntent(mfd);
364
365     return errors ;
366 }
367 #endif
368 #ifdef AFS_AIX_ENV
369 /*
370  * (This function was grabbed from df.c)
371  */
372 int
373 getmount(vmountpp)
374 register struct vmount  **vmountpp;     /* place to tell where buffer is */
375 {
376         int                     size;
377         register struct vmount  *vm;
378         int                     nmounts;
379
380         /* set initial size of mntctl buffer to a MAGIC NUMBER */
381         size = BUFSIZ;
382
383         /* try the operation until ok or a fatal error */
384         while (1) {
385                 if ((vm = (struct vmount *)malloc(size)) == NULL) {
386                         /* failed getting memory for mount status buf */
387                         perror("FATAL ERROR: get_stat malloc failed\n");
388                         exit(-1);
389                 }
390
391                 /*
392                  * perform the QUERY mntctl - if it returns > 0, that is the
393                  * number of vmount structures in the buffer.  If it returns
394                  * -1, an error occured.  If it returned 0, then look in
395                  * first word of buffer for needed size.
396                  */
397                 if ((nmounts = mntctl(MCTL_QUERY, size, (caddr_t)vm)) > 0) {
398                         /* OK, got it, now return */
399                         *vmountpp = vm;
400                         return(nmounts);
401
402                 } else if (nmounts == 0) {
403                         /* the buffer wasn't big enough .... */
404                         /* .... get required buffer size */
405                         size = *(int *)vm;
406                         free(vm);
407
408                 } else {
409                         /* some other kind of error occurred */
410                         free(vm);
411                         return(-1);
412                 }
413         }
414 }
415
416 int VAttachPartitions(void)
417 {
418     int errors = 0;
419     int nmounts;
420     struct vmount *vmountp;
421
422     if ((nmounts = getmount(&vmountp)) <= 0)    {   
423         Log("Problems in getting # of mount entries(getmount)\n");
424         exit(-1);
425     }
426     for (; nmounts; nmounts--,
427          vmountp = (struct vmount *)((int)vmountp + vmountp->vmt_length)) {
428         char *part = vmt2dataptr(vmountp, VMT_STUB);
429
430         if (vmountp->vmt_flags & (MNT_READONLY|MNT_REMOVABLE|MNT_REMOTE))
431             continue; /* Ignore any "special" partitions */
432
433 #ifdef AFS_AIX42_ENV
434         {
435             struct superblock fs;
436             /* The Log statements are non-sequiters in the SalvageLog and don't
437              * even appear in the VolserLog, so restrict them to the FileLog.
438              */
439             if (ReadSuper(&fs, vmt2dataptr(vmountp, VMT_OBJECT))<0) {
440                 if (programType == fileServer)
441                     Log("Can't read superblock for %s, ignoring it.\n", part);
442                 continue;
443             }
444             if (IsBigFilesFileSystem(&fs)) {
445                 if (programType == fileServer)
446                     Log("%s is a big files filesystem, ignoring it.\n", part);
447                 continue;
448             }
449         }
450 #endif
451
452         if (VCheckPartition(part, vmt2dataptr(vmountp, VMT_OBJECT)) < 0 )
453             errors ++;
454     }
455     return errors ;
456
457 }
458 #endif
459 #if defined(AFS_DUX40_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
460 int VAttachPartitions(void)
461 {
462     int errors = 0;
463     struct fstab *fsent;
464
465     if (setfsent() < 0) {
466         Log("Error listing filesystems.\n");
467         exit(-1);
468     }
469
470     while (fsent = getfsent()) {
471         if (strcmp(fsent->fs_type, "rw") != 0) continue;
472
473         if (VCheckPartition(fsent->fs_file, fsent->fs_spec) < 0 )
474             errors ++;
475     }
476     endfsent();
477     
478     return errors ;
479 }
480 #endif
481
482 #ifdef AFS_NT40_ENV
483 #include <string.h>
484 #include <sys/stat.h>
485 /* VValidVPTEntry
486  *
487  * validate names in vptab.
488  *
489  * Return value:
490  * 1 valid entry
491  * 0 invalid entry
492  */
493
494 int VValidVPTEntry(struct vptab *vpe)
495 {
496     int len = strlen(vpe->vp_name);
497     int i;
498
499     if (len < VICE_PREFIX_SIZE+1 || len > VICE_PREFIX_SIZE + 2)
500         return 0;
501     if (strncmp(vpe->vp_name, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE))
502         return 0;
503     
504     for (i=VICE_PREFIX_SIZE; i<len; i++) {
505         if (vpe->vp_name[i] < 'a' || vpe->vp_name[i] > 'z') {
506             Log("Invalid partition name %s in registry, ignoring it.\n",
507                 vpe->vp_name);
508             return 0;
509         }
510     }
511     if (len == VICE_PREFIX_SIZE + 2) {
512         i = (int)(vpe->vp_name[VICE_PREFIX_SIZE]-'a') * 26 +
513             (int)(vpe->vp_name[VICE_PREFIX_SIZE+1]-'a') ;
514         if (i>255) {
515             Log("Invalid partition name %s in registry, ignoring it.\n",
516                 vpe->vp_name);
517             return 0;
518         }
519     }
520
521     len = strlen(vpe->vp_dev);
522     if (len != 2 || vpe->vp_dev[1] != ':'  || vpe->vp_dev[0] < 'A' ||
523         vpe->vp_dev[0] > 'Z') {
524         Log("Invalid device name %s in registry, ignoring it.\n",
525             vpe->vp_dev);
526         return 0;
527     }
528
529     return 1;
530 }
531
532 int VCheckPartition(char *partName)
533 {
534     char volRoot[4];
535     char volFsType[64];
536     DWORD dwDummy;
537     int err;
538
539     /* partName is presumed to be of the form "X:" */
540     (void) sprintf(volRoot, "%c:\\", *partName);
541
542     if (!GetVolumeInformation(volRoot,    /* volume root directory */
543                               NULL,       /* volume name buffer */
544                               0,          /* volume name size */
545                               NULL,       /* volume serial number */
546                               &dwDummy,   /* max component length */
547                               &dwDummy,   /* file system flags */
548                               volFsType,  /* file system name */
549                               sizeof(volFsType))) {
550         err = GetLastError();
551         Log("VCheckPartition: Failed to get partition information for %s, ignoring it.\n",
552             partName);
553         return -1;
554     }
555
556     if (strcmp(volFsType, "NTFS")) {
557         Log("VCheckPartition: Partition %s is not an NTFS partition, ignoring it.\n", partName);
558         return -1;
559     }
560
561     return 0;
562 }
563
564
565 int VAttachPartitions(void)
566 {
567     struct DiskPartition *partP, *prevP, *nextP;
568     struct vpt_iter iter;
569     struct vptab entry;
570
571     if (vpt_Start(&iter)<0) {
572         Log("No partitions to attach.\n");
573         return 0;
574     }
575
576     while (0==vpt_NextEntry(&iter, &entry)) {
577         if (!VValidVPTEntry(&entry)) {
578             continue;
579         }
580
581         /* This test for duplicates relies on the fact that the method
582          * of storing the partition names in the NT registry means the same
583          * partition name will never appear twice in the list.
584          */
585         for (partP = DiskPartitionList; partP; partP = partP->next) {
586             if (*partP->devName == *entry.vp_dev) {
587                 Log("Same drive (%s) used for both partition %s and partition %s, ignoring both.\n", entry.vp_dev, partP->name, entry.vp_name);
588                 partP->flags = PART_DUPLICATE;
589                 break; /* Only one entry will ever be in this list. */
590             }
591         }
592         if (partP) continue; /* found a duplicate */
593
594         if (VCheckPartition(entry.vp_dev)<0)
595             continue;
596         /* This test allows for manually inserting the FORCESALVAGE flag
597          * and thereby invoking the salvager. scandisk obviously won't be
598          * doing this for us.
599          */
600         if (programType == fileServer) {
601             struct stat status;
602             char salvpath[MAXPATHLEN];
603             strcpy(salvpath, entry.vp_dev);
604             strcat(salvpath, "\\FORCESALVAGE");
605             if (stat(salvpath, &status) == 0) {
606                 Log("VAttachPartitions: Found %s; aborting\n", salvpath);
607                 exit(1);
608             }
609         }
610         VInitPartition(entry.vp_name, entry.vp_dev, *entry.vp_dev - 'A');
611     }
612     vpt_Finish(&iter);
613
614     /* Run through partition list and clear out the dupes. */
615     prevP = nextP = NULL;
616     for (partP = DiskPartitionList; partP; partP = nextP) {
617         nextP = partP->next;
618         if (partP->flags == PART_DUPLICATE) {
619             if (prevP)
620                 prevP->next = partP->next;
621             else
622                 DiskPartitionList = partP->next;
623             free(partP);
624         }
625         else
626             prevP = partP;
627     }
628
629     return 0;
630 }
631 #endif
632
633 #ifdef AFS_LINUX22_ENV
634 int VAttachPartitions(void)
635 {
636     int errors = 0;
637     FILE *mfd;
638     struct mntent *mntent;
639     
640     if ((mfd = setmntent("/proc/mounts", "r")) == NULL) {
641         if ((mfd = setmntent("/etc/mtab", "r")) == NULL) {
642             Log("Problems in getting mount entries(setmntent)\n");
643             exit(-1);
644         }
645     }
646     while (mntent = getmntent(mfd)) {
647         if (VCheckPartition(mntent->mnt_dir, mntent->mnt_fsname) < 0 )
648             errors ++;
649     }
650     endmntent(mfd);
651
652     return errors ;
653 }
654 #endif /* AFS_LINUX22_ENV */
655
656 /* This routine is to be called whenever the actual name of the partition
657  * is required. The canonical name is still in part->name.
658  */
659 char * VPartitionPath(struct DiskPartition *part)
660 {
661 #ifdef AFS_NT40_ENV
662     return part->devName;
663 #else
664     return part->name;
665 #endif    
666 }
667
668 /* get partition structure, abortp tells us if we should abort on failure */
669 struct DiskPartition *VGetPartition_r(char *name, int abortp)
670 {
671     register struct DiskPartition *dp;
672     for (dp = DiskPartitionList; dp; dp = dp->next) {
673         if (strcmp(dp->name, name) == 0)
674             break;
675     }
676     if (abortp)
677         assert(dp != NULL);
678     return dp;
679 }
680
681 struct DiskPartition *VGetPartition(char *name, int abortp)
682 {
683     struct DiskPartition *retVal;
684     VOL_LOCK
685     retVal = VGetPartition_r(name, abortp);
686     VOL_UNLOCK
687     return retVal;
688 }
689
690 #ifdef AFS_NT40_ENV
691 void VSetPartitionDiskUsage_r(register struct DiskPartition *dp)
692 {
693     ULARGE_INTEGER free_user, total, free_total;
694     int ufree, tot, tfree;
695     
696     if (!GetDiskFreeSpaceEx(VPartitionPath(dp), &free_user, &total,
697                             &free_total)) {
698         printf("Failed to get disk space info for %s, error = %d\n",
699                dp->name, GetLastError());
700         return;
701     }
702
703     /* Convert to 1K units. */
704     ufree = (int) Int64ShraMod32(free_user.QuadPart, 10);
705     tot = (int) Int64ShraMod32(total.QuadPart, 10);
706     tfree = (int) Int64ShraMod32(free_total.QuadPart, 10);
707
708     dp->minFree = tfree - ufree; /* only used in VPrintDiskStats_r */
709     dp->totalUsable = tot;
710     dp->free = tfree;
711 }
712
713 #else
714 void VSetPartitionDiskUsage_r(register struct DiskPartition *dp)
715 {
716     extern int errno;
717     int fd, totalblks, free, used, availblks, bsize, code;
718     int reserved;
719 #if AFS_HAVE_STATVFS
720     struct statvfs statbuf;
721 #else
722     struct statfs statbuf;
723 #endif
724
725     if (dp->flags & PART_DONTUPDATE)
726         return;
727     /* Note:  we don't bother syncing because it's only an estimate, update
728        is syncing every 30 seconds anyway, we only have to keep the disk
729        approximately 10% from full--you just can't get the stuff in from
730        the net fast enough to worry */
731 #if AFS_HAVE_STATVFS
732     code = statvfs(dp->name, &statbuf);
733 #else
734     code = statfs(dp->name, &statbuf);
735 #endif
736     if (code < 0) {
737         Log("statfs of %s failed in VSetPartitionDiskUsage (errno = %d)\n", dp->name, errno);
738         return;
739     }
740     if (statbuf.f_blocks == -1) {   /* Undefined; skip stats.. */   
741         Log("statfs of %s failed in VSetPartitionDiskUsage\n", dp->name);
742         return;
743     }
744     totalblks = statbuf.f_blocks;
745     free = statbuf.f_bfree;
746     reserved = free - statbuf.f_bavail;
747 #if AFS_HAVE_STATVFS
748     bsize = statbuf.f_frsize;
749 #else
750     bsize = statbuf.f_bsize;
751 #endif
752     availblks = totalblks - reserved;
753     dp->f_files = statbuf.f_files;      /* max # of files in partition */
754
755     /* Now free and totalblks are in fragment units, but we want them in
756      * 1K units.
757      */
758     if (bsize >= 1024) {
759         free *= (bsize/1024);
760         totalblks *= (bsize / 1024);
761         availblks *= (bsize / 1024 );
762         reserved *= (bsize / 1024 );
763     }
764     else {
765         free /= (1024/bsize);
766         totalblks /= (1024/bsize);
767         availblks /= (1024/bsize);
768         reserved /= (1024/bsize);
769     }
770     /* now compute remaining figures */
771     used = totalblks - free;
772
773     dp->minFree = reserved; /* only used in VPrintDiskStats_r */
774     dp->totalUsable = availblks;
775     dp->free = availblks - used; /* this is exactly f_bavail */
776 }
777 #endif /* AFS_NT40_ENV */
778
779 void VSetPartitionDiskUsage(register struct DiskPartition *dp)
780 {
781     VOL_LOCK
782     VSetPartitionDiskUsage_r(dp);
783     VOL_UNLOCK
784 }
785
786 void VResetDiskUsage_r(void)
787 {
788     struct DiskPartition *dp;
789     for (dp = DiskPartitionList; dp; dp = dp->next) {
790         VSetPartitionDiskUsage_r(dp);
791 #ifndef AFS_PTHREAD_ENV
792         IOMGR_Poll();
793 #endif /* !AFS_PTHREAD_ENV */
794     }
795 }
796
797 void VResetDiskUsage(void)
798 {
799     VOL_LOCK
800     VResetDiskUsage_r();
801     VOL_UNLOCK
802 }
803
804 void VAdjustDiskUsage_r(Error *ec, Volume *vp, afs_int32 blocks, afs_int32 checkBlocks)
805 {
806     afs_int32 rem, minavail;
807     *ec = 0;
808     /* why blocks instead of checkBlocks in the check below?  Otherwise, any check
809        for less than BlocksSpare would skip the error-checking path, and we
810        could grow existing files forever, not just for another BlocksSpare
811        blocks. */
812     if (blocks > 0) {
813 #ifdef  AFS_AIX32_ENV
814         if ((rem = vp->partition->free - checkBlocks) < 
815             (minavail = (vp->partition->totalUsable * aixlow_water) / 100))
816 #else
817         if (vp->partition->free - checkBlocks < 0)
818 #endif
819             *ec = VDISKFULL;
820         else if (V_maxquota(vp) && V_diskused(vp) + checkBlocks > V_maxquota(vp))
821             *ec = VOVERQUOTA;
822     }    
823     vp->partition->free -= blocks;
824     V_diskused(vp) += blocks;
825 }
826
827 void VAdjustDiskUsage(Error *ec, Volume *vp, afs_int32 blocks, afs_int32 checkBlocks)
828 {
829     VOL_LOCK
830     VAdjustDiskUsage_r(ec, vp, blocks, checkBlocks);
831     VOL_UNLOCK
832 }
833
834 int VDiskUsage_r(Volume *vp, afs_int32 blocks)
835 {
836     afs_int32 rem, minavail;
837     if (blocks > 0) {
838 #ifdef  AFS_AIX32_ENV
839         if ((rem = vp->partition->free - blocks) < 
840             (minavail = (vp->partition->totalUsable * aixlow_water) / 100))
841 #else
842         if (vp->partition->free - blocks < 0)
843 #endif
844             return(VDISKFULL);
845     }    
846     vp->partition->free -= blocks;
847     return 0;
848 }
849
850 int VDiskUsage(Volume *vp, afs_int32 blocks)
851 {
852     int retVal;
853     VOL_LOCK
854     retVal = VDiskUsage_r(vp, blocks);
855     VOL_UNLOCK
856     return retVal;
857 }
858
859 void VPrintDiskStats_r(void)
860 {
861     struct DiskPartition *dp;
862     for (dp = DiskPartitionList; dp; dp = dp->next) {
863         Log("Partition %s: %d available 1K blocks (minfree=%d), ",
864             dp->name, dp->totalUsable, dp->minFree);
865         if (dp->free < 0)
866             Log("overallocated by %d blocks\n", -dp->free);
867         else
868             Log("%d free blocks\n", dp->free);
869     }
870 }
871
872 void VPrintDiskStats(void)
873 {
874     VOL_LOCK
875     VPrintDiskStats_r();
876     VOL_UNLOCK
877 }
878
879 #ifdef AFS_NT40_ENV
880 /* Need a separate lock file on NT, since NT only has mandatory file locks. */
881 #define LOCKFILE "LOCKFILE"
882 void VLockPartition_r(char *name)
883 {
884     struct DiskPartition *dp = VGetPartition_r(name, 0);
885     OVERLAPPED lap;
886     
887     if (!dp) return;
888     if (dp->lock_fd == -1) {
889         char path[64];
890         int rc;
891         (void) sprintf(path, "%s\\%s", VPartitionPath(dp), LOCKFILE);
892         dp->lock_fd = (int)CreateFile(path, GENERIC_WRITE,
893                                  FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
894                                  CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
895         assert (dp->lock_fd != (int)INVALID_HANDLE_VALUE);
896
897         memset((char*)&lap, 0, sizeof(lap));
898         rc = LockFileEx((HANDLE)dp->lock_fd, LOCKFILE_EXCLUSIVE_LOCK,
899                         0, 1, 0, &lap);
900         assert(rc);
901     }
902 }
903
904 void VUnlockPartition_r(char *name)
905 {
906     register struct DiskPartition *dp = VGetPartition_r(name, 0);
907     OVERLAPPED lap;
908
909     if (!dp) return;    /* no partition, will fail later */
910     memset((char*)&lap, 0, sizeof(lap));
911
912     UnlockFileEx((HANDLE)dp->lock_fd, 0, 1, 0, &lap);
913     CloseHandle((HANDLE)dp->lock_fd);
914     dp->lock_fd = -1;
915 }
916 #else /* AFS_NT40_ENV */
917
918 #if defined(AFS_HPUX_ENV)
919 #define BITS_PER_CHAR   (8)
920 #define BITS(type)      (sizeof(type) * BITS_PER_CHAR)
921
922 #define LOCKRDONLY_OFFSET       ((PRIV_LOCKRDONLY - 1) / BITS(int))
923 #endif /* defined(AFS_HPUX_ENV) */
924
925 void VLockPartition_r(char *name)
926 {
927     register struct DiskPartition *dp = VGetPartition_r(name, 0);
928     char *partitionName;
929     int retries, code;
930     struct timeval pausing;
931 #if defined(AFS_HPUX_ENV)
932     int                 lockfRtn;
933     struct privgrp_map  privGrpList[PRIV_MAXGRPS];
934     unsigned int        *globalMask;
935     int                 globalMaskIndex;
936 #endif /* defined(AFS_HPUX_ENV) */
937     
938     if (!dp) return;    /* no partition, will fail later */
939     if (dp->lock_fd != -1) return;
940
941 #if    defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
942     partitionName = dp->devName;
943     code = O_RDWR;
944 #else
945     partitionName = dp->name;
946     code = O_RDONLY;
947 #endif
948
949     for (retries=25; retries; retries--) {
950         dp->lock_fd = open(partitionName, code);
951         if (dp->lock_fd != -1) break;
952         pausing.tv_sec = 0;
953         pausing.tv_usec = 500000;
954         select(0, NULL, NULL, NULL, &pausing);
955     }
956     assert(retries != 0);
957
958 #if defined (AFS_HPUX_ENV)
959
960         assert(getprivgrp(privGrpList) == 0);
961
962         /*
963          * In general, it will difficult and time-consuming ,if not impossible,
964          * to try to find the privgroup to which this process belongs that has the
965          * smallest membership, to minimise the security hole.  So, we use the privgrp
966          * to which everybody belongs.
967          */
968         /* first, we have to find the global mask */
969         for (globalMaskIndex = 0; globalMaskIndex < PRIV_MAXGRPS;
970              globalMaskIndex++) {
971           if (privGrpList[globalMaskIndex].priv_groupno == PRIV_GLOBAL) {
972             globalMask = &(privGrpList[globalMaskIndex].
973                            priv_mask[LOCKRDONLY_OFFSET]);
974             break;
975           }
976         }
977
978         if (((*globalMask) & privmask(PRIV_LOCKRDONLY)) == 0) {
979           /* allow everybody to set a lock on a read-only file descriptor */
980           (*globalMask) |= privmask(PRIV_LOCKRDONLY);
981           assert(setprivgrp(PRIV_GLOBAL,
982                             privGrpList[globalMaskIndex].priv_mask) == 0);
983
984           lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
985
986           /* remove the privilege granted to everybody to lock a read-only fd */
987           (*globalMask) &= ~(privmask(PRIV_LOCKRDONLY));
988           assert(setprivgrp(PRIV_GLOBAL,
989                             privGrpList[globalMaskIndex].priv_mask) == 0);
990         }
991         else {
992           /* in this case, we should be able to do this with impunity, anyway */
993           lockfRtn = lockf(dp->lock_fd, F_LOCK, 0);
994         }
995         
996         assert (lockfRtn != -1); 
997 #else
998 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
999         assert (lockf(dp->lock_fd, F_LOCK, 0) != -1); 
1000 #else
1001         assert (flock(dp->lock_fd, LOCK_EX) == 0);
1002 #endif  /* defined(AFS_AIX_ENV) */
1003 #endif
1004 }
1005
1006 void VUnlockPartition_r(char *name)
1007 {
1008     register struct DiskPartition *dp = VGetPartition_r(name, 0);
1009     if (!dp) return;    /* no partition, will fail later */
1010     close(dp->lock_fd);
1011     dp->lock_fd = -1;
1012 }
1013
1014 #endif /* AFS_NT40_ENV */
1015
1016 void VLockPartition(char *name)
1017 {
1018     VOL_LOCK
1019     VLockPartition_r(name);
1020     VOL_UNLOCK
1021 }
1022
1023 void VUnlockPartition(char *name)
1024 {
1025     VOL_LOCK
1026     VUnlockPartition_r(name);
1027     VOL_UNLOCK
1028 }