libmount: add reference counter to libmnt_table

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2013-08-21 16:07:25 +02:00
parent 6195f9e6fa
commit c9f1585e67
10 changed files with 100 additions and 50 deletions

View File

@ -289,6 +289,8 @@ libmnt_table
mnt_free_table
mnt_new_table
mnt_reset_table
mnt_ref_table
mnt_unref_table
mnt_new_table_from_dir
mnt_new_table_from_file
mnt_table_add_fs

View File

@ -81,7 +81,8 @@ struct libmnt_cache *mnt_new_cache(void)
* mnt_free_cache:
* @cache: pointer to struct libmnt_cache instance
*
* Deallocates the cache.
* Deallocates the cache. This function does not care about reference count. Don't
* use this function directly -- it's better to use use mnt_unref_cache().
*/
void mnt_free_cache(struct libmnt_cache *cache)
{

View File

@ -89,9 +89,7 @@ void mnt_free_context(struct libmnt_context *cxt)
free(cxt->fstype_pattern);
free(cxt->optstr_pattern);
if (!(cxt->flags & MNT_FL_EXTERN_FSTAB))
mnt_free_table(cxt->fstab);
mnt_unref_table(cxt->fstab);
mnt_unref_cache(cxt->cache);
mnt_context_clear_loopdev(cxt);
@ -137,7 +135,7 @@ int mnt_reset_context(struct libmnt_context *cxt)
fl = cxt->flags;
mnt_unref_fs(cxt->fs);
mnt_free_table(cxt->mtab);
mnt_unref_table(cxt->mtab);
free(cxt->helper);
free(cxt->orig_user);
@ -163,8 +161,6 @@ int mnt_reset_context(struct libmnt_context *cxt)
mnt_context_set_tabfilter(cxt, NULL, NULL);
/* restore non-resettable flags */
cxt->flags |= (fl & MNT_FL_EXTERN_FSTAB);
cxt->flags |= (fl & MNT_FL_EXTERN_CACHE);
cxt->flags |= (fl & MNT_FL_NOMTAB);
cxt->flags |= (fl & MNT_FL_FAKE);
cxt->flags |= (fl & MNT_FL_SLOPPY);
@ -891,7 +887,11 @@ int mnt_context_set_options_pattern(struct libmnt_context *cxt, const char *patt
*
* The mount context reads /etc/fstab to the private struct libmnt_table by default.
* This function allows to overwrite the private fstab with an external
* instance. Note that the external instance is not deallocated by mnt_free_context().
* instance.
*
* This function modify the @tb reference counter. This function does not set
* the cache for the @tb. You have to explicitly call mnt_table_set_cache(tb,
* mnt_context_get_cache(cxt));
*
* The fstab is used read-only and is not modified, it should be possible to
* share the fstab between more mount contexts (TODO: test it.)
@ -906,10 +906,10 @@ int mnt_context_set_fstab(struct libmnt_context *cxt, struct libmnt_table *tb)
assert(cxt);
if (!cxt)
return -EINVAL;
if (!(cxt->flags & MNT_FL_EXTERN_FSTAB))
mnt_free_table(cxt->fstab);
set_flag(cxt, MNT_FL_EXTERN_FSTAB, tb != NULL);
mnt_ref_table(tb); /* new */
mnt_unref_table(cxt->fstab); /* old */
cxt->fstab = tb;
return 0;
}
@ -936,16 +936,12 @@ int mnt_context_get_fstab(struct libmnt_context *cxt, struct libmnt_table **tb)
return -ENOMEM;
if (cxt->table_errcb)
mnt_table_set_parser_errcb(cxt->fstab, cxt->table_errcb);
cxt->flags &= ~MNT_FL_EXTERN_FSTAB;
mnt_table_set_cache(cxt->fstab, mnt_context_get_cache(cxt));
rc = mnt_table_parse_fstab(cxt->fstab, NULL);
if (rc)
return rc;
}
/* never touch an external fstab */
if (!(cxt->flags & MNT_FL_EXTERN_FSTAB))
mnt_table_set_cache(cxt->fstab, mnt_context_get_cache(cxt));
if (tb)
*tb = cxt->fstab;
return 0;
@ -980,13 +976,12 @@ int mnt_context_get_mtab(struct libmnt_context *cxt, struct libmnt_table **tb)
cxt->table_fltrcb,
cxt->table_fltrcb_data);
mnt_table_set_cache(cxt->mtab, mnt_context_get_cache(cxt));
rc = mnt_table_parse_mtab(cxt->mtab, cxt->mtab_path);
if (rc)
return rc;
}
mnt_table_set_cache(cxt->mtab, mnt_context_get_cache(cxt));
if (tb)
*tb = cxt->mtab;
@ -1057,7 +1052,7 @@ int mnt_context_get_table(struct libmnt_context *cxt,
rc = mnt_table_parse_file(*tb, filename);
if (rc) {
mnt_free_table(*tb);
mnt_unref_table(*tb);
return rc;
}
@ -1086,6 +1081,11 @@ int mnt_context_set_tables_errcb(struct libmnt_context *cxt,
if (!cxt)
return -EINVAL;
if (cxt->mtab)
mnt_table_set_parser_errcb(cxt->mtab, cb);
if (cxt->fstab)
mnt_table_set_parser_errcb(cxt->fstab, cb);
cxt->table_errcb = cb;
return 0;
}
@ -1099,8 +1099,9 @@ int mnt_context_set_tables_errcb(struct libmnt_context *cxt,
* function allows to overwrite the private cache with an external instance.
* This function increments cache reference counter.
*
* If the @cache argument is NULL, then the current private cache instance is
* reset.
* If the @cache argument is NULL, then the current cache instance is reset.
* This function apply the cache to fstab and mtab instances (if already
* exists).
*
* The old cache instance reference counter is de-incremented.
*
@ -1115,6 +1116,12 @@ int mnt_context_set_cache(struct libmnt_context *cxt, struct libmnt_cache *cache
mnt_unref_cache(cxt->cache); /* old */
cxt->cache = cache;
if (cxt->mtab)
mnt_table_set_cache(cxt->mtab, cache);
if (cxt->fstab)
mnt_table_set_cache(cxt->fstab, cache);
return 0;
}
@ -1133,9 +1140,9 @@ struct libmnt_cache *mnt_context_get_cache(struct libmnt_context *cxt)
return NULL;
if (!cxt->cache) {
cxt->cache = mnt_new_cache();
if (!cxt->cache)
return NULL;
struct libmnt_cache *cache = mnt_new_cache();
mnt_context_set_cache(cxt, cache);
mnt_unref_cache(cache);
}
return cxt->cache;
}

View File

@ -415,6 +415,9 @@ extern struct libmnt_table *mnt_new_table(void)
__ul_attribute__((warn_unused_result));
extern void mnt_free_table(struct libmnt_table *tb);
extern void mnt_ref_table(struct libmnt_table *table);
extern void mnt_unref_table(struct libmnt_table *table);
extern int mnt_reset_table(struct libmnt_table *tb);
extern int mnt_table_get_nents(struct libmnt_table *tb);
extern int mnt_table_is_empty(struct libmnt_table *tb);

View File

@ -267,6 +267,7 @@ global:
mnt_fs_set_comment;
mnt_ref_cache;
mnt_ref_fs;
mnt_ref_table;
mnt_table_append_intro_comment;
mnt_table_append_trailing_comment;
mnt_table_enable_comments;
@ -284,4 +285,5 @@ global:
mnt_table_write_file;
mnt_unref_cache;
mnt_unref_fs;
mnt_unref_table;
} MOUNT_2.23;

