From 1d3d90a8673b8e3fc8a20829ca5f95b64bef5397 Mon Sep 17 00:00:00 2001 From: Marco van Wieringen Date: Sun, 16 May 2010 15:59:45 +0200 Subject: [PATCH] Added fstype rewrite code so we have a uniform way of retrieving the fstype on Linux and OSF1 without the need to add code for each new filesystem added to a OS. --- bacula/src/findlib/fstype.c | 168 ++++--------- bacula/src/findlib/protos.h | 1 + bacula/src/lib/Makefile.in | 6 +- bacula/src/lib/mntent_cache.c | 449 ++++++++++++++++++++++++++++++++++ bacula/src/lib/mntent_cache.h | 52 ++++ 5 files changed, 553 insertions(+), 123 deletions(-) create mode 100644 bacula/src/lib/mntent_cache.c create mode 100644 bacula/src/lib/mntent_cache.h diff --git a/bacula/src/findlib/fstype.c b/bacula/src/findlib/fstype.c index 131191177d..122c11563d 100644 --- a/bacula/src/findlib/fstype.c +++ b/bacula/src/findlib/fstype.c @@ -53,10 +53,12 @@ "HAVE_NETBSD_OS\n" \ "HAVE_OPENBSD_OS\n" \ "HAVE_SUN_OS\n" \ + "HAVE_OSF1_OS\n" \ "HAVE_WIN32\n" #define false 0 #define true 1 #define bstrncpy strncpy +#define bstrcmp(s1, s2) !strcmp(s1, s2) #define Dmsg0(n,s) fprintf(stderr, s) #define Dmsg1(n,s,a1) fprintf(stderr, s, a1) #define Dmsg2(n,s,a1,a2) fprintf(stderr, s, a1, a2) @@ -65,11 +67,11 @@ /* * These functions should be implemented for each OS * - * bool fstype(const char *fname, char *fs, int fslen); + * bool fstype(const char *fname, char *fs, int fslen); */ -#if defined(HAVE_DARWIN_OS) \ - || defined(HAVE_FREEBSD_OS ) \ - || defined(HAVE_OPENBSD_OS) +#if defined(HAVE_DARWIN_OS) || \ + defined(HAVE_FREEBSD_OS) || \ + defined(HAVE_OPENBSD_OS) #include #include @@ -77,13 +79,16 @@ bool fstype(const char *fname, char *fs, int fslen) { struct statfs st; + if (statfs(fname, &st) == 0) { bstrncpy(fs, st.f_fstypename, fslen); return true; } + Dmsg1(50, "statfs() failed for \"%s\"\n", fname); return false; } + #elif defined(HAVE_NETBSD_OS) #include #include @@ -96,15 +101,18 @@ bool fstype(const char *fname, char *fs, int fslen) bool fstype(const char *fname, char *fs, int fslen) { struct statvfs st; + if (statvfs(fname, &st) == 0) { bstrncpy(fs, st.f_fstypename, fslen); return true; } + Dmsg1(50, "statfs() failed for \"%s\"\n", fname); return false; } -#elif defined(HAVE_HPUX_OS) \ - || defined(HAVE_IRIX_OS) + +#elif defined(HAVE_HPUX_OS) || \ + defined(HAVE_IRIX_OS) #include #include @@ -112,111 +120,36 @@ bool fstype(const char *fname, char *fs, int fslen) bool fstype(const char *fname, char *fs, int fslen) { struct statvfs st; + if (statvfs(fname, &st) == 0) { bstrncpy(fs, st.f_basetype, fslen); return true; } + Dmsg1(50, "statfs() failed for \"%s\"\n", fname); return false; } -#elif defined(HAVE_LINUX_OS) +#elif defined(HAVE_LINUX_OS) || \ + defined(HAVE_OSF1_OS) -#include +#include +#include "lib/mntent_cache.h" bool fstype(const char *fname, char *fs, int fslen) { - struct statfs st; - if (statfs(fname, &st) == 0) { - /* - * Values nicked from statfs(2), testing and - * - * $ grep -r SUPER_MAGIC /usr/include/linux - * - * Entries are sorted on ("fsname") - */ - switch (st.f_type) { - - /* Known good values */ - case 0xef53: bstrncpy(fs, "ext2", fslen); return true; /* EXT2_SUPER_MAGIC */ - /* case 0xef53: ext2 and ext3 are the same */ /* EXT3_SUPER_MAGIC */ - case 0x3153464a: bstrncpy(fs, "jfs", fslen); return true; /* JFS_SUPER_MAGIC */ - case 0x5346544e: bstrncpy(fs, "ntfs", fslen); return true; /* NTFS_SB_MAGIC */ - case 0x9fa0: bstrncpy(fs, "proc", fslen); return true; /* PROC_SUPER_MAGIC */ - case 0x52654973: bstrncpy(fs, "reiserfs", fslen); return true; /* REISERFS_SUPER_MAGIC */ - case 0x58465342: bstrncpy(fs, "xfs", fslen); return true; /* XFS_SB_MAGIC */ - case 0x9fa2: bstrncpy(fs, "usbdevfs", fslen); return true; /* USBDEVICE_SUPER_MAGIC */ - case 0x62656572: bstrncpy(fs, "sysfs", fslen); return true; /* SYSFS_MAGIC */ - case 0x517B: bstrncpy(fs, "smbfs", fslen); return true; /* SMB_SUPER_MAGIC */ - case 0x9660: bstrncpy(fs, "iso9660", fslen); return true; /* ISOFS_SUPER_MAGIC */ - -#if 0 /* These need confirmation */ - case 0xadf5: bstrncpy(fs, "adfs", fslen); return true; /* ADFS_SUPER_MAGIC */ - case 0xadff: bstrncpy(fs, "affs", fslen); return true; /* AFFS_SUPER_MAGIC */ - case 0x6B414653: bstrncpy(fs, "afs", fslen); return true; /* AFS_FS_MAGIC */ - case 0x0187: bstrncpy(fs, "autofs", fslen); return true; /* AUTOFS_SUPER_MAGIC */ - case 0x62646576: bstrncpy(fs, "bdev", fslen); return true; /* ??? */ - case 0x42465331: bstrncpy(fs, "befs", fslen); return true; /* BEFS_SUPER_MAGIC */ - case 0x1BADFACE: bstrncpy(fs, "bfs", fslen); return true; /* BFS_MAGIC */ - case 0x42494e4d: bstrncpy(fs, "binfmt_misc", fslen); return true; /* ??? */ - case (('C'<<8)|'N'): bstrncpy(fs, "capifs", fslen); return true; /* CAPIFS_SUPER_MAGIC */ - case 0xFF534D42: bstrncpy(fs, "cifs", fslen); return true; /* CIFS_MAGIC_NUMBER */ - case 0x73757245: bstrncpy(fs, "coda", fslen); return true; /* CODA_SUPER_MAGIC */ - case 0x012ff7b7: bstrncpy(fs, "coherent", fslen); return true; /* COH_SUPER_MAGIC */ - case 0x28cd3d45: bstrncpy(fs, "cramfs", fslen); return true; /* CRAMFS_MAGIC */ - case 0x1373: bstrncpy(fs, "devfs", fslen); return true; /* DEVFS_SUPER_MAGIC */ - case 0x1cd1: bstrncpy(fs, "devpts", fslen); return true; /* ??? */ - case 0x414A53: bstrncpy(fs, "efs", fslen); return true; /* EFS_SUPER_MAGIC */ - case 0x03111965: bstrncpy(fs, "eventpollfs", fslen); return true; /* EVENTPOLLFS_MAGIC */ - case 0x137d: bstrncpy(fs, "ext", fslen); return true; /* EXT_SUPER_MAGIC */ - case 0xef51: bstrncpy(fs, "ext2", fslen); return true; /* EXT2_OLD_SUPER_MAGIC */ - case 0xBAD1DEA: bstrncpy(fs, "futexfs", fslen); return true; /* ??? */ - case 0xaee71ee7: bstrncpy(fs, "gadgetfs", fslen); return true; /* GADGETFS_MAGIC */ - case 0x00c0ffee: bstrncpy(fs, "hostfs", fslen); return true; /* HOSTFS_SUPER_MAGIC */ - case 0xf995e849: bstrncpy(fs, "hpfs", fslen); return true; /* HPFS_SUPER_MAGIC */ - case 0xb00000ee: bstrncpy(fs, "hppfs", fslen); return true; /* HPPFS_SUPER_MAGIC */ - case 0x958458f6: bstrncpy(fs, "hugetlbfs", fslen); return true; /* HUGETLBFS_MAGIC */ - case 0x12061983: bstrncpy(fs, "hwgfs", fslen); return true; /* HWGFS_MAGIC */ - case 0x66726f67: bstrncpy(fs, "ibmasmfs", fslen); return true; /* IBMASMFS_MAGIC */ - case 0x9660: bstrncpy(fs, "isofs", fslen); return true; /* ISOFS_SUPER_MAGIC */ - case 0x07c0: bstrncpy(fs, "jffs", fslen); return true; /* JFFS_MAGIC_SB_BITMASK */ - case 0x72b6: bstrncpy(fs, "jffs2", fslen); return true; /* JFFS2_SUPER_MAGIC */ - case 0x2468: bstrncpy(fs, "minix", fslen); return true; /* MINIX2_SUPER_MAGIC */ - case 0x2478: bstrncpy(fs, "minix", fslen); return true; /* MINIX2_SUPER_MAGIC2 */ - case 0x137f: bstrncpy(fs, "minix", fslen); return true; /* MINIX_SUPER_MAGIC */ - case 0x138f: bstrncpy(fs, "minix", fslen); return true; /* MINIX_SUPER_MAGIC2 */ - case 0x19800202: bstrncpy(fs, "mqueue", fslen); return true; /* MQUEUE_MAGIC */ - case 0x4d44: bstrncpy(fs, "msdos", fslen); return true; /* MSDOS_SUPER_MAGIC */ - case 0x564c: bstrncpy(fs, "ncpfs", fslen); return true; /* NCP_SUPER_MAGIC */ - case 0x6969: bstrncpy(fs, "nfs", fslen); return true; /* NFS_SUPER_MAGIC */ - case 0x9fa1: bstrncpy(fs, "openpromfs", fslen); return true; /* OPENPROM_SUPER_MAGIC */ - case 0x6f70726f: bstrncpy(fs, "oprofilefs", fslen); return true; /* OPROFILEFS_MAGIC */ - case 0xa0b4d889: bstrncpy(fs, "pfmfs", fslen); return true; /* PFMFS_MAGIC */ - case 0x50495045: bstrncpy(fs, "pipfs", fslen); return true; /* PIPEFS_MAGIC */ - case 0x002f: bstrncpy(fs, "qnx4", fslen); return true; /* QNX4_SUPER_MAGIC */ - case 0x858458f6: bstrncpy(fs, "ramfs", fslen); return true; /* RAMFS_MAGIC */ - case 0x7275: bstrncpy(fs, "romfs", fslen); return true; /* ROMFS_MAGIC */ - case 0x858458f6: bstrncpy(fs, "rootfs", fslen); return true; /* RAMFS_MAGIC */ - case 0x67596969: bstrncpy(fs, "rpc_pipefs", fslen); return true; /* RPCAUTH_GSSMAGIC */ - case 0x534F434B: bstrncpy(fs, "sockfs", fslen); return true; /* SOCKFS_MAGIC */ - case 0x012ff7b6: bstrncpy(fs, "sysv2", fslen); return true; /* SYSV2_SUPER_MAGIC */ - case 0x012ff7b5: bstrncpy(fs, "sysv4", fslen); return true; /* SYSV4_SUPER_MAGIC */ - case 0x858458f6: bstrncpy(fs, "tmpfs", fslen); return true; /* RAMFS_MAGIC */ - case 0x01021994: bstrncpy(fs, "tmpfs", fslen); return true; /* TMPFS_MAGIC */ - case 0x15013346: bstrncpy(fs, "udf", fslen); return true; /* UDF_SUPER_MAGIC */ - case 0x00011954: bstrncpy(fs, "ufs", fslen); return true; /* UFS_MAGIC */ - case 0xa501FCF5: bstrncpy(fs, "vxfs", fslen); return true; /* VXFS_SUPER_MAGIC */ - case 0x012ff7b4: bstrncpy(fs, "xenix", fslen); return true; /* XENIX_SUPER_MAGIC */ - case 0x012fd16d: bstrncpy(fs, "xiafs", fslen); return true; /* _XIAFS_SUPER_MAGIC */ -#endif + struct stat st; + mntent_cache_entry_t *mce; - default: - Dmsg2(10, "Unknown file system type \"0x%x\" for \"%s\".\n", st.f_type, - fname); - return false; + if (lstat(fname, &st) == 0) { + if ((mce = find_mntent_mapping(st.st_dev)) != NULL) { + bstrncpy(fs, mce->fstype, fslen); + return true; } + return false; } - Dmsg1(50, "statfs() failed for \"%s\"\n", fname); + + Dmsg1(50, "lstat() failed for \"%s\"\n", fname); return false; } @@ -228,37 +161,15 @@ bool fstype(const char *fname, char *fs, int fslen) bool fstype(const char *fname, char *fs, int fslen) { struct stat st; + if (lstat(fname, &st) == 0) { bstrncpy(fs, st.st_fstype, fslen); return true; } - Dmsg1(50, "lstat() failed for \"%s\"\n", fname); - return false; -} -#elif defined (__digital__) && defined (__unix__) /* Tru64 */ -/* Tru64 */ -#include -#include - -bool fstype(const char *fname, char *fs, int fslen) -{ - struct statfs st; - if (statfs((char *)fname, &st) == 0) { - switch (st.f_type) { - /* Known good values */ - case 0xa: bstrncpy(fs, "advfs", fslen); return true; /* Tru64 AdvFS */ - case 0xe: bstrncpy(fs, "nfs", fslen); return true; /* Tru64 NFS */ - default: - Dmsg2(10, "Unknown file system type \"0x%x\" for \"%s\".\n", st.f_type, - fname); - return false; - } - } - Dmsg1(50, "statfs() failed for \"%s\"\n", fname); + Dmsg1(50, "lstat() failed for \"%s\"\n", fname); return false; } -/* Tru64 */ #elif defined (HAVE_WIN32) /* Windows */ @@ -290,6 +201,7 @@ bool fstype(const char *fname, char *fs, int fslen) return result != 0; } + /* Windows */ #else /* No recognised OS */ @@ -307,6 +219,22 @@ bool fstype(const char *fname, char *fs, int fslen) } #endif +/* + * Compare function build on top of fstype, OS independent. + * + * bool fstype_equals(const char *fname, const char *fstypename); + */ +bool fstype_equals(const char *fname, const char *fstypename) +{ + char fs_typename[128]; + + if (fstype(fname, fs_typename, sizeof(fs_typename))) { + return bstrcmp(fs_typename, fstypename); + } + + return false; +} + #ifdef TEST_PROGRAM int main(int argc, char **argv) { diff --git a/bacula/src/findlib/protos.h b/bacula/src/findlib/protos.h index 6fafb62eaa..7e7510a722 100644 --- a/bacula/src/findlib/protos.h +++ b/bacula/src/findlib/protos.h @@ -81,6 +81,7 @@ bool makepath(ATTR *attr, const char *path, mode_t mode, /* from fstype.c */ bool fstype(const char *fname, char *fs, int fslen); +bool fstype_equals(const char *fname, const char *fstypename); /* from drivetype.c */ bool drivetype(const char *fname, char *fs, int fslen); diff --git a/bacula/src/lib/Makefile.in b/bacula/src/lib/Makefile.in index 4767f7df37..9776622cd5 100644 --- a/bacula/src/lib/Makefile.in +++ b/bacula/src/lib/Makefile.in @@ -45,8 +45,8 @@ INCLUDE_FILES = ../baconfig.h ../bacula.h ../bc_types.h \ berrno.h bits.h bpipe.h breg.h bregex.h \ bsock.h btime.h btimers.h crypto.h dlist.h \ fnmatch.h guid_to_name.h htable.h lex.h \ - lib.h md5.h mem_pool.h message.h openssl.h \ - plugins.h protos.h queue.h rblist.h \ + lib.h md5.h mem_pool.h message.h mntent_cache.h \ + openssl.h plugins.h protos.h queue.h rblist.h \ runscript.h rwlock.h serial.h sha1.h \ smartall.h status.h tls.h tree.h var.h \ waitq.h watchdog.h workq.h \ @@ -61,7 +61,7 @@ LIBBAC_SRCS = attr.c base64.c berrno.c bsys.c bget_msg.c \ bsock.c bpipe.c bsnprintf.c btime.c \ cram-md5.c crc32.c crypto.c daemon.c edit.c fnmatch.c \ guid_to_name.c hmac.c jcr.c lex.c alist.c dlist.c \ - md5.c message.c mem_pool.c openssl.c \ + md5.c message.c mem_pool.c mntent_cache.c openssl.c \ plugins.c priv.c queue.c bregex.c \ rwlock.c scan.c serial.c sha1.c \ signal.c smartall.c rblist.c tls.c tree.c \ diff --git a/bacula/src/lib/mntent_cache.c b/bacula/src/lib/mntent_cache.c new file mode 100644 index 0000000000..a564709c5f --- /dev/null +++ b/bacula/src/lib/mntent_cache.c @@ -0,0 +1,449 @@ +/* + Bacula® - The Network Backup Solution + + Copyright (C) 2009 Free Software Foundation Europe e.V. + + The main author of Bacula is Kern Sibbald, with contributions from + many others, a complete list can be found in the file AUTHORS. + This program is Free Software; you can redistribute it and/or + modify it under the terms of version two of the GNU General Public + License as published by the Free Software Foundation and included + in the file LICENSE. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. + + Bacula® is a registered trademark of Kern Sibbald. + The licensor of Bacula is the Free Software Foundation Europe + (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich, + Switzerland, email:ftf@fsfeurope.org. +*/ + +/** + * This code implements a cache with the current mounted filesystems for which + * its uses the mostly in kernel mount information and export the different OS + * specific interfaces using a generic interface. We use a hashed cache which is + * accessed using a hash on the device id and we keep the previous cache hit as + * most of the time we get called quite a lot with most of the time the same + * device so keeping the previous cache hit we have a very optimized code path. + * + * This interface is implemented for the following OS-es: + * + * - Linux + * - HPUX + * - DARWIN (OSX) + * - IRIX + * - AIX + * - OSF1 (True64) + * - Solaris + * + * Currently we only use this code for Linux and OSF1 based fstype determination. + * For the other OS-es we can use the fstype present in stat structure on those OS-es. + * + * This code replaces the big switch we used before based on SUPER_MAGIC present in + * the statfs(2) structure but which need extra code for each new filesystem added to + * the OS and for Linux that tends to be often as it has quite some different filesystems. + * This new implementation should eliminate this as we use the Linux /proc/mounts in kernel + * data which automatically adds any new filesystem when added to the kernel. + */ + +/* + * Marco van Wieringen, August 2009 + */ + +#include "bacula.h" +#include "mntent_cache.h" + +#include +#include +#include +#include +#include + +#if defined(HAVE_GETMNTENT) +#if defined(HAVE_LINUX_OS) || defined(HAVE_HPUX_OS) +#include +#elif defined(HAVE_SUN_OS) +#include +#endif /* HAVE_GETMNTENT */ +#elif defined(HAVE_GETMNTINFO) +#if defined(HAVE_DARWIN_OS) +#include +#include +#include +#else +#include +#include +#endif +#elif defined(HAVE_AIX_OS) +#include +#include +#elif defined(HAVE_OSF1_OS) +#include +#endif + +static char cache_initialized = 0; + +/** + * Protected data by mutex lock. + */ +static pthread_mutex_t mntent_cache_lock = PTHREAD_MUTEX_INITIALIZER; +static mntent_cache_entry_t *mntent_cache_entry_hashtable[NR_MNTENT_CACHE_ENTRIES]; +static mntent_cache_entry_t *previous_cache_hit = NULL; + +/** + * Simple hash function. + */ +static uint32_t mntent_hash_function(uint32_t dev) +{ + return (dev % NR_MNTENT_CACHE_ENTRIES); +} + +/** + * Add a new entry to the cache. + * This function should be called with a write lock on the mntent_cache. + */ +static void add_mntent_mapping(uint32_t dev, const char *special, const char *mountpoint, + const char *fstype, const char *mntopts) +{ + uint32_t hash; + mntent_cache_entry_t *mce; + + /** + * Select the correct hash bucket. + */ + hash = mntent_hash_function(dev); + + /** + * See if this is the first being put into the hash bucket. + */ + if (mntent_cache_entry_hashtable[hash] == (mntent_cache_entry_t *)NULL) { + mce = (mntent_cache_entry_t *)malloc(sizeof(mntent_cache_entry_t)); + memset((caddr_t)mce, 0, sizeof(mntent_cache_entry_t)); + mntent_cache_entry_hashtable[hash] = mce; + } else { + /** + * Walk the linked list in the hash bucket. + */ + for (mce = mntent_cache_entry_hashtable[hash]; mce->next != NULL; mce = mce->next) ; + mce->next = (mntent_cache_entry_t *)malloc(sizeof(mntent_cache_entry_t)); + mce = mce->next; + memset((caddr_t)mce, 0, sizeof(mntent_cache_entry_t)); + } + + mce->dev = dev; + mce->special = bstrdup(special); + mce->mountpoint = bstrdup(mountpoint); + mce->fstype = bstrdup(fstype); + if (mntopts) { + mce->mntopts = bstrdup(mntopts); + } +} + +/** + * OS specific function to load the different mntents into the cache. + * This function should be called with a write lock on the mntent_cache. + */ +static void refresh_mount_cache(void) +{ +#if defined(HAVE_GETMNTENT) + FILE *fp; + struct stat st; +#if defined(HAVE_LINUX_OS) || defined(HAVE_HPUX_OS) || defined(HAVE_IRIX_OS) + struct mntent *mnt; + +#if defined(HAVE_LINUX_OS) + if ((fp = setmntent("/proc/mounts", "r")) == (FILE *)NULL) { + if ((fp = setmntent(_PATH_MOUNTED, "r")) == (FILE *)NULL) { + return; + } + } +#elif defined(HAVE_HPUX_OS) + if ((fp = fopen(MNT_MNTTAB, "r")) == (FILE *)NULL) { + return; + } +#elif defined(HAVE_IRIX_OS) + if ((fp = setmntent(MOUNTED, "r")) == (FILE *)NULL) { + return; + } +#endif + + while ((mnt = getmntent(fp)) != (struct mntent *)NULL) { + if (stat(mnt->mnt_dir, &st) < 0) { + continue; + } + + add_mntent_mapping(st.st_dev, mnt->mnt_fsname, mnt->mnt_dir, mnt->mnt_type, mnt->mnt_opts); + } + + endmntent(fp); +#elif defined(HAVE_SUN_OS) + struct mnttab mnt; + + if ((fp = fopen(MNTTAB, "r")) == (FILE *)NULL) + return; + + while (getmntent(fp, &mnt) == 0) { + if (stat(mnt.mnt_mountp, &st) < 0) { + continue; + } + + add_mntent_mapping(st.st_dev, mnt.mnt_special, mnt.mnt_mountp, mnt.mnt_fstype, mnt.mnt_mntopts); + } + + fclose(fp); +#endif /* HAVE_SUN_OS */ +#elif defined(HAVE_GETMNTINFO) + int cnt; + struct stat st; +#if defined(HAVE_DARWIN_OS) + struct statfs *mntinfo; +#else + struct statvfs *mntinfo; +#endif + + if ((cnt = getmntinfo(&mntinfo, MNT_NOWAIT)) > 0) { + while (cnt > 0) { + if (stat(mntinfo->f_mntonname, &st) == 0) { + add_mntent_mapping(st.st_dev, + mntinfo->f_mntfromname, + mntinfo->f_mntonname, + mntinfo->f_fstypename, + NULL); + } + mntinfo++; + cnt--; + } + } +#elif defined(HAVE_AIX_OS) + int bufsize; + char *entries, *current; + struct vmount *vmp; + struct stat st; + struct vfs_ent *ve; + int n_entries, cnt; + + if (mntctl(MCTL_QUERY, sizeof(bufsize), (struct vmount *)&bufsize) != 0) { + return; + } + + entries = malloc(bufsize); + if ((n_entries = mntctl(MCTL_QUERY, bufsize, (struct vmount *) entries)) < 0) { + free(entries); + return; + } + + cnt = 0; + current = entries; + while (cnt < n_entries) { + vmp = (struct vmount *)current; + + if (stat(current + vmp->vmt_data[VMT_STUB].vmt_off, &st) < 0) { + continue; + } + + ve = getvfsbytype(vmp->vmt_gfstype); + if (ve && ve->vfsent_name) { + add_mntent_mapping(st.st_dev, + current + vmp->vmt_data[VMT_OBJECT].vmt_off, + current + vmp->vmt_data[VMT_STUB].vmt_off, + ve->vfsent_name, + current + vmp->vmt_data[VMT_ARGS].vmt_off); + } + current = current + vmp->vmt_length; + cnt++; + } + free(entries); +#elif defined(HAVE_OSF1_OS) + struct statfs *entries, *current; + struct stat st; + int n_entries, cnt; + int size; + + if ((n_entries = getfsstat((struct statfs *)0, 0L, MNT_NOWAIT)) < 0) { + return; + } + + size = (n_entries + 1) * sizeof(struct statfs); + entries = malloc(size); + + if ((n_entries = getfsstat(entries, size, MNT_NOWAIT)) < 0) { + free(entries); + return; + } + + cnt = 0; + current = entries; + while (cnt < n_entries) { + if (stat(current->f_mntonname, &st) < 0) { + continue; + } + add_mntent_mapping(st.st_dev, + current->f_mntfromname, + current->f_mntonname, + current->f_fstypename, + NULL); + current++; + cnt++; + } + free(stats); +#endif +} + +/** + * Clear the cache (either by flushing it or by initializing it.) + * This function should be called with a write lock on the mntent_cache. + */ +static void clear_mount_cache() +{ + uint32_t hash; + mntent_cache_entry_t *mce, *mce_next; + + if (cache_initialized == 0) { + /** + * Initialize the hash table. + */ + memset((caddr_t)mntent_cache_entry_hashtable, 0, NR_MNTENT_CACHE_ENTRIES * sizeof(mntent_cache_entry_t *)); + cache_initialized = 1; + } else { + /** + * Clear the previous_cache_hit. + */ + previous_cache_hit = NULL; + + /** + * Walk all hash buckets. + */ + for (hash = 0; hash < NR_MNTENT_CACHE_ENTRIES; hash++) { + /** + * Walk the content of this hash bucket. + */ + mce = mntent_cache_entry_hashtable[hash]; + mntent_cache_entry_hashtable[hash] = NULL; + while (mce != NULL) { + /** + * Save the pointer to the next entry. + */ + mce_next = mce->next; + + /** + * Free the structure. + */ + if (mce->mntopts) + free(mce->mntopts); + free(mce->fstype); + free(mce->mountpoint); + free(mce->special); + free(mce); + + mce = mce_next; + } + } + } +} + +/** + * Initialize the cache for use. + */ +static void initialize_mntent_cache(void) +{ + /** + * Lock the cache while we update it. + */ + P(mntent_cache_lock); + + /** + * Make sure the cache is empty (either by flushing it or by initializing it.) + */ + clear_mount_cache(); + + /** + * Refresh the cache. + */ + refresh_mount_cache(); + + /** + * We are done updating the cache. + */ + V(mntent_cache_lock); +} + +void preload_mntent_cache(void) +{ + initialize_mntent_cache(); +} + +void flush_mntent_cache(void) +{ + /** + * Lock the cache while we update it. + */ + P(mntent_cache_lock); + + /** + * Make sure the cache is empty (either by flushing it or by initializing it.) + */ + clear_mount_cache(); + + /** + * We are done updating the cache. + */ + V(mntent_cache_lock); +} + +/** + * Find a mapping in the cache. + */ +mntent_cache_entry_t *find_mntent_mapping(uint32_t dev) +{ + uint32_t hash; + mntent_cache_entry_t *mce; + + /** + * Initialize the cache if that was not done before. + */ + if (cache_initialized == 0) { + initialize_mntent_cache(); + } + + /** + * Shortcut when we get a request for the same device again. + */ + if (previous_cache_hit && previous_cache_hit->dev == dev) { + return previous_cache_hit; + } + + /** + * Lock the cache while we walk it. + */ + P(mntent_cache_lock); + + /** + * Select the correct hash bucket. + */ + hash = mntent_hash_function(dev); + + /** + * Walk the hash bucket. + */ + for (mce = mntent_cache_entry_hashtable[hash]; mce != NULL; mce = mce->next) { + if (mce->dev == dev) { + previous_cache_hit = mce; + V(mntent_cache_lock); + return mce; + } + } + + /** + * We are done walking the cache. + */ + V(mntent_cache_lock); + return NULL; +} diff --git a/bacula/src/lib/mntent_cache.h b/bacula/src/lib/mntent_cache.h new file mode 100644 index 0000000000..0f019966e8 --- /dev/null +++ b/bacula/src/lib/mntent_cache.h @@ -0,0 +1,52 @@ +/* + Bacula® - The Network Backup Solution + + Copyright (C) 2009 Free Software Foundation Europe e.V. + + The main author of Bacula is Kern Sibbald, with contributions from + many others, a complete list can be found in the file AUTHORS. + This program is Free Software; you can redistribute it and/or + modify it under the terms of version two of the GNU General Public + License as published by the Free Software Foundation and included + in the file LICENSE. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. + + Bacula® is a registered trademark of Kern Sibbald. + The licensor of Bacula is the Free Software Foundation Europe + (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich, + Switzerland, email:ftf@fsfeurope.org. +*/ + +/* + * Marco van Wieringen, August 2009 + */ + +#ifndef _MNTENT_CACHE_H +#define _MNTENT_CACHE_H 1 + +#define NR_MNTENT_CACHE_ENTRIES 32 + +struct mntent_cache_entry_t { + uint32_t dev; + char *special; + char *mountpoint; + char *fstype; + char *mntopts; + struct mntent_cache_entry_t *next; +}; + +mntent_cache_entry_t *find_mntent_mapping(uint32_t dev); + +void preload_mntent_cache(void); +void flush_mntent_cache(void); + +#endif /* _MNTENT_CACHE_H */ -- 2.39.5