2 * Copyright 2000, International Business Machines Corporation and others.
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
9 * Portions Copyright (c) 2003 Apple Computer, Inc.
10 * Portions Copyright (c) 2006 Sine Nomine Associates
17 Institution: The Information Technology Center, Carnegie-Mellon University
21 #include <afsconfig.h>
22 #include <afs/param.h>
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>
41 #if !defined(AFS_SGI_ENV)
43 #include <sys/mount.h>
45 #else /* AFS_OSF_ENV */
46 #ifdef AFS_VFSINCL_ENV
49 #include <sys/fs/ufs_fs.h>
51 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
52 #include <ufs/ufs/dinode.h>
53 #include <ufs/ffs/fs.h>
58 #else /* AFS_VFSINCL_ENV */
59 #if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX22_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
62 #endif /* AFS_VFSINCL_ENV */
63 #endif /* AFS_OSF_ENV */
67 #include <sys/lockf.h>
71 #include <checklist.h>
73 #if defined(AFS_SUN_ENV)
80 #include <sys/mnttab.h>
81 #include <sys/mntent.h>
83 #ifdef AFS_LINUX22_ENV
85 #include <sys/statfs.h>
92 #endif /* AFS_SGI_ENV */
93 #endif /* AFS_NT40_ENV */
94 #if defined(AFS_SGI_ENV)
100 #ifdef AFS_PTHREAD_ENV
101 # include <opr/lock.h>
103 #include <afs/afsint.h>
104 #include <rx/rx_queue.h>
106 #include <afs/errors.h>
109 #include <afs/afssyscalls.h>
116 #include "namei_ops.h"
117 #endif /* AFS_NT40_ENV */
118 #endif /* AFS_NAMEI_ENV */
121 #include "partition.h"
123 #if defined(AFS_HPUX_ENV)
124 #include <sys/privgrp.h>
125 #endif /* defined(AFS_HPUX_ENV) */
128 #include <jfs/filsys.h>
132 extern int VValidVPTEntry(struct vptab
*vptp
);
135 int aixlow_water
= 8; /* default 8% */
136 struct DiskPartition64
*DiskPartitionList
;
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"
143 static struct DiskPartition64
*DiskPartitionTable
[VOLMAXPARTS
+1];
145 static struct DiskPartition64
* VLookupPartition_r(char * path
);
146 static void AddPartitionToTable_r(struct DiskPartition64
*);
147 #endif /* AFS_DEMAND_ATTACH_FS */
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
155 #include <afs/xfsattrs.h>
157 VerifyXFSInodeSize(char *part
, char *fstype
)
160 int length
= SIZEOF_XFS_ATTR_T
;
165 if (strcmp("xfs", fstype
))
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)) {
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");
181 (void)attr_remove(part
, AFS_XFS_ATTR
, ATTR_ROOT
);
185 #endif /* AFS_SGI_XFS_IOPS_ENV */
188 VInitPartitionPackage(void)
190 #ifdef AFS_DEMAND_ATTACH_FS
191 memset(&DiskPartitionTable
, 0, sizeof(DiskPartitionTable
));
192 #endif /* AFS_DEMAND_ATTACH_FS */
197 VInitPartition_r(char *path
, char *devname
, Device dev
)
199 struct DiskPartition64
*dp
, *op
;
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
) {
210 DiskPartitionList
= dp
;
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
;
225 dp
->devName
= strdup(devname
);
228 dp
->lock_fd
= INVALID_FD
;
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
));
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;
243 char lockpath
[MAXPATHLEN
+1];
244 snprintf(lockpath
, MAXPATHLEN
, "%s/" AFS_PARTLOCK_FILE
, dp
->name
);
245 lockpath
[MAXPATHLEN
] = '\0';
246 VLockFileInit(&dp
->headerLockFile
, lockpath
);
248 snprintf(lockpath
, MAXPATHLEN
, "%s/" AFS_VOLUMELOCK_FILE
, dp
->name
);
249 lockpath
[MAXPATHLEN
] = '\0';
250 VLockFileInit(&dp
->volLockFile
, lockpath
);
252 VDiskLockInit(&dp
->headerLock
, &dp
->headerLockFile
, 1);
253 #endif /* AFS_DEMAND_ATTACH_FS */
257 VInitPartition(char *path
, char *devname
, Device dev
)
260 VInitPartition_r(path
, devname
, dev
);
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
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.
274 * No specific user space file system checks, since we don't know what
275 * is being used for vice partitions.
277 * Use partition name as devname.
280 VCheckPartition(char *part
, char *devname
, int logging
)
282 struct afs_stat_st status
;
283 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
284 char AFSIDatPath
[MAXPATHLEN
];
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
)) {
292 if (afs_stat(part
, &status
) < 0) {
293 Log("VInitVnodes: Couldn't find file system %s; ignored\n", part
);
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
);
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
);
317 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
318 strcpy(AFSIDatPath
, part
);
319 strcat(AFSIDatPath
, "/AFSIDat");
321 if (afs_stat(AFSIDatPath
, &status
) < 0) {
325 dirp
= opendir(part
);
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
);
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
);
342 #ifdef AFS_SGI_XFS_IOPS_ENV
343 if (VerifyXFSInodeSize(part
, status
.st_fstype
) < 0)
346 #endif /* AFS_NAMEI_ENV */
347 #endif /* !AFS_LINUX20_ENV && !AFS_NT40_ENV */
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
);
356 VInitPartition(part
, devname
, status
.st_dev
);
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
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.
373 VIsAlwaysAttach(char *part
, int *awouldattach
)
376 struct afs_stat_st st
;
379 #endif /* AFS_NAMEI_ENV */
386 if (strncmp(part
, VICE_PARTITION_PREFIX
, VICE_PREFIX_SIZE
))
393 strncpy(checkfile
, part
, 100);
394 strcat(checkfile
, OS_DIRSEP
);
395 strcat(checkfile
, VICE_ALWAYSATTACH_FILE
);
397 ret
= afs_stat(checkfile
, &st
);
398 return (ret
< 0) ? 0 : 1;
399 #else /* AFS_NAMEI_ENV */
401 #endif /* AFS_NAMEI_ENV */
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.
409 VIsNeverAttach(char *part
)
411 struct afs_stat_st st
;
415 if (strncmp(part
, VICE_PARTITION_PREFIX
, VICE_PREFIX_SIZE
))
418 strncpy(checkfile
, part
, 100);
419 strcat(checkfile
, OS_DIRSEP
);
420 strcat(checkfile
, VICE_NEVERATTACH_FILE
);
422 ret
= afs_stat(checkfile
, &st
);
423 return (ret
< 0) ? 0 : 1;
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.
432 VAttachPartitions2(void)
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';
446 /* Only keep track of "/vicepx" partitions since automounter
448 if (VIsAlwaysAttach(pname
, &wouldattach
)) {
449 VCheckPartition(pname
, "", 0);
451 struct afs_stat_st st
;
452 if (wouldattach
&& VGetPartition(pname
, 0) == NULL
&&
453 afs_stat(pname
, &st
) == 0 && S_ISDIR(st
.st_mode
)) {
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
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. */
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",
475 #endif /* AFS_NAMEI_ENV */
477 #endif /* AFS_NT40_ENV */
481 VAttachPartitions(void)
487 if (!(mntfile
= afs_fopen(MNTTAB
, "r"))) {
488 Log("Can't open %s\n", MNTTAB
);
492 while (!getmntent(mntfile
, &mnt
)) {
494 /* Ignore non ufs or non read/write partitions */
495 /* but allow zfs too if we're in the NAMEI environment */
498 (((strcmp(mnt
.mnt_fstype
, "ufs") &&
499 strcmp(mnt
.mnt_fstype
, "zfs"))))
501 (strcmp(mnt
.mnt_fstype
, "ufs") != 0)
503 || (strncmp(mnt
.mnt_mntopts
, "ro,ignore", 9) == 0))
506 /* Skip this Partition? */
507 if (VIsNeverAttach(mnt
.mnt_mountp
))
510 /* If we're going to always attach this partition, do it later. */
511 if (VIsAlwaysAttach(mnt
.mnt_mountp
, NULL
))
514 #ifndef AFS_NAMEI_ENV
515 if (hasmntopt(&mnt
, "logging") != NULL
) {
518 #endif /* !AFS_NAMEI_ENV */
520 if (VCheckPartition(mnt
.mnt_mountp
, mnt
.mnt_special
, logging
) < 0)
524 (void)fclose(mntfile
);
526 /* Process the always-attach partitions, if any. */
527 VAttachPartitions2();
532 #endif /* AFS_SUN5_ENV */
533 #if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)) || defined(AFS_HPUX_ENV)
535 VAttachPartitions(void)
539 struct mntent
*mntent
;
541 if ((mfd
= setmntent(MOUNTED
, "r")) == NULL
) {
542 Log("Problems in getting mount entries(setmntent)\n");
545 while (mntent
= getmntent(mfd
)) {
546 if (!hasmntopt(mntent
, MNTOPT_RW
))
549 /* Skip this Partition? */
550 if (VIsNeverAttach(mntent
->mnt_dir
))
553 /* If we're going to always attach this partition, do it later. */
554 if (VIsAlwaysAttach(mntent
->mnt_dir
, NULL
))
557 if (VCheckPartition(mntent
->mnt_dir
, mntent
->mnt_fsname
, 0) < 0)
563 /* Process the always-attach partitions, if any. */
564 VAttachPartitions2();
571 * (This function was grabbed from df.c)
574 getmount(struct vmount
**vmountpp
)
580 /* set initial size of mntctl buffer to a MAGIC NUMBER */
583 /* try the operation until ok or a fatal error */
585 if ((vm
= malloc(size
)) == NULL
) {
586 /* failed getting memory for mount status buf */
587 perror("FATAL ERROR: get_stat malloc failed\n");
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.
597 if ((nmounts
= mntctl(MCTL_QUERY
, size
, (caddr_t
) vm
)) > 0) {
598 /* OK, got it, now return */
602 } else if (nmounts
== 0) {
603 /* the buffer wasn't big enough .... */
604 /* .... get required buffer size */
609 /* some other kind of error occurred */
617 VAttachPartitions(void)
621 struct vmount
*vmountp
;
623 if ((nmounts
= getmount(&vmountp
)) <= 0) {
624 Log("Problems in getting # of mount entries(getmount)\n");
629 (struct vmount
*)((int)vmountp
+ vmountp
->vmt_length
)) {
630 char *part
= vmt2dataptr(vmountp
, VMT_STUB
);
632 if (vmountp
->vmt_flags
& (MNT_READONLY
| MNT_REMOVABLE
| MNT_REMOTE
))
633 continue; /* Ignore any "special" partitions */
636 #ifndef AFS_NAMEI_ENV
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.
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
);
647 if (IsBigFilesFileSystem(&fs
)) {
648 if (programType
== fileServer
)
649 Log("%s is a big files filesystem, ignoring it.\n", part
);
656 /* Skip this Partition? */
657 if (VIsNeverAttach(part
))
660 /* If we're going to always attach this partition, do it later. */
661 if (VIsAlwaysAttach(part
, NULL
))
664 if (VCheckPartition(part
, vmt2dataptr(vmountp
, VMT_OBJECT
), 0) < 0)
668 /* Process the always-attach partitions, if any. */
669 VAttachPartitions2();
674 #if defined(AFS_DUX40_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
676 VAttachPartitions(void)
681 if (setfsent() < 0) {
682 Log("Error listing filesystems.\n");
686 while ((fsent
= getfsent())) {
687 if (strcmp(fsent
->fs_type
, "rw") != 0)
690 /* Skip this Partition? */
691 if (VIsNeverAttach(fsent
->fs_file
))
694 /* If we're going to always attach this partition, do it later. */
695 if (VIsAlwaysAttach(fsent
->fs_file
, NULL
))
698 if (VCheckPartition(fsent
->fs_file
, fsent
->fs_spec
, 0) < 0)
703 /* Process the always-attach partitions, if any. */
704 VAttachPartitions2();
713 * validate names in vptab.
721 VValidVPTEntry(struct vptab
*vpe
)
723 int len
= strlen(vpe
->vp_name
);
726 if (len
< VICE_PREFIX_SIZE
+ 1 || len
> VICE_PREFIX_SIZE
+ 2)
728 if (strncmp(vpe
->vp_name
, VICE_PARTITION_PREFIX
, VICE_PREFIX_SIZE
))
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",
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');
742 Log("Invalid partition name %s in registry, ignoring it.\n",
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",
760 VCheckPartition(char *partName
)
767 /* partName is presumed to be of the form "X:" */
768 (void)sprintf(volRoot
, "%c:\\", *partName
);
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
);
783 if (strcmp(volFsType
, "NTFS")) {
784 Log("VCheckPartition: Partition %s is not an NTFS partition, ignoring it.\n", partName
);
793 VAttachPartitions(void)
795 struct DiskPartition64
*partP
, *prevP
, *nextP
;
796 struct vpt_iter iter
;
799 if (vpt_Start(&iter
) < 0) {
800 Log("No partitions to attach.\n");
804 while (0 == vpt_NextEntry(&iter
, &entry
)) {
805 if (!VValidVPTEntry(&entry
)) {
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.
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. */
821 continue; /* found a duplicate */
823 if (VCheckPartition(entry
.vp_dev
) < 0)
825 /* This test allows for manually inserting the FORCESALVAGE flag
826 * and thereby invoking the salvager. scandisk obviously won't be
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
);
839 VInitPartition(entry
.vp_name
, entry
.vp_dev
, *entry
.vp_dev
- 'A');
843 /* Run through partition list and clear out the dupes. */
844 prevP
= nextP
= NULL
;
845 for (partP
= DiskPartitionList
; partP
; partP
= nextP
) {
847 if (partP
->flags
== PART_DUPLICATE
) {
849 prevP
->next
= partP
->next
;
851 DiskPartitionList
= partP
->next
;
861 #ifdef AFS_LINUX22_ENV
863 VAttachPartitions(void)
867 struct mntent
*mntent
;
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");
875 while ((mntent
= getmntent(mfd
))) {
876 /* Skip this Partition? */
877 if (VIsNeverAttach(mntent
->mnt_dir
))
880 /* If we're going to always attach this partition, do it later. */
881 if (VIsAlwaysAttach(mntent
->mnt_dir
, NULL
))
884 if (VCheckPartition(mntent
->mnt_dir
, mntent
->mnt_fsname
, 0) < 0)
889 /* Process the always-attach partitions, if any. */
890 VAttachPartitions2();
894 #endif /* AFS_LINUX22_ENV */
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.
900 VPartitionPath(struct DiskPartition64
*part
)
903 return part
->devName
;
909 /* get partition structure, abortp tells us if we should abort on failure */
910 struct DiskPartition64
*
911 VGetPartition_r(char *name
, int abortp
)
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)
921 #endif /* AFS_DEMAND_ATTACH_FS */
923 opr_Assert(dp
!= NULL
);
927 struct DiskPartition64
*
928 VGetPartition(char *name
, int abortp
)
930 struct DiskPartition64
*retVal
;
932 retVal
= VGetPartition_r(name
, abortp
);
939 VSetPartitionDiskUsage_r(struct DiskPartition64
*dp
)
941 ULARGE_INTEGER free_user
, total
, free_total
;
942 int ufree
, tot
, tfree
;
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
,
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);
956 dp
->minFree
= tfree
- ufree
; /* only used in VPrintDiskStats_r */
957 dp
->totalUsable
= tot
;
963 VSetPartitionDiskUsage_r(struct DiskPartition64
*dp
)
966 afs_int64 totalblks
, free
, used
, availblks
;
969 struct afs_statvfs statbuf
;
971 struct afs_statfs statbuf
;
974 if (dp
->flags
& PART_DONTUPDATE
)
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 */
981 code
= afs_statvfs(dp
->name
, &statbuf
);
983 code
= afs_statfs(dp
->name
, &statbuf
);
986 Log("statfs of %s failed in VSetPartitionDiskUsage (errno = %d)\n",
990 if (statbuf
.f_blocks
== -1) { /* Undefined; skip stats.. */
991 Log("statfs of %s failed in VSetPartitionDiskUsage\n", dp
->name
);
994 totalblks
= statbuf
.f_blocks
;
995 free
= statbuf
.f_bfree
;
996 reserved
= free
- statbuf
.f_bavail
;
998 bsize
= statbuf
.f_frsize
;
1000 bsize
= statbuf
.f_bsize
;
1002 availblks
= totalblks
- reserved
;
1003 dp
->f_files
= statbuf
.f_files
; /* max # of files in partition */
1005 /* Now free and totalblks are in fragment units, but we want them in
1008 if (bsize
>= 1024) {
1009 free
*= (bsize
/ 1024);
1010 totalblks
*= (bsize
/ 1024);
1011 availblks
*= (bsize
/ 1024);
1012 reserved
*= (bsize
/ 1024);
1014 free
/= (1024 / bsize
);
1015 totalblks
/= (1024 / bsize
);
1016 availblks
/= (1024 / bsize
);
1017 reserved
/= (1024 / bsize
);
1019 /* now compute remaining figures */
1020 used
= totalblks
- free
;
1022 dp
->minFree
= reserved
; /* only used in VPrintDiskStats_r */
1023 dp
->totalUsable
= availblks
;
1024 dp
->free
= availblks
- used
; /* this is exactly f_bavail */
1026 #endif /* AFS_NT40_ENV */
1029 VSetPartitionDiskUsage(struct DiskPartition64
*dp
)
1032 VSetPartitionDiskUsage_r(dp
);
1037 VResetDiskUsage_r(void)
1039 struct DiskPartition64
*dp
;
1040 for (dp
= DiskPartitionList
; dp
; dp
= dp
->next
) {
1041 VSetPartitionDiskUsage_r(dp
);
1042 #ifndef AFS_PTHREAD_ENV
1044 #endif /* !AFS_PTHREAD_ENV */
1049 VResetDiskUsage(void)
1052 VResetDiskUsage_r();
1057 VAdjustDiskUsage_r(Error
* ec
, Volume
* vp
, afs_sfsize_t blocks
,
1058 afs_sfsize_t checkBlocks
)
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
1066 #ifdef AFS_AIX32_ENV
1067 afs_int32 rem
, minavail
;
1069 if ((rem
= vp
->partition
->free
- checkBlocks
) < (minavail
=
1075 if (vp
->partition
->free
- checkBlocks
< 0)
1078 else if (V_maxquota(vp
)
1079 && V_diskused(vp
) + checkBlocks
> V_maxquota(vp
))
1082 vp
->partition
->free
-= blocks
;
1083 V_diskused(vp
) += blocks
;
1087 VAdjustDiskUsage(Error
* ec
, Volume
* vp
, afs_sfsize_t blocks
,
1088 afs_sfsize_t checkBlocks
)
1091 VAdjustDiskUsage_r(ec
, vp
, blocks
, checkBlocks
);
1096 VDiskUsage_r(Volume
* vp
, afs_sfsize_t blocks
)
1099 #ifdef AFS_AIX32_ENV
1100 afs_int32 rem
, minavail
;
1102 if ((rem
= vp
->partition
->free
- blocks
) < (minavail
=
1105 aixlow_water
) / 100))
1107 if (vp
->partition
->free
- blocks
< 0)
1111 vp
->partition
->free
-= blocks
;
1116 VDiskUsage(Volume
* vp
, afs_sfsize_t blocks
)
1120 retVal
= VDiskUsage_r(vp
, blocks
);
1126 VPrintDiskStats_r(void)
1128 struct DiskPartition64
*dp
;
1129 for (dp
= DiskPartitionList
; dp
; dp
= dp
->next
) {
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
);
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
);
1145 VPrintDiskStats(void)
1148 VPrintDiskStats_r();
1153 /* Need a separate lock file on NT, since NT only has mandatory file locks. */
1154 #define LOCKFILE "LOCKFILE"
1156 VLockPartition_r(char *name
)
1158 struct DiskPartition64
*dp
= VGetPartition_r(name
, 0);
1163 if (dp
->lock_fd
== INVALID_FD
) {
1166 (void)sprintf(path
, "%s\\%s", VPartitionPath(dp
), LOCKFILE
);
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
);
1173 memset(&lap
, 0, sizeof(lap
));
1174 rc
= LockFileEx((HANDLE
) dp
->lock_fd
, LOCKFILE_EXCLUSIVE_LOCK
, 0, 1,
1181 VUnlockPartition_r(char *name
)
1183 struct DiskPartition64
*dp
= VGetPartition_r(name
, 0);
1187 return; /* no partition, will fail later */
1188 memset(&lap
, 0, sizeof(lap
));
1190 UnlockFileEx((HANDLE
) dp
->lock_fd
, 0, 1, 0, &lap
);
1191 CloseHandle((HANDLE
) dp
->lock_fd
);
1192 dp
->lock_fd
= INVALID_FD
;
1194 #else /* AFS_NT40_ENV */
1196 #if defined(AFS_HPUX_ENV)
1197 #define BITS_PER_CHAR (8)
1198 #define BITS(type) (sizeof(type) * BITS_PER_CHAR)
1200 #define LOCKRDONLY_OFFSET ((PRIV_LOCKRDONLY - 1) / BITS(int))
1201 #endif /* defined(AFS_HPUX_ENV) */
1204 VLockPartition_r(char *name
)
1206 struct DiskPartition64
*dp
= VGetPartition_r(name
, 0);
1207 char *partitionName
;
1209 struct timeval pausing
;
1210 #if defined(AFS_HPUX_ENV)
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];
1223 sprintf((char *)&LockFileName
, "%s/AFSINODE_FSLock", name
);
1224 partitionName
= (char *)&LockFileName
;
1229 return; /* no partition, will fail later */
1230 if (dp
->lock_fd
!= INVALID_FD
)
1233 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
1234 #if !defined(AFS_AIX42_ENV) || !defined(AFS_NAMEI_ENV)
1235 partitionName
= dp
->devName
;
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
;
1243 partitionName
= dp
->name
;
1247 for (retries
= 25; retries
; retries
--) {
1249 dp
->lock_fd
= afs_open(partitionName
, code
, 0644);
1251 dp
->lock_fd
= afs_open(partitionName
, code
);
1253 if (dp
->lock_fd
!= INVALID_FD
)
1255 if (errno
== ENOENT
)
1258 pausing
.tv_usec
= 500000;
1259 select(0, NULL
, NULL
, NULL
, &pausing
);
1261 opr_Assert(retries
!= 0);
1263 #if defined (AFS_HPUX_ENV)
1265 opr_Verify(getprivgrp(privGrpList
) == 0);
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.
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
) {
1278 &(privGrpList
[globalMaskIndex
].priv_mask
[LOCKRDONLY_OFFSET
]);
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);
1289 lockfRtn
= lockf(dp
->lock_fd
, F_LOCK
, 0);
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);
1296 /* in this case, we should be able to do this with impunity, anyway */
1297 lockfRtn
= lockf(dp
->lock_fd
, F_LOCK
, 0);
1300 opr_Assert(lockfRtn
!= -1);
1302 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
1303 opr_Verify(lockf(dp
->lock_fd
, F_LOCK
, 0) != -1);
1305 opr_Verify(flock(dp
->lock_fd
, LOCK_EX
) == 0);
1306 #endif /* defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) */
1311 VUnlockPartition_r(char *name
)
1313 struct DiskPartition64
*dp
= VGetPartition_r(name
, 0);
1315 return; /* no partition, will fail later */
1317 dp
->lock_fd
= INVALID_FD
;
1320 #endif /* AFS_NT40_ENV */
1323 VLockPartition(char *name
)
1326 VLockPartition_r(name
);
1331 VUnlockPartition(char *name
)
1334 VUnlockPartition_r(name
);
1338 #ifdef AFS_DEMAND_ATTACH_FS
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
1345 * lock a partition's vol headers.
1347 * @param[in] dp the partition to lock
1348 * @param[in] locktype READ_LOCK or WRITE_LOCK
1350 * @return operation status
1354 VPartHeaderLock(struct DiskPartition64
*dp
, int locktype
)
1358 /* block on acquiring the lock */
1361 code
= VGetDiskLock(&dp
->headerLock
, locktype
, nonblock
);
1363 Log("VPartHeaderLock: error %d locking partititon %s\n", code
,
1364 VPartitionPath(dp
));
1370 * unlock a partition's vol headers.
1372 * @param[in] dp the partition to unlock
1373 * @param[in] locktype READ_LOCK or WRITE_LOCK
1376 VPartHeaderUnlock(struct DiskPartition64
*dp
, int locktype
)
1378 VReleaseDiskLock(&dp
->headerLock
, locktype
);
1381 /* XXX not sure this will work on AFS_NT40_ENV
1382 * needs to be tested!
1386 * lookup a disk partition object by its index number.
1388 * @param[in] id partition index number
1389 * @param[in] abortp see abortp usage note below
1391 * @return disk partition object
1392 * @retval NULL no such disk partition
1394 * @note when abortp is non-zero, lookups which would return
1395 * NULL will result in an assertion failure
1397 * @pre VOL_LOCK must be held
1399 * @internal volume package internal use only
1402 struct DiskPartition64
*
1403 VGetPartitionById_r(afs_int32 id
, int abortp
)
1405 struct DiskPartition64
*dp
= NULL
;
1407 if ((id
>= 0) && (id
<= VOLMAXPARTS
)) {
1408 dp
= DiskPartitionTable
[id
];
1412 opr_Assert(dp
!= NULL
);
1418 * lookup a disk partition object by its index number.
1420 * @param[in] id partition index number
1421 * @param[in] abortp see abortp usage note below
1423 * @return disk partition object
1424 * @retval NULL no such disk partition
1426 * @note when abortp is non-zero, lookups which would return
1427 * NULL will result in an assertion failure
1430 struct DiskPartition64
*
1431 VGetPartitionById(afs_int32 id
, int abortp
)
1433 struct DiskPartition64
* dp
;
1436 dp
= VGetPartitionById_r(id
, abortp
);
1442 static struct DiskPartition64
*
1443 VLookupPartition_r(char * path
)
1445 afs_int32 id
= volutil_GetPartitionID(path
);
1447 if (id
< 0 || id
> VOLMAXPARTS
)
1450 return DiskPartitionTable
[id
];
1454 AddPartitionToTable_r(struct DiskPartition64
*dp
)
1456 opr_Assert(dp
->index
>= 0 && dp
->index
<= VOLMAXPARTS
);
1457 DiskPartitionTable
[dp
->index
] = dp
;
1462 DeletePartitionFromTable_r(struct DiskPartition64
*dp
)
1464 opr_Assert(dp
->index
>= 0 && dp
->index
<= VOLMAXPARTS
);
1465 DiskPartitionTable
[dp
->index
] = NULL
;
1468 #endif /* AFS_DEMAND_ATTACH_FS */