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>
32 #include <sys/param.h>
33 #include <sys/types.h>
36 #if AFS_HAVE_STATVFS || AFS_HAVE_STATVFS64
37 #include <sys/statvfs.h>
38 #endif /* AFS_HAVE_STATVFS */
39 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
40 #include <sys/mount.h>
43 #if !defined(AFS_SGI_ENV)
45 #include <sys/mount.h>
47 #else /* AFS_OSF_ENV */
48 #ifdef AFS_VFSINCL_ENV
51 #include <sys/fs/ufs_fs.h>
53 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
54 #include <ufs/ufs/dinode.h>
55 #include <ufs/ffs/fs.h>
60 #else /* AFS_VFSINCL_ENV */
61 #if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX22_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
64 #endif /* AFS_VFSINCL_ENV */
65 #endif /* AFS_OSF_ENV */
72 #include <sys/lockf.h>
78 #include <checklist.h>
80 #if defined(AFS_SUN_ENV)
88 #include <sys/mnttab.h>
89 #include <sys/mntent.h>
91 #ifdef AFS_LINUX22_ENV
93 #include <sys/statfs.h>
100 #endif /* AFS_SGI_ENV */
101 #endif /* AFS_NT40_ENV */
102 #if defined(AFS_SGI_ENV)
103 #include <sys/errno.h>
104 #include <sys/stat.h>
106 #include <sys/file.h>
111 #include <afs/afsint.h>
113 #include <afs/errors.h>
116 #include <afs/afssyscalls.h>
123 #include "namei_ops.h"
125 #endif /* AFS_NT40_ENV */
126 #endif /* AFS_NAMEI_ENV */
129 #include "partition.h"
130 #include <afs/afs_assert.h>
132 #if defined(AFS_HPUX_ENV)
133 #include <sys/types.h>
134 #include <sys/privgrp.h>
135 #endif /* defined(AFS_HPUX_ENV) */
138 #include <jfs/filsys.h>
143 #define afs_stat stat64
144 #define afs_open open64
145 #define afs_fopen fopen64
147 #if AFS_HAVE_STATVFS64
148 # define afs_statvfs statvfs64
150 # if AFS_HAVE_STATFS64
151 # define afs_statfs statfs64
153 # if AFS_HAVE_STATVFS
154 # define afs_statvfs statvfs
156 # define afs_statfs statfs
157 # endif /* !AFS_HAVE_STATVFS */
158 # endif /* !AFS_HAVE_STATFS64 */
159 #endif /* !AFS_HAVE_STATVFS64 */
160 #endif /* !AFS_NT40_ENV */
162 #else /* !O_LARGEFILE */
164 #define afs_stat stat
165 #define afs_open open
166 #define afs_fopen fopen
169 #define afs_statvfs statvfs
170 #else /* !AFS_HAVE_STATVFS */
171 #define afs_statfs statfs
172 #endif /* !AFS_HAVE_STATVFS */
173 #endif /* !AFS_NT40_ENV */
175 #endif /* !O_LARGEFILE */
177 int aixlow_water
= 8; /* default 8% */
178 struct DiskPartition64
*DiskPartitionList
;
180 #ifdef AFS_DEMAND_ATTACH_FS
181 /* file to lock to conceptually "lock" the vol headers on a partition */
182 #define AFS_PARTLOCK_FILE ".volheaders.lock"
183 #define AFS_VOLUMELOCK_FILE ".volume.lock"
185 static struct DiskPartition64
*DiskPartitionTable
[VOLMAXPARTS
+1];
187 static struct DiskPartition64
* VLookupPartition_r(char * path
);
188 static void AddPartitionToTable_r(struct DiskPartition64
*);
189 #endif /* AFS_DEMAND_ATTACH_FS */
191 #ifdef AFS_SGI_XFS_IOPS_ENV
192 /* Verify that the on disk XFS inodes on the partition are large enough to
193 * hold the AFS attribute. Returns -1 if the attribute can't be set or is
194 * too small to fit in the inode. Returns 0 if the attribute does fit in
197 #include <afs/xfsattrs.h>
199 VerifyXFSInodeSize(char *part
, char *fstype
)
202 int length
= SIZEOF_XFS_ATTR_T
;
207 if (strcmp("xfs", fstype
))
210 if (attr_set(part
, AFS_XFS_ATTR
, &junk
, length
, ATTR_ROOT
) == 0) {
211 if (((fd
= open(part
, O_RDONLY
, 0)) != -1)
212 && (fcntl(fd
, F_FSGETXATTRA
, &fsx
) == 0)) {
214 if (fsx
.fsx_nextents
) {
215 Log("Partition %s: XFS inodes too small, exiting.\n", part
);
216 Log("Run xfs_size_check utility and remake partitions.\n");
223 (void)attr_remove(part
, AFS_XFS_ATTR
, ATTR_ROOT
);
227 #endif /* AFS_SGI_XFS_IOPS_ENV */
230 VInitPartitionPackage(void)
232 #ifdef AFS_DEMAND_ATTACH_FS
233 memset(&DiskPartitionTable
, 0, sizeof(DiskPartitionTable
));
234 #endif /* AFS_DEMAND_ATTACH_FS */
239 VInitPartition_r(char *path
, char *devname
, Device dev
)
241 struct DiskPartition64
*dp
, *op
;
243 dp
= (struct DiskPartition64
*)malloc(sizeof(struct DiskPartition64
));
244 /* Add it to the end, to preserve order when we print statistics */
245 for (op
= DiskPartitionList
; op
; op
= op
->next
) {
252 DiskPartitionList
= dp
;
254 dp
->name
= (char *)malloc(strlen(path
) + 1);
255 strncpy(dp
->name
, path
, strlen(path
) + 1);
256 dp
->index
= volutil_GetPartitionID(path
);
257 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
258 /* Create a lockfile for the partition, of the form /vicepa/Lock/vicepa */
259 dp
->devName
= (char *)malloc(2 * strlen(path
) + 6);
260 strcpy(dp
->devName
, path
);
261 strcat(dp
->devName
, OS_DIRSEP
);
262 strcat(dp
->devName
, "Lock");
263 mkdir(dp
->devName
, 0700);
264 strcat(dp
->devName
, path
);
265 close(afs_open(dp
->devName
, O_RDWR
| O_CREAT
, 0600));
266 dp
->device
= dp
->index
;
268 dp
->devName
= (char *)malloc(strlen(devname
) + 1);
269 strncpy(dp
->devName
, devname
, strlen(devname
) + 1);
272 dp
->lock_fd
= INVALID_FD
;
274 dp
->f_files
= 1; /* just a default value */
275 #if defined(AFS_NAMEI_ENV) && !defined(AFS_NT40_ENV)
276 if (programType
== fileServer
)
277 (void)namei_ViceREADME(VPartitionPath(dp
));
279 VSetPartitionDiskUsage_r(dp
);
280 #ifdef AFS_DEMAND_ATTACH_FS
281 AddPartitionToTable_r(dp
);
282 queue_Init(&dp
->vol_list
.head
);
283 CV_INIT(&dp
->vol_list
.cv
, "vol list", CV_DEFAULT
, 0);
284 dp
->vol_list
.len
= 0;
285 dp
->vol_list
.busy
= 0;
287 char lockpath
[MAXPATHLEN
+1];
288 afs_snprintf(lockpath
, MAXPATHLEN
, "%s/" AFS_PARTLOCK_FILE
, dp
->name
);
289 lockpath
[MAXPATHLEN
] = '\0';
290 VLockFileInit(&dp
->headerLockFile
, lockpath
);
292 afs_snprintf(lockpath
, MAXPATHLEN
, "%s/" AFS_VOLUMELOCK_FILE
, dp
->name
);
293 lockpath
[MAXPATHLEN
] = '\0';
294 VLockFileInit(&dp
->volLockFile
, lockpath
);
296 VDiskLockInit(&dp
->headerLock
, &dp
->headerLockFile
, 1);
297 #endif /* AFS_DEMAND_ATTACH_FS */
301 VInitPartition(char *path
, char *devname
, Device dev
)
304 VInitPartition_r(path
, devname
, dev
);
309 /* VAttachPartitions() finds the vice partitions on this server. Calls
310 * VCheckPartition() to do some basic checks on the partition. If the partition
311 * is a valid vice partition, VCheckPartition will add it to the DiskPartition
313 * Returns the number of errors returned by VCheckPartition. An error in
314 * VCheckPartition means that partition is a valid vice partition but the
315 * fileserver should not start because of the error found on that partition.
318 * No specific user space file system checks, since we don't know what
319 * is being used for vice partitions.
321 * Use partition name as devname.
324 VCheckPartition(char *part
, char *devname
, int logging
)
326 struct afs_stat status
;
327 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
328 char AFSIDatPath
[MAXPATHLEN
];
331 /* Only keep track of "/vicepx" partitions since it can get hairy
332 * when NFS mounts are involved.. */
333 if (strncmp(part
, VICE_PARTITION_PREFIX
, VICE_PREFIX_SIZE
)) {
336 if (afs_stat(part
, &status
) < 0) {
337 Log("VInitVnodes: Couldn't find file system %s; ignored\n", part
);
341 Log("This program is compiled without AFS_NAMEI_ENV, and "
342 "partition %s is mounted with the 'logging' option. "
343 "Using the inode fileserver backend with 'logging' UFS "
344 "partitions causes volume corruption, so please either "
345 "mount the partition without logging, or use the namei "
346 "fileserver backend. Aborting...\n", part
);
349 #ifndef AFS_AIX32_ENV
350 if (programType
== fileServer
) {
351 char salvpath
[MAXPATHLEN
];
352 strcpy(salvpath
, part
);
353 strcat(salvpath
, "/FORCESALVAGE");
354 if (afs_stat(salvpath
, &status
) == 0) {
355 Log("VInitVnodes: Found %s; aborting\n", salvpath
);
361 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_NT40_ENV)
362 strcpy(AFSIDatPath
, part
);
363 strcat(AFSIDatPath
, "/AFSIDat");
365 if (afs_stat(AFSIDatPath
, &status
) < 0) {
369 dirp
= opendir(part
);
371 while ((dp
= readdir(dirp
))) {
372 if (dp
->d_name
[0] == 'V') {
373 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
);
380 #else /* AFS_NAMEI_ENV */
381 if (afs_stat(AFSIDatPath
, &status
) == 0) {
382 Log("This program is compiled without AFS_NAMEI_ENV, but partition %s seems to contain volumes which use the namei-interface; aborting\n", part
);
386 #ifdef AFS_SGI_XFS_IOPS_ENV
387 if (VerifyXFSInodeSize(part
, status
.st_fstype
) < 0)
390 #endif /* AFS_NAMEI_ENV */
391 #endif /* !AFS_LINUX20_ENV && !AFS_NT40_ENV */
393 #if defined(AFS_DUX40_ENV) && !defined(AFS_NAMEI_ENV)
394 if (status
.st_ino
!= ROOTINO
) {
395 Log("%s is not a mounted file system; ignored.\n", part
);
400 VInitPartition(part
, devname
, status
.st_dev
);
405 /* VIsAlwaysAttach() checks whether a /vicepX directory should always be
406 * attached (return value 1), or only attached when it is a separately
407 * mounted partition (return value 0). For non-NAMEI environments, it
410 * *awouldattach will be set to 1 if the given path at least looks like a vice
411 * partition (that is, if we return 0, the only thing preventing this partition
412 * from being attached is the existence of the AlwaysAttach file), or to 0
413 * otherwise. *awouldattach is set regardless of whether or not the partition
414 * should always be attached or not.
417 VIsAlwaysAttach(char *part
, int *awouldattach
)
423 #endif /* AFS_NAMEI_ENV */
430 if (strncmp(part
, VICE_PARTITION_PREFIX
, VICE_PREFIX_SIZE
))
437 strncpy(checkfile
, part
, 100);
438 strcat(checkfile
, OS_DIRSEP
);
439 strcat(checkfile
, VICE_ALWAYSATTACH_FILE
);
441 ret
= afs_stat(checkfile
, &st
);
442 return (ret
< 0) ? 0 : 1;
443 #else /* AFS_NAMEI_ENV */
445 #endif /* AFS_NAMEI_ENV */
448 /* VIsNeverAttach() checks whether a /vicepX directory should never be
449 * attached (return value 1), or follow the normal mounting logic. The
450 * Always Attach flag may override the NeverAttach flag.
453 VIsNeverAttach(char *part
)
459 if (strncmp(part
, VICE_PARTITION_PREFIX
, VICE_PREFIX_SIZE
))
462 strncpy(checkfile
, part
, 100);
463 strcat(checkfile
, OS_DIRSEP
);
464 strcat(checkfile
, VICE_NEVERATTACH_FILE
);
466 ret
= afs_stat(checkfile
, &st
);
467 return (ret
< 0) ? 0 : 1;
470 /* VAttachPartitions2() looks for and attaches /vicepX partitions
471 * where a special file (VICE_ALWAYSATTACH_FILE) exists. This is
472 * used to attach /vicepX directories which aren't on dedicated
473 * partitions, in the NAMEI fileserver.
476 VAttachPartitions2(void)
484 dirp
= opendir(OS_DIRSEP
);
485 while ((de
= readdir(dirp
))) {
486 strcpy(pname
, OS_DIRSEP
);
487 strncat(pname
, de
->d_name
, 20);
488 pname
[sizeof(pname
) - 1] = '\0';
490 /* Only keep track of "/vicepx" partitions since automounter
492 if (VIsAlwaysAttach(pname
, &wouldattach
)) {
493 VCheckPartition(pname
, "", 0);
496 if (wouldattach
&& VGetPartition(pname
, 0) == NULL
&&
497 afs_stat(pname
, &st
) == 0 && S_ISDIR(st
.st_mode
)) {
499 /* This is a /vicep* dir, and it has not been attached as a
500 * partition. This probably means that this is a /vicep* dir
501 * that is not a separate partition, so just give a notice so
502 * admins are not confused as to why their /vicep* dirs are not
505 * It is possible that the dir _is_ a separate partition and we
506 * failed to attach it earlier, making this message a bit
507 * confusing. But that should be rare, and an error message
508 * about the failure will already be logged right before this,
509 * so it should be clear enough. */
511 Log("VAttachPartitions: not attaching %s; either it is not a "
512 "separate partition, or it failed to attach (create the "
513 "file %s/" VICE_ALWAYSATTACH_FILE
" to force attachment)\n",
519 #endif /* AFS_NAMEI_ENV */
521 #endif /* AFS_NT40_ENV */
525 VAttachPartitions(void)
531 if (!(mntfile
= afs_fopen(MNTTAB
, "r"))) {
532 Log("Can't open %s\n", MNTTAB
);
536 while (!getmntent(mntfile
, &mnt
)) {
538 /* Ignore non ufs or non read/write partitions */
539 /* but allow zfs too if we're in the NAMEI environment */
542 (((strcmp(mnt
.mnt_fstype
, "ufs") &&
543 strcmp(mnt
.mnt_fstype
, "zfs"))))
545 (strcmp(mnt
.mnt_fstype
, "ufs") != 0)
547 || (strncmp(mnt
.mnt_mntopts
, "ro,ignore", 9) == 0))
550 /* Skip this Partition? */
551 if (VIsNeverAttach(mnt
.mnt_mountp
))
554 /* If we're going to always attach this partition, do it later. */
555 if (VIsAlwaysAttach(mnt
.mnt_mountp
, NULL
))
558 #ifndef AFS_NAMEI_ENV
559 if (hasmntopt(&mnt
, "logging") != NULL
) {
562 #endif /* !AFS_NAMEI_ENV */
564 if (VCheckPartition(mnt
.mnt_mountp
, mnt
.mnt_special
, logging
) < 0)
568 (void)fclose(mntfile
);
570 /* Process the always-attach partitions, if any. */
571 VAttachPartitions2();
576 #endif /* AFS_SUN5_ENV */
577 #if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)) || defined(AFS_HPUX_ENV)
579 VAttachPartitions(void)
583 struct mntent
*mntent
;
585 if ((mfd
= setmntent(MOUNTED
, "r")) == NULL
) {
586 Log("Problems in getting mount entries(setmntent)\n");
589 while (mntent
= getmntent(mfd
)) {
590 if (!hasmntopt(mntent
, MNTOPT_RW
))
593 /* Skip this Partition? */
594 if (VIsNeverAttach(mntent
->mnt_dir
))
597 /* If we're going to always attach this partition, do it later. */
598 if (VIsAlwaysAttach(mntent
->mnt_dir
, NULL
))
601 if (VCheckPartition(mntent
->mnt_dir
, mntent
->mnt_fsname
, 0) < 0)
607 /* Process the always-attach partitions, if any. */
608 VAttachPartitions2();
615 * (This function was grabbed from df.c)
618 getmount(struct vmount
**vmountpp
)
624 /* set initial size of mntctl buffer to a MAGIC NUMBER */
627 /* try the operation until ok or a fatal error */
629 if ((vm
= (struct vmount
*)malloc(size
)) == NULL
) {
630 /* failed getting memory for mount status buf */
631 perror("FATAL ERROR: get_stat malloc failed\n");
636 * perform the QUERY mntctl - if it returns > 0, that is the
637 * number of vmount structures in the buffer. If it returns
638 * -1, an error occured. If it returned 0, then look in
639 * first word of buffer for needed size.
641 if ((nmounts
= mntctl(MCTL_QUERY
, size
, (caddr_t
) vm
)) > 0) {
642 /* OK, got it, now return */
646 } else if (nmounts
== 0) {
647 /* the buffer wasn't big enough .... */
648 /* .... get required buffer size */
653 /* some other kind of error occurred */
661 VAttachPartitions(void)
665 struct vmount
*vmountp
;
667 if ((nmounts
= getmount(&vmountp
)) <= 0) {
668 Log("Problems in getting # of mount entries(getmount)\n");
673 (struct vmount
*)((int)vmountp
+ vmountp
->vmt_length
)) {
674 char *part
= vmt2dataptr(vmountp
, VMT_STUB
);
676 if (vmountp
->vmt_flags
& (MNT_READONLY
| MNT_REMOVABLE
| MNT_REMOTE
))
677 continue; /* Ignore any "special" partitions */
680 #ifndef AFS_NAMEI_ENV
682 struct superblock fs
;
683 /* The Log statements are non-sequiters in the SalvageLog and don't
684 * even appear in the VolserLog, so restrict them to the FileLog.
686 if (ReadSuper(&fs
, vmt2dataptr(vmountp
, VMT_OBJECT
)) < 0) {
687 if (programType
== fileServer
)
688 Log("Can't read superblock for %s, ignoring it.\n", part
);
691 if (IsBigFilesFileSystem(&fs
)) {
692 if (programType
== fileServer
)
693 Log("%s is a big files filesystem, ignoring it.\n", part
);
700 /* Skip this Partition? */
701 if (VIsNeverAttach(part
))
704 /* If we're going to always attach this partition, do it later. */
705 if (VIsAlwaysAttach(part
, NULL
))
708 if (VCheckPartition(part
, vmt2dataptr(vmountp
, VMT_OBJECT
), 0) < 0)
712 /* Process the always-attach partitions, if any. */
713 VAttachPartitions2();
718 #if defined(AFS_DUX40_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
720 VAttachPartitions(void)
725 if (setfsent() < 0) {
726 Log("Error listing filesystems.\n");
730 while ((fsent
= getfsent())) {
731 if (strcmp(fsent
->fs_type
, "rw") != 0)
734 /* Skip this Partition? */
735 if (VIsNeverAttach(fsent
->fs_file
))
738 /* If we're going to always attach this partition, do it later. */
739 if (VIsAlwaysAttach(fsent
->fs_file
, NULL
))
742 if (VCheckPartition(fsent
->fs_file
, fsent
->fs_spec
, 0) < 0)
747 /* Process the always-attach partitions, if any. */
748 VAttachPartitions2();
756 #include <sys/stat.h>
759 * validate names in vptab.
767 VValidVPTEntry(struct vptab
*vpe
)
769 int len
= strlen(vpe
->vp_name
);
772 if (len
< VICE_PREFIX_SIZE
+ 1 || len
> VICE_PREFIX_SIZE
+ 2)
774 if (strncmp(vpe
->vp_name
, VICE_PARTITION_PREFIX
, VICE_PREFIX_SIZE
))
777 for (i
= VICE_PREFIX_SIZE
; i
< len
; i
++) {
778 if (vpe
->vp_name
[i
] < 'a' || vpe
->vp_name
[i
] > 'z') {
779 Log("Invalid partition name %s in registry, ignoring it.\n",
784 if (len
== VICE_PREFIX_SIZE
+ 2) {
785 i
= (int)(vpe
->vp_name
[VICE_PREFIX_SIZE
] - 'a') * 26 +
786 (int)(vpe
->vp_name
[VICE_PREFIX_SIZE
+ 1] - 'a');
788 Log("Invalid partition name %s in registry, ignoring it.\n",
794 len
= strlen(vpe
->vp_dev
);
795 if (len
!= 2 || vpe
->vp_dev
[1] != ':' || vpe
->vp_dev
[0] < 'A'
796 || vpe
->vp_dev
[0] > 'Z') {
797 Log("Invalid device name %s in registry, ignoring it.\n",
806 VCheckPartition(char *partName
)
813 /* partName is presumed to be of the form "X:" */
814 (void)sprintf(volRoot
, "%c:\\", *partName
);
816 if (!GetVolumeInformation(volRoot
, /* volume root directory */
817 NULL
, /* volume name buffer */
818 0, /* volume name size */
819 NULL
, /* volume serial number */
820 &dwDummy
, /* max component length */
821 &dwDummy
, /* file system flags */
822 volFsType
, /* file system name */
823 sizeof(volFsType
))) {
824 err
= GetLastError();
825 Log("VCheckPartition: Failed to get partition information for %s, ignoring it.\n", partName
);
829 if (strcmp(volFsType
, "NTFS")) {
830 Log("VCheckPartition: Partition %s is not an NTFS partition, ignoring it.\n", partName
);
839 VAttachPartitions(void)
841 struct DiskPartition64
*partP
, *prevP
, *nextP
;
842 struct vpt_iter iter
;
845 if (vpt_Start(&iter
) < 0) {
846 Log("No partitions to attach.\n");
850 while (0 == vpt_NextEntry(&iter
, &entry
)) {
851 if (!VValidVPTEntry(&entry
)) {
855 /* This test for duplicates relies on the fact that the method
856 * of storing the partition names in the NT registry means the same
857 * partition name will never appear twice in the list.
859 for (partP
= DiskPartitionList
; partP
; partP
= partP
->next
) {
860 if (*partP
->devName
== *entry
.vp_dev
) {
861 Log("Same drive (%s) used for both partition %s and partition %s, ignoring both.\n", entry
.vp_dev
, partP
->name
, entry
.vp_name
);
862 partP
->flags
= PART_DUPLICATE
;
863 break; /* Only one entry will ever be in this list. */
867 continue; /* found a duplicate */
869 if (VCheckPartition(entry
.vp_dev
) < 0)
871 /* This test allows for manually inserting the FORCESALVAGE flag
872 * and thereby invoking the salvager. scandisk obviously won't be
875 if (programType
== fileServer
) {
876 struct afs_stat status
;
877 char salvpath
[MAXPATHLEN
];
878 strcpy(salvpath
, entry
.vp_dev
);
879 strcat(salvpath
, "\\FORCESALVAGE");
880 if (afs_stat(salvpath
, &status
) == 0) {
881 Log("VAttachPartitions: Found %s; aborting\n", salvpath
);
885 VInitPartition(entry
.vp_name
, entry
.vp_dev
, *entry
.vp_dev
- 'A');
889 /* Run through partition list and clear out the dupes. */
890 prevP
= nextP
= NULL
;
891 for (partP
= DiskPartitionList
; partP
; partP
= nextP
) {
893 if (partP
->flags
== PART_DUPLICATE
) {
895 prevP
->next
= partP
->next
;
897 DiskPartitionList
= partP
->next
;
907 #ifdef AFS_LINUX22_ENV
909 VAttachPartitions(void)
913 struct mntent
*mntent
;
915 if ((mfd
= setmntent("/proc/mounts", "r")) == NULL
) {
916 if ((mfd
= setmntent("/etc/mtab", "r")) == NULL
) {
917 Log("Problems in getting mount entries(setmntent)\n");
921 while ((mntent
= getmntent(mfd
))) {
922 /* Skip this Partition? */
923 if (VIsNeverAttach(mntent
->mnt_dir
))
926 /* If we're going to always attach this partition, do it later. */
927 if (VIsAlwaysAttach(mntent
->mnt_dir
, NULL
))
930 if (VCheckPartition(mntent
->mnt_dir
, mntent
->mnt_fsname
, 0) < 0)
935 /* Process the always-attach partitions, if any. */
936 VAttachPartitions2();
940 #endif /* AFS_LINUX22_ENV */
942 /* This routine is to be called whenever the actual name of the partition
943 * is required. The canonical name is still in part->name.
946 VPartitionPath(struct DiskPartition64
*part
)
949 return part
->devName
;
955 /* get partition structure, abortp tells us if we should abort on failure */
956 struct DiskPartition64
*
957 VGetPartition_r(char *name
, int abortp
)
959 struct DiskPartition64
*dp
;
960 #ifdef AFS_DEMAND_ATTACH_FS
961 dp
= VLookupPartition_r(name
);
962 #else /* AFS_DEMAND_ATTACH_FS */
963 for (dp
= DiskPartitionList
; dp
; dp
= dp
->next
) {
964 if (strcmp(dp
->name
, name
) == 0)
967 #endif /* AFS_DEMAND_ATTACH_FS */
969 osi_Assert(dp
!= NULL
);
973 struct DiskPartition64
*
974 VGetPartition(char *name
, int abortp
)
976 struct DiskPartition64
*retVal
;
978 retVal
= VGetPartition_r(name
, abortp
);
985 VSetPartitionDiskUsage_r(struct DiskPartition64
*dp
)
987 ULARGE_INTEGER free_user
, total
, free_total
;
988 int ufree
, tot
, tfree
;
990 if (!GetDiskFreeSpaceEx
991 (VPartitionPath(dp
), &free_user
, &total
, &free_total
)) {
992 printf("Failed to get disk space info for %s, error = %d\n", dp
->name
,
997 /* Convert to 1K units. */
998 ufree
= (int)Int64ShraMod32(free_user
.QuadPart
, 10);
999 tot
= (int)Int64ShraMod32(total
.QuadPart
, 10);
1000 tfree
= (int)Int64ShraMod32(free_total
.QuadPart
, 10);
1002 dp
->minFree
= tfree
- ufree
; /* only used in VPrintDiskStats_r */
1003 dp
->totalUsable
= tot
;
1009 VSetPartitionDiskUsage_r(struct DiskPartition64
*dp
)
1012 afs_int64 totalblks
, free
, used
, availblks
;
1015 struct afs_statvfs statbuf
;
1017 struct afs_statfs statbuf
;
1020 if (dp
->flags
& PART_DONTUPDATE
)
1022 /* Note: we don't bother syncing because it's only an estimate, update
1023 * is syncing every 30 seconds anyway, we only have to keep the disk
1024 * approximately 10% from full--you just can't get the stuff in from
1025 * the net fast enough to worry */
1027 code
= afs_statvfs(dp
->name
, &statbuf
);
1029 code
= afs_statfs(dp
->name
, &statbuf
);
1032 Log("statfs of %s failed in VSetPartitionDiskUsage (errno = %d)\n",
1036 if (statbuf
.f_blocks
== -1) { /* Undefined; skip stats.. */
1037 Log("statfs of %s failed in VSetPartitionDiskUsage\n", dp
->name
);
1040 totalblks
= statbuf
.f_blocks
;
1041 free
= statbuf
.f_bfree
;
1042 reserved
= free
- statbuf
.f_bavail
;
1044 bsize
= statbuf
.f_frsize
;
1046 bsize
= statbuf
.f_bsize
;
1048 availblks
= totalblks
- reserved
;
1049 dp
->f_files
= statbuf
.f_files
; /* max # of files in partition */
1051 /* Now free and totalblks are in fragment units, but we want them in
1054 if (bsize
>= 1024) {
1055 free
*= (bsize
/ 1024);
1056 totalblks
*= (bsize
/ 1024);
1057 availblks
*= (bsize
/ 1024);
1058 reserved
*= (bsize
/ 1024);
1060 free
/= (1024 / bsize
);
1061 totalblks
/= (1024 / bsize
);
1062 availblks
/= (1024 / bsize
);
1063 reserved
/= (1024 / bsize
);
1065 /* now compute remaining figures */
1066 used
= totalblks
- free
;
1068 dp
->minFree
= reserved
; /* only used in VPrintDiskStats_r */
1069 dp
->totalUsable
= availblks
;
1070 dp
->free
= availblks
- used
; /* this is exactly f_bavail */
1072 #endif /* AFS_NT40_ENV */
1075 VSetPartitionDiskUsage(struct DiskPartition64
*dp
)
1078 VSetPartitionDiskUsage_r(dp
);
1083 VResetDiskUsage_r(void)
1085 struct DiskPartition64
*dp
;
1086 for (dp
= DiskPartitionList
; dp
; dp
= dp
->next
) {
1087 VSetPartitionDiskUsage_r(dp
);
1088 #ifndef AFS_PTHREAD_ENV
1090 #endif /* !AFS_PTHREAD_ENV */
1095 VResetDiskUsage(void)
1098 VResetDiskUsage_r();
1103 VAdjustDiskUsage_r(Error
* ec
, Volume
* vp
, afs_sfsize_t blocks
,
1104 afs_sfsize_t checkBlocks
)
1107 /* why blocks instead of checkBlocks in the check below? Otherwise, any check
1108 * for less than BlocksSpare would skip the error-checking path, and we
1109 * could grow existing files forever, not just for another BlocksSpare
1112 #ifdef AFS_AIX32_ENV
1113 afs_int32 rem
, minavail
;
1115 if ((rem
= vp
->partition
->free
- checkBlocks
) < (minavail
=
1121 if (vp
->partition
->free
- checkBlocks
< 0)
1124 else if (V_maxquota(vp
)
1125 && V_diskused(vp
) + checkBlocks
> V_maxquota(vp
))
1128 vp
->partition
->free
-= blocks
;
1129 V_diskused(vp
) += blocks
;
1133 VAdjustDiskUsage(Error
* ec
, Volume
* vp
, afs_sfsize_t blocks
,
1134 afs_sfsize_t checkBlocks
)
1137 VAdjustDiskUsage_r(ec
, vp
, blocks
, checkBlocks
);
1142 VDiskUsage_r(Volume
* vp
, afs_sfsize_t blocks
)
1145 #ifdef AFS_AIX32_ENV
1146 afs_int32 rem
, minavail
;
1148 if ((rem
= vp
->partition
->free
- blocks
) < (minavail
=
1151 aixlow_water
) / 100))
1153 if (vp
->partition
->free
- blocks
< 0)
1157 vp
->partition
->free
-= blocks
;
1162 VDiskUsage(Volume
* vp
, afs_sfsize_t blocks
)
1166 retVal
= VDiskUsage_r(vp
, blocks
);
1172 VPrintDiskStats_r(void)
1174 struct DiskPartition64
*dp
;
1175 for (dp
= DiskPartitionList
; dp
; dp
= dp
->next
) {
1177 Log("Partition %s: %"AFS_INT64_FMT
1178 " available 1K blocks (minfree=%"AFS_INT64_FMT
"), "
1179 "overallocated by %"AFS_INT64_FMT
" blocks\n", dp
->name
,
1180 dp
->totalUsable
, dp
->minFree
, -dp
->free
);
1182 Log("Partition %s: %"AFS_INT64_FMT
1183 " available 1K blocks (minfree=%"AFS_INT64_FMT
"), "
1184 "%"AFS_INT64_FMT
" free blocks\n", dp
->name
,
1185 dp
->totalUsable
, dp
->minFree
, dp
->free
);
1191 VPrintDiskStats(void)
1194 VPrintDiskStats_r();
1199 /* Need a separate lock file on NT, since NT only has mandatory file locks. */
1200 #define LOCKFILE "LOCKFILE"
1202 VLockPartition_r(char *name
)
1204 struct DiskPartition64
*dp
= VGetPartition_r(name
, 0);
1209 if (dp
->lock_fd
== INVALID_FD
) {
1212 (void)sprintf(path
, "%s\\%s", VPartitionPath(dp
), LOCKFILE
);
1214 (FD_t
)CreateFile(path
, GENERIC_WRITE
,
1215 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
1216 CREATE_ALWAYS
, FILE_ATTRIBUTE_HIDDEN
, NULL
);
1217 osi_Assert(dp
->lock_fd
!= INVALID_FD
);
1219 memset(&lap
, 0, sizeof(lap
));
1220 rc
= LockFileEx((HANDLE
) dp
->lock_fd
, LOCKFILE_EXCLUSIVE_LOCK
, 0, 1,
1227 VUnlockPartition_r(char *name
)
1229 struct DiskPartition64
*dp
= VGetPartition_r(name
, 0);
1233 return; /* no partition, will fail later */
1234 memset(&lap
, 0, sizeof(lap
));
1236 UnlockFileEx((HANDLE
) dp
->lock_fd
, 0, 1, 0, &lap
);
1237 CloseHandle((HANDLE
) dp
->lock_fd
);
1238 dp
->lock_fd
= INVALID_FD
;
1240 #else /* AFS_NT40_ENV */
1242 #if defined(AFS_HPUX_ENV)
1243 #define BITS_PER_CHAR (8)
1244 #define BITS(type) (sizeof(type) * BITS_PER_CHAR)
1246 #define LOCKRDONLY_OFFSET ((PRIV_LOCKRDONLY - 1) / BITS(int))
1247 #endif /* defined(AFS_HPUX_ENV) */
1250 VLockPartition_r(char *name
)
1252 struct DiskPartition64
*dp
= VGetPartition_r(name
, 0);
1253 char *partitionName
;
1255 struct timeval pausing
;
1256 #if defined(AFS_HPUX_ENV)
1258 struct privgrp_map privGrpList
[PRIV_MAXGRPS
];
1259 unsigned int *globalMask
;
1260 int globalMaskIndex
;
1261 #endif /* defined(AFS_HPUX_ENV) */
1262 #if defined(AFS_DARWIN_ENV)
1263 char lockfile
[MAXPATHLEN
];
1264 #endif /* defined(AFS_DARWIN_ENV) */
1265 #ifdef AFS_NAMEI_ENV
1266 #ifdef AFS_AIX42_ENV
1267 char LockFileName
[MAXPATHLEN
+ 1];
1269 sprintf((char *)&LockFileName
, "%s/AFSINODE_FSLock", name
);
1270 partitionName
= (char *)&LockFileName
;
1275 return; /* no partition, will fail later */
1276 if (dp
->lock_fd
!= INVALID_FD
)
1279 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV)
1280 #if !defined(AFS_AIX42_ENV) || !defined(AFS_NAMEI_ENV)
1281 partitionName
= dp
->devName
;
1284 #elif defined(AFS_DARWIN_ENV)
1285 strlcpy((partitionName
= lockfile
), dp
->name
, sizeof(lockfile
));
1286 strlcat(lockfile
, "/.lock.afs", sizeof(lockfile
));
1287 code
= O_RDONLY
| O_CREAT
;
1289 partitionName
= dp
->name
;
1293 for (retries
= 25; retries
; retries
--) {
1295 dp
->lock_fd
= afs_open(partitionName
, code
, 0644);
1297 dp
->lock_fd
= afs_open(partitionName
, code
);
1299 if (dp
->lock_fd
!= INVALID_FD
)
1301 if (errno
== ENOENT
)
1304 pausing
.tv_usec
= 500000;
1305 select(0, NULL
, NULL
, NULL
, &pausing
);
1307 osi_Assert(retries
!= 0);
1309 #if defined (AFS_HPUX_ENV)
1311 osi_Assert(getprivgrp(privGrpList
) == 0);
1314 * In general, it will difficult and time-consuming ,if not impossible,
1315 * to try to find the privgroup to which this process belongs that has the
1316 * smallest membership, to minimise the security hole. So, we use the privgrp
1317 * to which everybody belongs.
1319 /* first, we have to find the global mask */
1320 for (globalMaskIndex
= 0; globalMaskIndex
< PRIV_MAXGRPS
;
1321 globalMaskIndex
++) {
1322 if (privGrpList
[globalMaskIndex
].priv_groupno
== PRIV_GLOBAL
) {
1324 &(privGrpList
[globalMaskIndex
].priv_mask
[LOCKRDONLY_OFFSET
]);
1329 if (((*globalMask
) & privmask(PRIV_LOCKRDONLY
)) == 0) {
1330 /* allow everybody to set a lock on a read-only file descriptor */
1331 (*globalMask
) |= privmask(PRIV_LOCKRDONLY
);
1332 osi_Assert(setprivgrp(PRIV_GLOBAL
, privGrpList
[globalMaskIndex
].priv_mask
)
1335 lockfRtn
= lockf(dp
->lock_fd
, F_LOCK
, 0);
1337 /* remove the privilege granted to everybody to lock a read-only fd */
1338 (*globalMask
) &= ~(privmask(PRIV_LOCKRDONLY
));
1339 osi_Assert(setprivgrp(PRIV_GLOBAL
, privGrpList
[globalMaskIndex
].priv_mask
)
1342 /* in this case, we should be able to do this with impunity, anyway */
1343 lockfRtn
= lockf(dp
->lock_fd
, F_LOCK
, 0);
1346 osi_Assert(lockfRtn
!= -1);
1348 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
1349 osi_Assert(lockf(dp
->lock_fd
, F_LOCK
, 0) != -1);
1351 osi_Assert(flock(dp
->lock_fd
, LOCK_EX
) == 0);
1352 #endif /* defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) */
1357 VUnlockPartition_r(char *name
)
1359 struct DiskPartition64
*dp
= VGetPartition_r(name
, 0);
1361 return; /* no partition, will fail later */
1363 dp
->lock_fd
= INVALID_FD
;
1366 #endif /* AFS_NT40_ENV */
1369 VLockPartition(char *name
)
1372 VLockPartition_r(name
);
1377 VUnlockPartition(char *name
)
1380 VUnlockPartition_r(name
);
1384 #ifdef AFS_DEMAND_ATTACH_FS
1386 /* new-style partition locks; these are only to have some mutual exclusion
1387 * between the VGC scanner and volume utilies creating/altering vol headers
1391 * lock a partition's vol headers.
1393 * @param[in] dp the partition to lock
1394 * @param[in] locktype READ_LOCK or WRITE_LOCK
1396 * @return operation status
1400 VPartHeaderLock(struct DiskPartition64
*dp
, int locktype
)
1404 /* block on acquiring the lock */
1407 code
= VGetDiskLock(&dp
->headerLock
, locktype
, nonblock
);
1409 Log("VPartHeaderLock: error %d locking partititon %s\n", code
,
1410 VPartitionPath(dp
));
1416 * unlock a partition's vol headers.
1418 * @param[in] dp the partition to unlock
1419 * @param[in] locktype READ_LOCK or WRITE_LOCK
1422 VPartHeaderUnlock(struct DiskPartition64
*dp
, int locktype
)
1424 VReleaseDiskLock(&dp
->headerLock
, locktype
);
1427 /* XXX not sure this will work on AFS_NT40_ENV
1428 * needs to be tested!
1432 * lookup a disk partition object by its index number.
1434 * @param[in] id partition index number
1435 * @param[in] abortp see abortp usage note below
1437 * @return disk partition object
1438 * @retval NULL no such disk partition
1440 * @note when abortp is non-zero, lookups which would return
1441 * NULL will result in an assertion failure
1443 * @pre VOL_LOCK must be held
1445 * @internal volume package internal use only
1448 struct DiskPartition64
*
1449 VGetPartitionById_r(afs_int32 id
, int abortp
)
1451 struct DiskPartition64
*dp
= NULL
;
1453 if ((id
>= 0) && (id
<= VOLMAXPARTS
)) {
1454 dp
= DiskPartitionTable
[id
];
1458 osi_Assert(dp
!= NULL
);
1464 * lookup a disk partition object by its index number.
1466 * @param[in] id partition index number
1467 * @param[in] abortp see abortp usage note below
1469 * @return disk partition object
1470 * @retval NULL no such disk partition
1472 * @note when abortp is non-zero, lookups which would return
1473 * NULL will result in an assertion failure
1476 struct DiskPartition64
*
1477 VGetPartitionById(afs_int32 id
, int abortp
)
1479 struct DiskPartition64
* dp
;
1482 dp
= VGetPartitionById_r(id
, abortp
);
1488 static struct DiskPartition64
*
1489 VLookupPartition_r(char * path
)
1491 afs_int32 id
= volutil_GetPartitionID(path
);
1493 if (id
< 0 || id
> VOLMAXPARTS
)
1496 return DiskPartitionTable
[id
];
1500 AddPartitionToTable_r(struct DiskPartition64
*dp
)
1502 osi_Assert(dp
->index
>= 0 && dp
->index
<= VOLMAXPARTS
);
1503 DiskPartitionTable
[dp
->index
] = dp
;
1508 DeletePartitionFromTable_r(struct DiskPartition64
*dp
)
1510 osi_Assert(dp
->index
>= 0 && dp
->index
<= VOLMAXPARTS
);
1511 DiskPartitionTable
[dp
->index
] = NULL
;
1514 #endif /* AFS_DEMAND_ATTACH_FS */