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
* 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)
{
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);
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);
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);
*
* 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.)
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;
}
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;
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;
rc = mnt_table_parse_file(*tb, filename);
if (rc) {
- mnt_free_table(*tb);
+ mnt_unref_table(*tb);
return rc;
}
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;
}
* 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.
*
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;
}
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;
}
__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);
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;
mnt_table_write_file;
mnt_unref_cache;
mnt_unref_fs;
+ mnt_unref_table;
} MOUNT_2.23;
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 */
#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 */
return NULL;
DBG(TAB, mnt_debug_h(tb, "alloc"));
-
+ tb->refcount = 1;
INIT_LIST_HEAD(&tb->ents);
return 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)
{
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);
* const char *dir = mnt_fs_get_target(fs);
* printf("mount point: %s\n", dir);
* }
- * mnt_free_table(fi);
* </programlisting>
* </informalexample>
*
return tb;
err:
fprintf(stderr, "%s: parsing failed\n", file);
- mnt_free_table(tb);
+ mnt_unref_table(tb);
return NULL;
}
mnt_unref_fs(fs);
rc = 0;
done:
- mnt_free_table(tb);
+ mnt_unref_table(tb);
return rc;
}
rc = 0;
done:
mnt_free_iter(itr);
- mnt_free_table(tb);
+ mnt_unref_table(tb);
return rc;
}
rc = 0;
}
done:
- mnt_free_table(tb);
+ mnt_unref_table(tb);
return rc;
}
mnt_fs_print_debug(fs, stdout);
rc = 0;
done:
- mnt_free_table(tb);
+ mnt_unref_table(tb);
return rc;
}
mnt_fs_print_debug(fs, stdout);
rc = 0;
done:
- mnt_free_table(tb);
+ mnt_unref_table(tb);
return rc;
}
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;
}
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;
if (tb) {
tb->fmt = fmt;
if (mnt_table_parse_file(tb, filename) != 0) {
- mnt_free_table(tb);
+ mnt_unref_table(tb);
tb = NULL;
}
}
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;
mnt_table_merge_user_fs(tb, u_fs);
}
- mnt_free_table(u_tb);
+ mnt_unref_table(u_tb);
return 0;
}
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);
if (lc)
mnt_unlock_file(lc);
- mnt_free_table(tb);
+ mnt_unref_table(tb);
return rc;
}
if (lc)
mnt_unlock_file(lc);
- mnt_free_table(tb);
+ mnt_unref_table(tb);
return rc;
}
if (lc)
mnt_unlock_file(lc);
- mnt_free_table(tb);
+ mnt_unref_table(tb);
return rc;
}
if (lc)
mnt_unlock_file(lc);
- mnt_free_table(tb);
+ mnt_unref_table(tb);
return rc;
}
mnt_unref_fs(fs);
rc = mnt_table_replace_file(tb, mnt_get_fstab_path());
- mnt_free_table(tb);
+ mnt_unref_table(tb);
return rc;
}