]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/mntent_cache.c
14422c040356a842770a518d62fb564e17a43895
[bacula/bacula] / bacula / src / lib / mntent_cache.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2009-2010 Free Software Foundation Europe e.V.
5
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
11    in the file LICENSE.
12
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.
17
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
21    02110-1301, USA.
22
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.
27 */
28
29 /**
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.
36  *
37  * This interface is implemented for the following OS-es:
38  *
39  * - Linux
40  * - HPUX
41  * - DARWIN (OSX)
42  * - IRIX
43  * - AIX
44  * - OSF1 (Tru64)
45  * - Solaris
46  *
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.
49  *
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.
55  */
56
57 /*
58  *  Marco van Wieringen, August 2009
59  */
60
61 #include "bacula.h"
62 #include "mntent_cache.h"
63
64 #include <errno.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <sys/types.h>
68 #include <sys/stat.h>
69
70 #if defined(HAVE_GETMNTENT)
71 #if defined(HAVE_LINUX_OS) || defined(HAVE_HPUX_OS) || defined(HAVE_AIX_OS)
72 #include <mntent.h>
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>
83 #else
84 #include <sys/param.h>
85 #include <sys/ucred.h>
86 #include <sys/mount.h>
87 #endif
88 #elif defined(HAVE_AIX_OS)
89 #include <fshelp.h>
90 #include <sys/vfs.h>
91 #elif defined(HAVE_OSF1_OS)
92 #include <sys/mount.h>
93 #endif
94
95 /*
96  * Protected data by mutex lock.
97  */
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;
101
102 /**
103  * Add a new entry to the cache.
104  * This function should be called with a write lock on the mntent_cache.
105  */
106 static void add_mntent_mapping(uint32_t dev, const char *special, const char *mountpoint,
107                                const char *fstype, const char *mntopts)
108 {
109    int len;
110    mntent_cache_entry_t *mce;
111
112    /*
113     * Calculate the length of all strings so we can allocate the buffer
114     * as one big chunk of memory using the hash_malloc method.
115     */
116    len = strlen(special) + 1;
117    len += strlen(mountpoint) + 1;
118    len += strlen(fstype) + 1;
119    if (mntopts) {
120       len += strlen(mntopts) + 1;
121    }
122
123    /*
124     * We allocate all members of the hash entry in the same memory chunk.
125     */
126    mce = (mntent_cache_entry_t *)mntent_cache_entry_hashtable->hash_malloc(sizeof(mntent_cache_entry_t) + len);
127    mce->dev = dev;
128
129    mce->special = (char *)mce + sizeof(mntent_cache_entry_t);
130    strcpy(mce->special, special);
131
132    mce->mountpoint = mce->special + strlen(mce->special) + 1;
133    strcpy(mce->mountpoint, mountpoint);
134
135    mce->fstype = mce->mountpoint + strlen(mce->mountpoint) + 1;
136    strcpy(mce->fstype, fstype);
137
138    if (mntopts) {
139       mce->mntopts = mce->fstype + strlen(mce->fstype) + 1;
140       strcpy(mce->mntopts, mntopts);
141    } else {
142       mce->mntopts = NULL;
143    }
144
145    mntent_cache_entry_hashtable->insert(mce->dev, mce);
146 }
147
148 /**
149  * OS specific function to load the different mntents into the cache.
150  * This function should be called with a write lock on the mntent_cache.
151  */
152 static void refresh_mount_cache(void)
153 {
154 #if defined(HAVE_GETMNTENT)
155    FILE *fp;
156    struct stat st;
157 #if defined(HAVE_LINUX_OS) || defined(HAVE_HPUX_OS) || defined(HAVE_IRIX_OS) || defined(HAVE_AIX_OS)
158    struct mntent *mnt;
159
160 #if defined(HAVE_LINUX_OS)
161    if ((fp = setmntent("/proc/mounts", "r")) == (FILE *)NULL) {
162       if ((fp = setmntent(_PATH_MOUNTED, "r")) == (FILE *)NULL) {
163          return;
164       }
165    }
166 #elif defined(HAVE_HPUX_OS)
167    if ((fp = fopen(MNT_MNTTAB, "r")) == (FILE *)NULL) {
168       return;
169    }
170 #elif defined(HAVE_IRIX_OS)
171    if ((fp = setmntent(MOUNTED, "r")) == (FILE *)NULL) {
172       return;
173    }
174 #elif defined(HAVE_AIX_OS)
175    if ((fp = setmntent(MNTTAB, "r")) == (FILE *)NULL) {
176       return;
177    }
178 #endif
179
180    while ((mnt = getmntent(fp)) != (struct mntent *)NULL) {
181       if (stat(mnt->mnt_dir, &st) < 0) {
182          continue;
183       }
184
185       add_mntent_mapping(st.st_dev, mnt->mnt_fsname, mnt->mnt_dir, mnt->mnt_type, mnt->mnt_opts);
186    }
187
188    endmntent(fp);
189 #elif defined(HAVE_SUN_OS)
190    struct mnttab mnt;
191
192    if ((fp = fopen(MNTTAB, "r")) == (FILE *)NULL)
193       return;
194
195    while (getmntent(fp, &mnt) == 0) {
196       if (stat(mnt.mnt_mountp, &st) < 0) {
197          continue;
198       }
199
200       add_mntent_mapping(st.st_dev, mnt.mnt_special, mnt.mnt_mountp, mnt.mnt_fstype, mnt.mnt_mntopts);
201    }
202
203    fclose(fp);
204 #endif /* HAVE_SUN_OS */
205 #elif defined(HAVE_GETMNTINFO)
206    int cnt;
207    struct stat st;
208 #if defined(HAVE_NETBSD_OS)
209    struct statvfs *mntinfo;
210 #else
211    struct statfs *mntinfo;
212 #endif
213 #if defined(ST_NOWAIT)
214    int flags = ST_NOWAIT;
215 #elif defined(MNT_NOWAIT)
216    int flags = MNT_NOWAIT;
217 #else
218    int flags = 0;
219 #endif
220
221    if ((cnt = getmntinfo(&mntinfo, flags)) > 0) {
222       while (cnt > 0) {
223          if (stat(mntinfo->f_mntonname, &st) == 0) {
224             add_mntent_mapping(st.st_dev,
225                                mntinfo->f_mntfromname,
226                                mntinfo->f_mntonname,
227                                mntinfo->f_fstypename,
228                                NULL);
229          }
230          mntinfo++;
231          cnt--;
232       }
233    }
234 #elif defined(HAVE_AIX_OS)
235    int bufsize;
236    char *entries, *current;
237    struct vmount *vmp;
238    struct stat st;
239    struct vfs_ent *ve;
240    int n_entries, cnt;
241
242    if (mntctl(MCTL_QUERY, sizeof(bufsize), (struct vmount *)&bufsize) != 0) {
243       return;
244    }
245
246    entries = malloc(bufsize);
247    if ((n_entries = mntctl(MCTL_QUERY, bufsize, (struct vmount *) entries)) < 0) {
248       free(entries);
249       return;
250    }
251
252    cnt = 0;
253    current = entries;
254    while (cnt < n_entries) {
255       vmp = (struct vmount *)current;
256
257       if (stat(current + vmp->vmt_data[VMT_STUB].vmt_off, &st) < 0) {
258          continue;
259       }
260
261       ve = getvfsbytype(vmp->vmt_gfstype);
262       if (ve && ve->vfsent_name) {
263          add_mntent_mapping(st.st_dev,
264                             current + vmp->vmt_data[VMT_OBJECT].vmt_off,
265                             current + vmp->vmt_data[VMT_STUB].vmt_off,
266                             ve->vfsent_name,
267                             current + vmp->vmt_data[VMT_ARGS].vmt_off);
268       }
269       current = current + vmp->vmt_length;
270       cnt++;
271    }
272    free(entries);
273 #elif defined(HAVE_OSF1_OS)
274    struct statfs *entries, *current;
275    struct stat st;
276    int n_entries, cnt;
277    int size;
278
279    if ((n_entries = getfsstat((struct statfs *)0, 0L, MNT_NOWAIT)) < 0) {
280       return;
281    }
282
283    size = (n_entries + 1) * sizeof(struct statfs);
284    entries = malloc(size);
285
286    if ((n_entries = getfsstat(entries, size, MNT_NOWAIT)) < 0) {
287       free(entries);
288       return;
289    }
290
291    cnt = 0;
292    current = entries;
293    while (cnt < n_entries) {
294       if (stat(current->f_mntonname, &st) < 0) {
295          continue;
296       }
297       add_mntent_mapping(st.st_dev,
298                          current->f_mntfromname,
299                          current->f_mntonname,
300                          current->f_fstypename,
301                          NULL);
302       current++;
303       cnt++;
304    }
305    free(stats);
306 #endif
307 }
308
309 /**
310  * Clear the cache (either by flushing it or by initializing it.)
311  * This function should be called with a write lock on the mntent_cache.
312  */
313 static void clear_mount_cache()
314 {
315    mntent_cache_entry_t *mce = NULL;
316
317    if (!mntent_cache_entry_hashtable) {
318       /**
319        * Initialize the hash table.
320        */
321       mntent_cache_entry_hashtable = (htable *)malloc(sizeof(htable));
322       mntent_cache_entry_hashtable->init(mce, &mce->link,
323                                          NR_MNTENT_CACHE_ENTRIES,
324                                          NR_MNTENT_HTABLE_PAGES);
325    } else {
326       /**
327        * Clear the previous_cache_hit.
328        */
329       previous_cache_hit = NULL;
330
331       /**
332        * Destroy the current content and (re)initialize the hashtable.
333        */
334       mntent_cache_entry_hashtable->destroy();
335       mntent_cache_entry_hashtable->init(mce, &mce->link,
336                                          NR_MNTENT_CACHE_ENTRIES,
337                                          NR_MNTENT_HTABLE_PAGES);
338    }
339 }
340
341 /**
342  * Initialize the cache for use.
343  */
344 static void initialize_mntent_cache(void)
345 {
346    /**
347     * Lock the cache while we update it.
348     */
349    P(mntent_cache_lock);
350
351    /**
352     * Make sure the cache is empty (either by flushing it or by initializing it.)
353     */
354    clear_mount_cache();
355
356    /**
357     * Refresh the cache.
358     */
359    refresh_mount_cache();
360
361    /**
362     * We are done updating the cache.
363     */
364    V(mntent_cache_lock);
365 }
366
367 void preload_mntent_cache(void)
368 {
369    initialize_mntent_cache();
370 }
371
372 void flush_mntent_cache(void)
373 {
374    /**
375     * Lock the cache while we update it.
376     */
377    P(mntent_cache_lock);
378
379    /**
380     * Make sure the cache is empty (either by flushing it or by initializing it.)
381     */
382    clear_mount_cache();
383
384    /**
385     * We are done updating the cache.
386     */
387    V(mntent_cache_lock);
388 }
389
390 /**
391  * Find a mapping in the cache.
392  */
393 mntent_cache_entry_t *find_mntent_mapping(uint32_t dev)
394 {
395    mntent_cache_entry_t *mce = NULL;
396
397    /**
398     * Initialize the cache if that was not done before.
399     */
400    if (!mntent_cache_entry_hashtable) {
401       initialize_mntent_cache();
402    }
403
404    /**
405     * Shortcut when we get a request for the same device again.
406     */
407    if (previous_cache_hit && previous_cache_hit->dev == dev) {
408       return previous_cache_hit;
409    }
410
411    /**
412     * Lock the cache while we walk it.
413     */
414    P(mntent_cache_lock);
415
416    mce = (mntent_cache_entry_t *)mntent_cache_entry_hashtable->lookup(dev);
417    if (mce) {
418       previous_cache_hit = mce;
419    }
420
421    /**
422     * We are done walking the cache.
423     */
424    V(mntent_cache_lock);
425    return mce;
426 }