2 Bacula® - The Network Backup Solution
4 Copyright (C) 2009-2010 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version three of the GNU Affero General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Affero General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
30 * This code implements a cache with the current mounted filesystems for which
31 * its uses the mostly in kernel mount information and export the different OS
32 * specific interfaces using a generic interface. We use a hashed cache which is
33 * accessed using a hash on the device id and we keep the previous cache hit as
34 * most of the time we get called quite a lot with most of the time the same
35 * device so keeping the previous cache hit we have a very optimized code path.
37 * This interface is implemented for the following OS-es:
47 * Currently we only use this code for Linux and OSF1 based fstype determination.
48 * For the other OS-es we can use the fstype present in stat structure on those OS-es.
50 * This code replaces the big switch we used before based on SUPER_MAGIC present in
51 * the statfs(2) structure but which need extra code for each new filesystem added to
52 * the OS and for Linux that tends to be often as it has quite some different filesystems.
53 * This new implementation should eliminate this as we use the Linux /proc/mounts in kernel
54 * data which automatically adds any new filesystem when added to the kernel.
58 * Marco van Wieringen, August 2009
62 #include "mntent_cache.h"
67 #include <sys/types.h>
70 #if defined(HAVE_GETMNTENT)
71 #if defined(HAVE_LINUX_OS) || defined(HAVE_HPUX_OS)
73 #elif defined(HAVE_SUN_OS)
74 #include <sys/mnttab.h>
75 #endif /* HAVE_GETMNTENT */
76 #elif defined(HAVE_GETMNTINFO)
77 #if defined(HAVE_DARWIN_OS) || defined(HAVE_FREEBSD_OS)
78 #include <sys/param.h>
79 #include <sys/ucred.h>
80 #include <sys/mount.h>
82 #include <sys/types.h>
83 #include <sys/statvfs.h>
85 #elif defined(HAVE_AIX_OS)
88 #elif defined(HAVE_OSF1_OS)
89 #include <sys/mount.h>
92 static char cache_initialized = 0;
95 * Protected data by mutex lock.
97 static pthread_mutex_t mntent_cache_lock = PTHREAD_MUTEX_INITIALIZER;
98 static mntent_cache_entry_t *mntent_cache_entry_hashtable[NR_MNTENT_CACHE_ENTRIES];
99 static mntent_cache_entry_t *previous_cache_hit = NULL;
102 * Simple hash function.
104 static uint32_t mntent_hash_function(uint32_t dev)
106 return (dev % NR_MNTENT_CACHE_ENTRIES);
110 * Add a new entry to the cache.
111 * This function should be called with a write lock on the mntent_cache.
113 static void add_mntent_mapping(uint32_t dev, const char *special, const char *mountpoint,
114 const char *fstype, const char *mntopts)
117 mntent_cache_entry_t *mce;
120 * Select the correct hash bucket.
122 hash = mntent_hash_function(dev);
125 * See if this is the first being put into the hash bucket.
127 if (mntent_cache_entry_hashtable[hash] == (mntent_cache_entry_t *)NULL) {
128 mce = (mntent_cache_entry_t *)malloc(sizeof(mntent_cache_entry_t));
129 memset((caddr_t)mce, 0, sizeof(mntent_cache_entry_t));
130 mntent_cache_entry_hashtable[hash] = mce;
133 * Walk the linked list in the hash bucket.
135 for (mce = mntent_cache_entry_hashtable[hash]; mce->next != NULL; mce = mce->next) ;
136 mce->next = (mntent_cache_entry_t *)malloc(sizeof(mntent_cache_entry_t));
138 memset((caddr_t)mce, 0, sizeof(mntent_cache_entry_t));
142 mce->special = bstrdup(special);
143 mce->mountpoint = bstrdup(mountpoint);
144 mce->fstype = bstrdup(fstype);
146 mce->mntopts = bstrdup(mntopts);
151 * OS specific function to load the different mntents into the cache.
152 * This function should be called with a write lock on the mntent_cache.
154 static void refresh_mount_cache(void)
156 #if defined(HAVE_GETMNTENT)
159 #if defined(HAVE_LINUX_OS) || defined(HAVE_HPUX_OS) || defined(HAVE_IRIX_OS)
162 #if defined(HAVE_LINUX_OS)
163 if ((fp = setmntent("/proc/mounts", "r")) == (FILE *)NULL) {
164 if ((fp = setmntent(_PATH_MOUNTED, "r")) == (FILE *)NULL) {
168 #elif defined(HAVE_HPUX_OS)
169 if ((fp = fopen(MNT_MNTTAB, "r")) == (FILE *)NULL) {
172 #elif defined(HAVE_IRIX_OS)
173 if ((fp = setmntent(MOUNTED, "r")) == (FILE *)NULL) {
178 while ((mnt = getmntent(fp)) != (struct mntent *)NULL) {
179 if (stat(mnt->mnt_dir, &st) < 0) {
183 add_mntent_mapping(st.st_dev, mnt->mnt_fsname, mnt->mnt_dir, mnt->mnt_type, mnt->mnt_opts);
187 #elif defined(HAVE_SUN_OS)
190 if ((fp = fopen(MNTTAB, "r")) == (FILE *)NULL)
193 while (getmntent(fp, &mnt) == 0) {
194 if (stat(mnt.mnt_mountp, &st) < 0) {
198 add_mntent_mapping(st.st_dev, mnt.mnt_special, mnt.mnt_mountp, mnt.mnt_fstype, mnt.mnt_mntopts);
202 #endif /* HAVE_SUN_OS */
203 #elif defined(HAVE_GETMNTINFO)
206 #if defined(HAVE_DARWIN_OS) || defined(HAVE_FREEBSD_OS)
207 struct statfs *mntinfo;
209 struct statvfs *mntinfo;
211 #if defined(ST_NOWAIT)
212 int flags = ST_NOWAIT;
213 #elif defined(MNT_NOWAIT)
214 int flags = MNT_NOWAIT;
219 if ((cnt = getmntinfo(&mntinfo, flags)) > 0) {
221 if (stat(mntinfo->f_mntonname, &st) == 0) {
222 add_mntent_mapping(st.st_dev,
223 mntinfo->f_mntfromname,
224 mntinfo->f_mntonname,
225 mntinfo->f_fstypename,
232 #elif defined(HAVE_AIX_OS)
234 char *entries, *current;
240 if (mntctl(MCTL_QUERY, sizeof(bufsize), (struct vmount *)&bufsize) != 0) {
244 entries = malloc(bufsize);
245 if ((n_entries = mntctl(MCTL_QUERY, bufsize, (struct vmount *) entries)) < 0) {
252 while (cnt < n_entries) {
253 vmp = (struct vmount *)current;
255 if (stat(current + vmp->vmt_data[VMT_STUB].vmt_off, &st) < 0) {
259 ve = getvfsbytype(vmp->vmt_gfstype);
260 if (ve && ve->vfsent_name) {
261 add_mntent_mapping(st.st_dev,
262 current + vmp->vmt_data[VMT_OBJECT].vmt_off,
263 current + vmp->vmt_data[VMT_STUB].vmt_off,
265 current + vmp->vmt_data[VMT_ARGS].vmt_off);
267 current = current + vmp->vmt_length;
271 #elif defined(HAVE_OSF1_OS)
272 struct statfs *entries, *current;
277 if ((n_entries = getfsstat((struct statfs *)0, 0L, MNT_NOWAIT)) < 0) {
281 size = (n_entries + 1) * sizeof(struct statfs);
282 entries = malloc(size);
284 if ((n_entries = getfsstat(entries, size, MNT_NOWAIT)) < 0) {
291 while (cnt < n_entries) {
292 if (stat(current->f_mntonname, &st) < 0) {
295 add_mntent_mapping(st.st_dev,
296 current->f_mntfromname,
297 current->f_mntonname,
298 current->f_fstypename,
308 * Clear the cache (either by flushing it or by initializing it.)
309 * This function should be called with a write lock on the mntent_cache.
311 static void clear_mount_cache()
314 mntent_cache_entry_t *mce, *mce_next;
316 if (cache_initialized == 0) {
318 * Initialize the hash table.
320 memset((caddr_t)mntent_cache_entry_hashtable, 0, NR_MNTENT_CACHE_ENTRIES * sizeof(mntent_cache_entry_t *));
321 cache_initialized = 1;
324 * Clear the previous_cache_hit.
326 previous_cache_hit = NULL;
329 * Walk all hash buckets.
331 for (hash = 0; hash < NR_MNTENT_CACHE_ENTRIES; hash++) {
333 * Walk the content of this hash bucket.
335 mce = mntent_cache_entry_hashtable[hash];
336 mntent_cache_entry_hashtable[hash] = NULL;
337 while (mce != NULL) {
339 * Save the pointer to the next entry.
341 mce_next = mce->next;
344 * Free the structure.
349 free(mce->mountpoint);
360 * Initialize the cache for use.
362 static void initialize_mntent_cache(void)
365 * Lock the cache while we update it.
367 P(mntent_cache_lock);
370 * Make sure the cache is empty (either by flushing it or by initializing it.)
377 refresh_mount_cache();
380 * We are done updating the cache.
382 V(mntent_cache_lock);
385 void preload_mntent_cache(void)
387 initialize_mntent_cache();
390 void flush_mntent_cache(void)
393 * Lock the cache while we update it.
395 P(mntent_cache_lock);
398 * Make sure the cache is empty (either by flushing it or by initializing it.)
403 * We are done updating the cache.
405 V(mntent_cache_lock);
409 * Find a mapping in the cache.
411 mntent_cache_entry_t *find_mntent_mapping(uint32_t dev)
414 mntent_cache_entry_t *mce;
417 * Initialize the cache if that was not done before.
419 if (cache_initialized == 0) {
420 initialize_mntent_cache();
424 * Shortcut when we get a request for the same device again.
426 if (previous_cache_hit && previous_cache_hit->dev == dev) {
427 return previous_cache_hit;
431 * Lock the cache while we walk it.
433 P(mntent_cache_lock);
436 * Select the correct hash bucket.
438 hash = mntent_hash_function(dev);
441 * Walk the hash bucket.
443 for (mce = mntent_cache_entry_hashtable[hash]; mce != NULL; mce = mce->next) {
444 if (mce->dev == dev) {
445 previous_cache_hit = mce;
446 V(mntent_cache_lock);
452 * We are done walking the cache.
454 V(mntent_cache_lock);