View File

@ -276,6 +276,7 @@ struct libmnt_fs {
struct libmnt_table {
int fmt; /* MNT_FMT_* file format */
int nents; /* number of entries */
int refcount; /* reference counter */
int comms; /* enable/disable comment parsing */
char *comm_intro; /* First comment in file */
char *comm_tail; /* Last comment in file */
@ -391,9 +392,6 @@ struct libmnt_context
#define MNT_FL_FORK (1 << 12)
#define MNT_FL_NOSWAPMATCH (1 << 13)
#define MNT_FL_EXTERN_FSTAB (1 << 16) /* cxt->fstab is not private */
#define MNT_FL_EXTERN_CACHE (1 << 17) /* cxt->cache is not private */
#define MNT_FL_MOUNTDATA (1 << 20)
#define MNT_FL_TAB_APPLIED (1 << 21) /* mtab/fstab merged to cxt->fs */
#define MNT_FL_MOUNTFLAGS_MERGED (1 << 22) /* MS_* flags was read from optstr */

View File

@ -66,7 +66,7 @@ struct libmnt_table *mnt_new_table(void)
return NULL;
DBG(TAB, mnt_debug_h(tb, "alloc"));
tb->refcount = 1;
INIT_LIST_HEAD(&tb->ents);
return tb;
}
@ -97,11 +97,47 @@ int mnt_reset_table(struct libmnt_table *tb)
return 0;
}
/**
* mnt_ref_table:
* @tb: table pointer
*
* Increments reference counter.
*/
void mnt_ref_table(struct libmnt_table *tb)
{
if (tb) {
tb->refcount++;
/*DBG(FS, mnt_debug_h(tb, "ref=%d", tb->refcount));*/
}
}
/**
* mnt_unref_table:
* @tb: table pointer
*
* De-increments reference counter, on zero the FS is automatically
* deallocated by mnt_free_table().
*/
void mnt_unref_table(struct libmnt_table *tb)
{
if (tb) {
tb->refcount--;
/*DBG(FS, mnt_debug_h(tb, "unref=%d", tb->refcount));*/
if (tb->refcount <= 0)
mnt_free_table(tb);
}
}
/**
* mnt_free_table:
* @tb: tab pointer
*
* Deallocates tab struct and all entries.
* Deallocates the table. This function does not care about reference count. Don't
* use this function directly -- it's better to use use mnt_unref_table().
*
* The table entries (filesystems) are unrefrenced by mnt_reset_table() and
* cache by mnt_unref_cache().
*/
void mnt_free_table(struct libmnt_table *tb)
{
@ -109,9 +145,11 @@ void mnt_free_table(struct libmnt_table *tb)
return;
mnt_reset_table(tb);
mnt_unref_cache(tb->cache);
WARN_REFCOUNT(TAB, tb, tb->refcount);
DBG(TAB, mnt_debug_h(tb, "free"));
mnt_unref_cache(tb->cache);
free(tb->comm_intro);
free(tb->comm_tail);
free(tb);
@ -551,7 +589,6 @@ int mnt_table_next_child_fs(struct libmnt_table *tb, struct libmnt_iter *itr,
* const char *dir = mnt_fs_get_target(fs);
* printf("mount point: %s\n", dir);
* }
* mnt_free_table(fi);
* </programlisting>
* </informalexample>
*
@ -1370,7 +1407,7 @@ struct libmnt_table *create_table(const char *file, int comments)
return tb;
err:
fprintf(stderr, "%s: parsing failed\n", file);
mnt_free_table(tb);
mnt_unref_table(tb);
return NULL;
}
@ -1400,7 +1437,7 @@ int test_copy_fs(struct libmnt_test *ts, int argc, char *argv[])
mnt_unref_fs(fs);
rc = 0;
done:
mnt_free_table(tb);
mnt_unref_table(tb);
return rc;
}
@ -1436,7 +1473,7 @@ int test_parse(struct libmnt_test *ts, int argc, char *argv[])
rc = 0;
done:
mnt_free_iter(itr);
mnt_free_table(tb);
mnt_unref_table(tb);
return rc;
}
@ -1478,7 +1515,7 @@ int test_find(struct libmnt_test *ts, int argc, char *argv[], int dr)
rc = 0;
}
done:
mnt_free_table(tb);
mnt_unref_table(tb);
return rc;
}
@ -1515,7 +1552,7 @@ int test_find_pair(struct libmnt_test *ts, int argc, char *argv[])
mnt_fs_print_debug(fs, stdout);
rc = 0;
done:
mnt_free_table(tb);
mnt_unref_table(tb);
return rc;
}
@ -1542,7 +1579,7 @@ int test_find_mountpoint(struct libmnt_test *ts, int argc, char *argv[])
mnt_fs_print_debug(fs, stdout);
rc = 0;
done:
mnt_free_table(tb);
mnt_unref_table(tb);
return rc;
}
@ -1587,8 +1624,8 @@ static int test_is_mounted(struct libmnt_test *ts, int argc, char *argv[])
rc = 0;
done:
mnt_free_table(tb);
mnt_free_table(fstab);
mnt_unref_table(tb);
mnt_unref_table(fstab);
mnt_free_iter(itr);
return rc;
}

