]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: add reference counter to libmnt_table
authorKarel Zak <kzak@redhat.com>
Wed, 21 Aug 2013 14:07:25 +0000 (16:07 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 21 Aug 2013 14:07:25 +0000 (16:07 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
libmount/docs/libmount-sections.txt
libmount/src/cache.c
libmount/src/context.c
libmount/src/libmount.h.in
libmount/src/libmount.sym
libmount/src/mountP.h
libmount/src/tab.c
libmount/src/tab_diff.c
libmount/src/tab_parse.c
libmount/src/tab_update.c

index 3ebc4aaf2e5a2916305b2a7bf57f72a38e6bf200..037877f56e2f1304f08d6d7e55e41b6b0af731c7 100644 (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
index cf5c6d430229be64e7dfb80392046edb7be7bb1a..92f46945623810161f476b1ae8edd7b983f5a690 100644 (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)
 {
index 5d5e8fba8dc82b38d309ebee07fae95d3137463f..014f0ee90d7aa3348548ae67ce78b673a4b29d29 100644 (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;
 }
index f4efd980435679e804a3d689289f1d9d88ddb0a9..b95b08bce45be4cfe17a038d04ab88263cfa542b 100644 (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);
index 5a7e0def3f92a28a8831eedde232e7ecd2d7c6ec..430c58ec676a40f083eada797a6fa66f94ab8279 100644 (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;
index bf450a6c1bfd25eaef48db5b2921711ddef3b738..9e6f4bd2828526202f0a913effeb32a0b026430d 100644 (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 */
index a2ad604f792024ab89df4038fab32c2833c5939d..b1078cf818c5e45b8d74169c151f0a6d03fceb7d 100644 (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;
 }
index 71ef0f41919bf9208e644ffe3e428f06ad91b9c2..052278233e409fbb747203c9b1392634fe91793c 100644 (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;
index e31dac8c2d4b437c99219fe9b6a410b65998b8c2..532f0ec04a40834689ac3508b4f7a97ef3793942 100644 (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;
 }
index b5c92726f4d44d7b8c0a9511cfc082f354f92886..a508b990340c0e394e76d1eb7bdcbfaf809085e1 100644 (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;
 }