2 Bacula® - The Network Backup Solution
4 Copyright (C) 2009-2011 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) || defined(HAVE_AIX_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_OPENBSD_OS)
78 #include <sys/param.h>
79 #include <sys/mount.h>
80 #elif defined(HAVE_NETBSD_OS)
81 #include <sys/types.h>
82 #include <sys/statvfs.h>
84 #include <sys/param.h>
85 #include <sys/ucred.h>
86 #include <sys/mount.h>
88 #elif defined(HAVE_AIX_OS)
91 #elif defined(HAVE_OSF1_OS)
92 #include <sys/mount.h>
96 * Protected data by mutex lock.
98 static pthread_mutex_t mntent_cache_lock = PTHREAD_MUTEX_INITIALIZER;
99 static mntent_cache_entry_t *previous_cache_hit = NULL;
100 static htable *mntent_cache_entry_hashtable = NULL;
103 * Last time a rescan of the mountlist took place.
105 static time_t last_rescan = 0;
108 * Add a new entry to the cache.
109 * This function should be called with a write lock on the mntent_cache.
111 static inline void add_mntent_mapping(uint32_t dev, const char *special, const char *mountpoint,
112 const char *fstype, const char *mntopts)
115 mntent_cache_entry_t *mce;
118 * Calculate the length of all strings so we can allocate the buffer
119 * as one big chunk of memory using the hash_malloc method.
121 len = strlen(special) + 1;
122 len += strlen(mountpoint) + 1;
123 len += strlen(fstype) + 1;
125 len += strlen(mntopts) + 1;
129 * We allocate all members of the hash entry in the same memory chunk.
131 mce = (mntent_cache_entry_t *)mntent_cache_entry_hashtable->hash_malloc(sizeof(mntent_cache_entry_t) + len);
134 mce->special = (char *)mce + sizeof(mntent_cache_entry_t);
135 strcpy(mce->special, special);
137 mce->mountpoint = mce->special + strlen(mce->special) + 1;
138 strcpy(mce->mountpoint, mountpoint);
140 mce->fstype = mce->mountpoint + strlen(mce->mountpoint) + 1;
141 strcpy(mce->fstype, fstype);
144 mce->mntopts = mce->fstype + strlen(mce->fstype) + 1;
145 strcpy(mce->mntopts, mntopts);
150 mntent_cache_entry_hashtable->insert(mce->dev, mce);
154 * OS specific function to load the different mntents into the cache.
155 * This function should be called with a write lock on the mntent_cache.
157 static void refresh_mount_cache(void)
159 #if defined(HAVE_GETMNTENT)
162 #if defined(HAVE_LINUX_OS) || defined(HAVE_HPUX_OS) || defined(HAVE_IRIX_OS) || defined(HAVE_AIX_OS)
165 #if defined(HAVE_LINUX_OS)
166 if ((fp = setmntent("/proc/mounts", "r")) == (FILE *)NULL) {
167 if ((fp = setmntent(_PATH_MOUNTED, "r")) == (FILE *)NULL) {
171 #elif defined(HAVE_HPUX_OS)
172 if ((fp = fopen(MNT_MNTTAB, "r")) == (FILE *)NULL) {
175 #elif defined(HAVE_IRIX_OS)
176 if ((fp = setmntent(MOUNTED, "r")) == (FILE *)NULL) {
179 #elif defined(HAVE_AIX_OS)
180 if ((fp = setmntent(MNTTAB, "r")) == (FILE *)NULL) {
185 while ((mnt = getmntent(fp)) != (struct mntent *)NULL) {
186 if (stat(mnt->mnt_dir, &st) < 0) {
190 add_mntent_mapping(st.st_dev, mnt->mnt_fsname, mnt->mnt_dir, mnt->mnt_type, mnt->mnt_opts);
194 #elif defined(HAVE_SUN_OS)
197 if ((fp = fopen(MNTTAB, "r")) == (FILE *)NULL)
200 while (getmntent(fp, &mnt) == 0) {
201 if (stat(mnt.mnt_mountp, &st) < 0) {
205 add_mntent_mapping(st.st_dev, mnt.mnt_special, mnt.mnt_mountp, mnt.mnt_fstype, mnt.mnt_mntopts);
209 #endif /* HAVE_SUN_OS */
210 #elif defined(HAVE_GETMNTINFO)
213 #if defined(HAVE_NETBSD_OS)
214 struct statvfs *mntinfo;
216 struct statfs *mntinfo;
218 #if defined(ST_NOWAIT)
219 int flags = ST_NOWAIT;
220 #elif defined(MNT_NOWAIT)
221 int flags = MNT_NOWAIT;
226 if ((cnt = getmntinfo(&mntinfo, flags)) > 0) {
228 if (stat(mntinfo->f_mntonname, &st) == 0) {
229 add_mntent_mapping(st.st_dev,
230 mntinfo->f_mntfromname,
231 mntinfo->f_mntonname,
232 mntinfo->f_fstypename,
239 #elif defined(HAVE_AIX_OS)
241 char *entries, *current;
247 if (mntctl(MCTL_QUERY, sizeof(bufsize), (struct vmount *)&bufsize) != 0) {
251 entries = malloc(bufsize);
252 if ((n_entries = mntctl(MCTL_QUERY, bufsize, (struct vmount *) entries)) < 0) {
259 while (cnt < n_entries) {
260 vmp = (struct vmount *)current;
262 if (stat(current + vmp->vmt_data[VMT_STUB].vmt_off, &st) < 0) {
266 ve = getvfsbytype(vmp->vmt_gfstype);
267 if (ve && ve->vfsent_name) {
268 add_mntent_mapping(st.st_dev,
269 current + vmp->vmt_data[VMT_OBJECT].vmt_off,
270 current + vmp->vmt_data[VMT_STUB].vmt_off,
272 current + vmp->vmt_data[VMT_ARGS].vmt_off);
274 current = current + vmp->vmt_length;
278 #elif defined(HAVE_OSF1_OS)
279 struct statfs *entries, *current;
284 if ((n_entries = getfsstat((struct statfs *)0, 0L, MNT_NOWAIT)) < 0) {
288 size = (n_entries + 1) * sizeof(struct statfs);
289 entries = malloc(size);
291 if ((n_entries = getfsstat(entries, size, MNT_NOWAIT)) < 0) {
298 while (cnt < n_entries) {
299 if (stat(current->f_mntonname, &st) < 0) {
302 add_mntent_mapping(st.st_dev,
303 current->f_mntfromname,
304 current->f_mntonname,
305 current->f_fstypename,
315 * Clear the cache (either by flushing it or by initializing it.)
316 * This function should be called with a write lock on the mntent_cache.
318 static void clear_mount_cache()
320 mntent_cache_entry_t *mce = NULL;
322 if (!mntent_cache_entry_hashtable) {
324 * Initialize the hash table.
326 mntent_cache_entry_hashtable = (htable *)malloc(sizeof(htable));
327 mntent_cache_entry_hashtable->init(mce, &mce->link,
328 NR_MNTENT_CACHE_ENTRIES,
329 NR_MNTENT_HTABLE_PAGES);
332 * Clear the previous_cache_hit.
334 previous_cache_hit = NULL;
337 * Destroy the current content and (re)initialize the hashtable.
339 mntent_cache_entry_hashtable->destroy();
340 mntent_cache_entry_hashtable->init(mce, &mce->link,
341 NR_MNTENT_CACHE_ENTRIES,
342 NR_MNTENT_HTABLE_PAGES);
347 * Initialize the cache for use.
349 static void initialize_mntent_cache(void)
352 * Lock the cache while we update it.
354 P(mntent_cache_lock);
357 * Make sure the cache is empty (either by flushing it or by initializing it.)
364 refresh_mount_cache();
367 * We are done updating the cache.
369 V(mntent_cache_lock);
372 void flush_mntent_cache(void)
375 * Lock the cache while we update it.
377 P(mntent_cache_lock);
380 * Make sure the cache is empty (either by flushing it or by initializing it.)
385 * We are done updating the cache.
387 V(mntent_cache_lock);
391 * Find a mapping in the cache.
393 mntent_cache_entry_t *find_mntent_mapping(uint32_t dev)
395 mntent_cache_entry_t *mce = NULL;
399 * Initialize the cache if that was not done before.
401 if (!mntent_cache_entry_hashtable) {
402 initialize_mntent_cache();
403 last_rescan = time(NULL);
406 * We rescan the mountlist when called when more then
407 * MNTENT_RESCAN_INTERVAL seconds have past since the
408 * last rescan. This way we never work with data older
409 * then MNTENT_RESCAN_INTERVAL seconds.
412 if ((now - last_rescan) > MNTENT_RESCAN_INTERVAL) {
413 initialize_mntent_cache();
418 * Shortcut when we get a request for the same device again.
420 if (previous_cache_hit && previous_cache_hit->dev == dev) {
421 return previous_cache_hit;
425 * Lock the cache while we walk it.
427 P(mntent_cache_lock);
429 mce = (mntent_cache_entry_t *)mntent_cache_entry_hashtable->lookup(dev);
432 * If we fail to lookup the mountpoint its probably a mountpoint added
433 * after we did our initial scan. Lets rescan the mountlist and try
438 * Make sure the cache is empty (either by flushing it or by initializing it.)
445 refresh_mount_cache();
447 mce = (mntent_cache_entry_t *)mntent_cache_entry_hashtable->lookup(dev);
451 * Store the last successfull lookup as the previous_cache_hit.
454 previous_cache_hit = mce;
458 * We are done walking the cache.
460 V(mntent_cache_lock);