View File

@ -355,8 +355,8 @@ int test_diff(struct libmnt_test *ts, int argc, char *argv[])
rc = 0;
done:
mnt_free_table(tb_old);
mnt_free_table(tb_new);
mnt_unref_table(tb_old);
mnt_unref_table(tb_new);
mnt_free_tabdiff(diff);
mnt_free_iter(itr);
return rc;

View File

@ -821,7 +821,7 @@ struct libmnt_table *__mnt_new_table_from_file(const char *filename, int fmt)
if (tb) {
tb->fmt = fmt;
if (mnt_table_parse_file(tb, filename) != 0) {
mnt_free_table(tb);
mnt_unref_table(tb);
tb = NULL;
}
}
@ -860,7 +860,7 @@ struct libmnt_table *mnt_new_table_from_dir(const char *dirname)
return NULL;
tb = mnt_new_table();
if (tb && mnt_table_parse_dir(tb, dirname) != 0) {
mnt_free_table(tb);
mnt_unref_table(tb);
tb = NULL;
}
return tb;
@ -1103,6 +1103,6 @@ int mnt_table_parse_mtab(struct libmnt_table *tb, const char *filename)
mnt_table_merge_user_fs(tb, u_fs);
}
mnt_free_table(u_tb);
mnt_unref_table(u_tb);
return 0;
}

