From deb0e659451d4ade29df3dbb842607edd6080c0d Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 17 Oct 2012 11:42:21 +0200 Subject: [PATCH 07/11] lib/loopdev: improve debug messages Signed-off-by: Karel Zak --- lib/loopdev.c | 22 ++++++++++++++++++---- sys-utils/losetup.c | 2 -- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/loopdev.c b/lib/loopdev.c index a9f6df2..0e6033a 100644 --- a/lib/loopdev.c +++ b/lib/loopdev.c @@ -87,8 +87,10 @@ int loopcxt_set_device(struct loopdev_cxt *lc, const char *device) if (!lc) return -EINVAL; - if (lc->fd >= 0) + if (lc->fd >= 0) { close(lc->fd); + DBG(lc, loopdev_debug("closing old open fd")); + } lc->fd = -1; lc->mode = 0; lc->has_info = 0; @@ -158,19 +160,26 @@ int loopcxt_init(struct loopdev_cxt *lc, int flags) memcpy(lc, &dummy, sizeof(dummy)); lc->flags = flags; + if (getenv("LOOPDEV_DEBUG")) + loopcxt_enable_debug(lc, TRUE); + rc = loopcxt_set_device(lc, NULL); if (rc) return rc; if (!(lc->flags & LOOPDEV_FL_NOSYSFS) && - get_linux_version() >= KERNEL_VERSION(2,6,37)) + get_linux_version() >= KERNEL_VERSION(2,6,37)) { /* * Use only sysfs for basic information about loop devices */ lc->flags |= LOOPDEV_FL_NOIOCTL; + DBG(lc, loopdev_debug("init: ignore ioctls")); + } - if (!(lc->flags & LOOPDEV_FL_CONTROL) && !stat(_PATH_DEV_LOOPCTL, &st)) + if (!(lc->flags & LOOPDEV_FL_CONTROL) && !stat(_PATH_DEV_LOOPCTL, &st)) { lc->flags |= LOOPDEV_FL_CONTROL; + DBG(lc, loopdev_debug("init: loop-control detected ")); + } return 0; } @@ -272,7 +281,9 @@ int loopcxt_get_fd(struct loopdev_cxt *lc) if (lc->fd < 0) { lc->mode = lc->flags & LOOPDEV_FL_RDWR ? O_RDWR : O_RDONLY; lc->fd = open(lc->device, lc->mode); - DBG(lc, loopdev_debug("open %s", lc->fd < 0 ? "failed" : "ok")); + DBG(lc, loopdev_debug("open %s [%s]: %s", lc->device, + lc->flags & LOOPDEV_FL_RDWR ? "rw" : "ro", + lc->fd < 0 ? "failed" : "ok")); } return lc->fd; } @@ -576,6 +587,7 @@ int loopcxt_next(struct loopdev_cxt *lc) * of loop devices). This is enough for 99% of all cases. */ if (iter->default_check) { + DBG(lc, loopdev_debug("iter: next: default check")); for (++iter->ncur; iter->ncur < LOOPDEV_DEFAULT_NNODES; iter->ncur++) { char name[16]; @@ -590,6 +602,7 @@ int loopcxt_next(struct loopdev_cxt *lc) /* C) the worst possibility, scan whole /dev or /dev/loop/ */ if (!iter->minors) { + DBG(lc, loopdev_debug("iter: next: scan /dev")); iter->nminors = (lc->flags & LOOPDEV_FL_DEVSUBDIR) ? loop_scandir(_PATH_DEV_LOOP, &iter->minors, 0) : loop_scandir(_PATH_DEV, &iter->minors, 1); @@ -1150,6 +1163,7 @@ int loopcxt_setup_device(struct loopdev_cxt *lc) DBG(lc, loopdev_debug("setup: backing file open: OK")); if (lc->fd != -1 && lc->mode != mode) { + DBG(lc, loopdev_debug("closing already open device (mode mismatch)")); close(lc->fd); lc->fd = -1; lc->mode = 0; diff --git a/sys-utils/losetup.c b/sys-utils/losetup.c index fdcc0be..a8381a8 100644 --- a/sys-utils/losetup.c +++ b/sys-utils/losetup.c @@ -250,8 +250,6 @@ int main(int argc, char **argv) if (loopcxt_init(&lc, 0)) err(EXIT_FAILURE, _("failed to initialize loopcxt")); - loopcxt_enable_debug(&lc, getenv("LOOPDEV_DEBUG") ? TRUE : FALSE); - while ((c = getopt_long(argc, argv, "ac:d:De:E:fhj:o:p:PrvV", longopts, NULL)) != -1) { -- 1.7.11.7