All of lore.kernel.org
 help / color / mirror / Atom feed
From: Cyril Hrubis <chrubis@suse.cz>
To: Wei Gao <wegao@suse.com>
Cc: ltp@lists.linux.it
Subject: Re: [LTP] [PATCH v19 1/3] lib: Add tst_attach_loopdev()
Date: Mon, 23 Nov 2026 11:51:42 +0100	[thread overview]
Message-ID: <awQavtEBYYht-HIP@yuki.lan> (raw)
In-Reply-To: <20261116085612.20171-2-wegao@suse.com>

Hi!
> +static int attach_device(const char *dev, const char *file, int warn_on_ebusy)
>  {
>  	int dev_fd, file_fd;
>  	struct loop_info loopinfo;
> @@ -168,7 +168,8 @@ int tst_attach_device(const char *dev, const char *file)
>  
>  	dev_fd = TST_RETRY_FN_EXP_BACKOFF(open(dev, O_RDWR), TST_RETVAL_GE0, 1);
>  	if (dev_fd < 0) {
> -		tst_resm(TWARN | TERRNO, "open('%s', O_RDWR) failed", dev);
> +		if (warn_on_ebusy || errno != EBUSY)
> +			tst_resm(TWARN | TERRNO, "open('%s', O_RDWR) failed", dev);
>  		return 1;
>  	}
>  
> @@ -180,10 +181,16 @@ int tst_attach_device(const char *dev, const char *file)
>  	}
>  
>  	if (ioctl(dev_fd, LOOP_SET_FD, file_fd) < 0) {
> +		int err = errno;
> +
>  		close(dev_fd);
>  		close(file_fd);
> -		tst_resm(TWARN | TERRNO, "ioctl(%s, LOOP_SET_FD, %s) failed",
> -			 dev, file);
> +		errno = err;

This errno needs to be restored right before the return, the tst_resm()
still may clobber the errno.

> +		if (warn_on_ebusy || errno != EBUSY) {
> +			tst_resm(TWARN | TERRNO, "ioctl(%s, LOOP_SET_FD, %s) failed",
> +				 dev, file);
> +		}
>  		return 1;
>  	}
>  
> @@ -207,6 +214,64 @@ int tst_attach_device(const char *dev, const char *file)
>  	return 0;
>  }
>  
> +int tst_attach_device(const char *dev, const char *file)
> +{
> +	return attach_device(dev, file, 1);
> +}
> +
> +static int attach_loopdev(char *dev, size_t dev_len, const char *file)
> +{
> +	if (tst_find_free_loopdev(dev, dev_len) == -1)
> +		return -1;
> +
> +	if (attach_device(dev, file, 0) == 0)
> +		return 0;
> +
> +	if (errno == EBUSY)
> +		return 1;
> +
> +	return -1;
> +}
> +
> +char *tst_attach_loopdev(const char *file)
> +{
> +	char dev[PATH_MAX];
> +	unsigned int delay = 1;
> +	unsigned int max_delay = tst_multiply_timeout(1000000);
> +	int ret;
> +
> +	for (;;) {
> +		ret = attach_loopdev(dev, sizeof(dev), file);
> +		if (ret <= 0)
> +			break;
> +
> +		if (delay < max_delay) {
> +			usleep(delay);
> +			delay *= 2;
> +		} else {
> +			ret = -1;
> +			break;
> +		}
> +	}
> +
> +	if (ret == 0)
> +		return strdup(dev);

It's unlikely, but the strdup() can fail. So I guess this should be
at least TBROK on NULL.

> +	return NULL;
> +}
> +
> +char *safe_attach_loopdev(const char *file, const int lineno, const char *file_path)
> +{
> +	char *dev = tst_attach_loopdev(file_path);
> +
> +	if (!dev) {
> +		tst_brkm_(file, lineno, TBROK, NULL,
> +			  "Failed to attach %s to loop device", file_path);
> +	}
> +
> +	return dev;
> +}
> +
>  uint64_t tst_get_device_size(const char *dev_path)
>  {
>  	int fd;
> @@ -386,17 +451,20 @@ int tst_dev_sync(int fd)
>  const char *tst_acquire_loop_device(unsigned int size, const char *filename)
>  {
>  	unsigned int acq_dev_size = size ? size : DEV_SIZE_MB;
> +	char *dev;
>  
>  	if (tst_prealloc_file(filename, 1024 * 1024, acq_dev_size)) {
>  		tst_resm(TWARN | TERRNO, "Failed to create %s", filename);
>  		return NULL;
>  	}
>  
> -	if (tst_find_free_loopdev(dev_path, sizeof(dev_path)) == -1)
> +	dev = tst_attach_loopdev(filename);
> +	if (!dev)
>  		return NULL;
>  
> -	if (tst_attach_device(dev_path, filename))
> -		return NULL;
> +	strncpy(dev_path, dev, sizeof(dev_path) - 1);
> +	dev_path[sizeof(dev_path) - 1] = '\0';
> +	free(dev);

Given that we allocate the path now, we can drop the dev_path global and
just return the allocated string instead.

-- 
Cyril Hrubis
chrubis@suse.cz

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

  parent reply	other threads:[~2026-11-23 10:51 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16  8:56 [LTP] [PATCH v19 0/3] fsconfig04: Check FSCONFIG_SET_PATH Wei Gao via ltp
2026-11-16  8:56 ` [LTP] [PATCH v19 1/3] lib: Add tst_attach_loopdev() Wei Gao via ltp
2026-11-16 12:16   ` [LTP] " linuxtestproject.agent
2026-11-23 10:51   ` Cyril Hrubis [this message]
2026-11-16  8:56 ` [LTP] [PATCH v19 2/3] newlib_tests: Add tst_attach_loopdev test Wei Gao via ltp
2026-11-16  8:56 ` [LTP] [PATCH v19 3/3] fsconfig04: Check FSCONFIG_SET_PATH Wei Gao via ltp

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=awQavtEBYYht-HIP@yuki.lan \
    --to=chrubis@suse.cz \
    --cc=ltp@lists.linux.it \
    --cc=wegao@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.