View File

@ -71,7 +71,7 @@ void mnt_free_update(struct libmnt_update *upd)
DBG(UPDATE, mnt_debug_h(upd, "free"));
mnt_unref_fs(upd->fs);
mnt_free_table(upd->mountinfo);
mnt_unref_table(upd->mountinfo);
free(upd->target);
free(upd->filename);
free(upd);
@ -698,7 +698,7 @@ static int update_add_entry(struct libmnt_update *upd, struct libmnt_lock *lc)
if (lc)
mnt_unlock_file(lc);
mnt_free_table(tb);
mnt_unref_table(tb);
return rc;
}
@ -729,7 +729,7 @@ static int update_remove_entry(struct libmnt_update *upd, struct libmnt_lock *lc
if (lc)
mnt_unlock_file(lc);
mnt_free_table(tb);
mnt_unref_table(tb);
return rc;
}
@ -761,7 +761,7 @@ static int update_modify_target(struct libmnt_update *upd, struct libmnt_lock *l
if (lc)
mnt_unlock_file(lc);
mnt_free_table(tb);
mnt_unref_table(tb);
return rc;
}
@ -803,7 +803,7 @@ static int update_modify_options(struct libmnt_update *upd, struct libmnt_lock *
if (lc)
mnt_unlock_file(lc);
mnt_free_table(tb);
mnt_unref_table(tb);
return rc;
}
@ -968,7 +968,7 @@ static int test_replace(struct libmnt_test *ts, int argc, char *argv[])
mnt_unref_fs(fs);
rc = mnt_table_replace_file(tb, mnt_get_fstab_path());
mnt_free_table(tb);
mnt_unref_table(tb);
return rc;
}