]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/sysfs: make possible to call sysfs_blkdev_deinit_path() in loop
authorKarel Zak <kzak@redhat.com>
Wed, 16 May 2018 10:52:47 +0000 (12:52 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 21 Jun 2018 11:07:46 +0000 (13:07 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
lib/path.c
lib/sysfs.c

index c665196a995af00b41c6b877b6dc4b3166efaa89..983524ef4c446ca28b43e6277a07b439b52e5cb6 100644 (file)
@@ -143,7 +143,7 @@ int ul_path_set_dialect(struct path_cxt *pc, void *data, void free_data(struct p
 {
        pc->dialect = data;
        pc->free_dialect = free_data;
-       DBG(CXT, ul_debugobj(pc, "new dialect"));
+       DBG(CXT, ul_debugobj(pc, "(re)set dialect"));
        return 0;
 }
 
index 3fe77144934d1f0a8afa27eb7031cd6f6dfa10ee..5b4031e00b043f0bbe0138fcf7faf79207c64644 100644 (file)
@@ -64,6 +64,9 @@ struct path_cxt *ul_new_sysfs_path(dev_t devno, struct path_cxt *parent, const c
 /*
  * sysfs_blkdev_* is sysfs extension to ul_path_* API for block devices.
  *
+ * The function is possible to call in loop and without sysfs_blkdev_deinit_path().
+ * The sysfs_blkdev_deinit_path() is automatically called by ul_unref_path().
+ *
  */
 int sysfs_blkdev_init_path(struct path_cxt *pc, dev_t devno, struct path_cxt *parent)
 {
@@ -85,19 +88,22 @@ int sysfs_blkdev_init_path(struct path_cxt *pc, dev_t devno, struct path_cxt *pa
                return rc;
 
        /* initialize sysfs blkdev specific stuff */
-       blk = calloc(1, sizeof(struct sysfs_blkdev));
-       if (!blk)
-               return -ENOMEM;
+       blk = ul_path_get_dialect(pc);
+       if (!blk) {
+               DBG(CXT, ul_debugobj(pc, "alloc new sysfs handler"));
+               blk = calloc(1, sizeof(struct sysfs_blkdev));
+               if (!blk)
+                       return -ENOMEM;
 
-       DBG(CXT, ul_debugobj(pc, "init for sysfs"));
+               ul_path_set_dialect(pc, blk, sysfs_blkdev_deinit_path);
+               ul_path_set_enoent_redirect(pc, sysfs_blkdev_enoent_redirect);
+       }
 
-       blk->devno = devno;
-       ul_path_set_dialect(pc, blk, sysfs_blkdev_deinit_path);
+       DBG(CXT, ul_debugobj(pc, "init sysfs stuff"));
 
+       blk->devno = devno;
        sysfs_blkdev_set_parent(pc, parent);
 
-       ul_path_set_enoent_redirect(pc, sysfs_blkdev_enoent_redirect);
-
        return 0;
 }
 
@@ -116,6 +122,8 @@ static void sysfs_blkdev_deinit_path(struct path_cxt *pc)
 
        ul_ref_path(blk->parent);
        free(blk);
+
+       ul_path_set_dialect(pc, NULL, NULL);
 }
 
 int sysfs_blkdev_set_parent(struct path_cxt *pc, struct path_cxt *parent)