All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v5 1/3] lib: alter find_free_loopdev()
Date: Wed, 17 Jul 2019 18:34:57 +0800	[thread overview]
Message-ID: <5D2EF9D1.3020102@cn.fujitsu.com> (raw)
In-Reply-To: <CAOQ4uxiV_weQXVnDn0vFq5exk=Z_F=70Lk1D+783fOV=hsf_6A@mail.gmail.com>


> On Wed, Jul 17, 2019 at 12:45 PM Yang Xu<xuyang2018.jy@cn.fujitsu.com>  wrote:
>> Alter find_free_loopdev() to tst_find_free_loopdev(path, path_len),
>> it passes the dev_path inside of the tst_device.c and NULL from other
>> tests. It returns the free loopdev minor (and -1 for no free loopdev).
>> We can call tst_find_free_loopdev(NULL, 0) to get a free minor number
>> without changing lib internal state.
>>
>> Signed-off-by: Yang Xu<xuyang2018.jy@cn.fujitsu.com>
>> Reviewed-by: Amir Goldstein<amir73il@gmail.com>
>> ---
>>   doc/test-writing-guidelines.txt | 12 ++++++++++++
>>   include/tst_device.h            |  5 +++++
>>   lib/tst_device.c                | 34 +++++++++++++++++----------------
>>   3 files changed, 35 insertions(+), 16 deletions(-)
>>
>> diff --git a/doc/test-writing-guidelines.txt b/doc/test-writing-guidelines.txt
>> index 4b1e7d25b..c65c707e6 100644
>> --- a/doc/test-writing-guidelines.txt
>> +++ b/doc/test-writing-guidelines.txt
>> @@ -1045,6 +1045,18 @@ IMPORTANT: All testcases should use 'tst_umount()' instead of 'umount(2)' to
>>   -------------------------------------------------------------------------------
>>   #include "tst_test.h"
>>
>> +int tst_find_free_loopdev(const char *path, size_t path_len);
>> +-------------------------------------------------------------------------------
>> +
>> +This function finds a free loopdev and returns the free loopdev minor (-1 for no
>> +free loopdev). If path is non-NULL, it will be filled with free loopdev path.
>> +We can call tst_find_free_loopdev(NULL, 0) in tests to get a free minor number
>> +without changing lib internal state.
>> +
>> +[source,c]
>> +-------------------------------------------------------------------------------
>> +#include "tst_test.h"
>> +
>>   unsigned long tst_dev_bytes_written(const char *dev);
>>   -------------------------------------------------------------------------------
>>
>> diff --git a/include/tst_device.h b/include/tst_device.h
>> index 61902b7e0..42b9fa95b 100644
>> --- a/include/tst_device.h
>> +++ b/include/tst_device.h
>> @@ -44,6 +44,11 @@ int tst_umount(const char *path);
>>    */
>>   int tst_clear_device(const char *dev);
>>
>> +/*
>> + * Finds a free loop device for use and returns the free loopdev minor(-1 for no
>> + * free loopdev). If path is non-NULL, it will be filled with free loopdev path.
>> + */
>> +int tst_find_free_loopdev(const char *path, size_t path_len);
>>   /*
>>    * Reads test block device stat file and returns the bytes written since the
>>    * last call of this function.
>> diff --git a/lib/tst_device.c b/lib/tst_device.c
>> index 65fcc1337..f2516fb08 100644
>> --- a/lib/tst_device.c
>> +++ b/lib/tst_device.c
>> @@ -53,22 +53,22 @@ static const char *dev_variants[] = {
>>          "/dev/block/loop%i"
>>   };
>>
>> -static int set_dev_path(int dev)
>> +static int set_dev_path(int dev, char *path, size_t path_len)
>>   {
>>          unsigned int i;
>>          struct stat st;
>>
>>          for (i = 0; i<  ARRAY_SIZE(dev_variants); i++) {
>> -               snprintf(dev_path, sizeof(dev_path), dev_variants[i], dev);
>> +               snprintf(path, path_len, dev_variants[i], dev);
>>
>> -               if (stat(dev_path,&st) == 0&&  S_ISBLK(st.st_mode))
>> +               if (stat(path,&st) == 0&&  S_ISBLK(st.st_mode))
>>                          return 1;
>>          }
>>
>>          return 0;
>>   }
>>
>> -static int find_free_loopdev(void)
>> +int tst_find_free_loopdev(char *path, size_t path_len)
> Another option is to leave this function internal and export a wrapper
> for the exact needed functionality:
>
> /* Just find a free minor number */
> int tst_find_free_loopdev(void)
> {
>          return find_free_loopdev(NULL, 0);
> }
>
> I don't mind wither way.
Hi Amir

The second way looks better for me.  But I also want to hear the idea of cyril.

>>   {
>>          int ctl_fd, dev_fd, rc, i;
>>          struct loop_info loopinfo;
>> @@ -80,12 +80,14 @@ static int find_free_loopdev(void)
>>                  rc = ioctl(ctl_fd, LOOP_CTL_GET_FREE);
>>                  close(ctl_fd);
>>                  if (rc>= 0) {
>> -                       set_dev_path(rc);
>> -                       tst_resm(TINFO, "Found free device '%s'", dev_path);
>> -                       return 0;
>> +                       if (path)
>> +                               set_dev_path(rc, path, path_len);
>> +                       tst_resm(TINFO, "Found free device %d '%s'",
>> +                               rc, path ?: "");
>> +                       return rc;
>>                  }
>>                  tst_resm(TINFO, "Couldn't find free loop device");
>> -               return 1;
>> +               return -1;
>>          }
>>
>>          switch (errno) {
>> @@ -104,24 +106,24 @@ static int find_free_loopdev(void)
>>           * Older way is to iterate over /dev/loop%i and /dev/loop/%i and try
>>           * LOOP_GET_STATUS ioctl() which fails for free loop devices.
>>           */
>> -       for (i = 0; i<  256; i++) {
>> +       for (i = 0; path&&  i<  256; i++) {
>>
>> -               if (!set_dev_path(i))
>> +               if (!set_dev_path(i, path, path_len))
>>                          continue;
>>
>> -               dev_fd = open(dev_path, O_RDONLY);
>> +               dev_fd = open(path, O_RDONLY);
>>
>>                  if (dev_fd<  0)
>>                          continue;
>>
>>                  if (ioctl(dev_fd, LOOP_GET_STATUS,&loopinfo) == 0) {
>> -                       tst_resm(TINFO, "Device '%s' in use", dev_path);
>> +                       tst_resm(TINFO, "Device '%s' in use", path);
>>                  } else {
>>                          if (errno != ENXIO)
>>                                  continue;
>> -                       tst_resm(TINFO, "Found free device '%s'", dev_path);
>> +                       tst_resm(TINFO, "Found free device '%s'", path);
>>                          close(dev_fd);
>> -                       return 0;
>> +                       return i;
>>                  }
>>
>>                  close(dev_fd);
>> @@ -129,7 +131,7 @@ static int find_free_loopdev(void)
>>
>>          tst_resm(TINFO, "No free devices found");
>>
>> -       return 1;
>> +       return -1;
>>   }
>>
>>   static int attach_device(const char *dev, const char *file)
>> @@ -274,7 +276,7 @@ const char *tst_acquire_device__(unsigned int size)
>>                  return NULL;
>>          }
>>
>> -       if (find_free_loopdev())
>> +       if (tst_find_free_loopdev(dev_path, sizeof(dev_path)) == -1)
>>                  return NULL;
>>
>>          if (attach_device(dev_path, DEV_FILE))
>> --
>> 2.18.1
>>
>>
>>
>
> .
>




  reply	other threads:[~2019-07-17 10:34 UTC|newest]

Thread overview: 86+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-03  3:41 [LTP] [PATCH] syscalls/copy_file_range02: increase coverage and remove EXDEV test Yang Xu
2019-07-05 15:22 ` Amir Goldstein
2019-07-05 20:42   ` [LTP] [PATCH v2] " Yang Xu
2019-07-08 15:17     ` Amir Goldstein
2019-07-09  6:57       ` Yang Xu
2019-07-09 10:06         ` Amir Goldstein
2019-07-10  7:18           ` [LTP] [PATCH v3 1/3] lib: alter find_free_loopdev() Yang Xu
2019-07-10  7:18             ` [LTP] [PATCH v3 2/3] syscalls/copy_file_range01: add cross-device test Yang Xu
2019-07-10  7:50               ` Amir Goldstein
2019-07-10  8:22                 ` Yang Xu
2019-07-10  7:18             ` [LTP] [PATCH v3 3/3] syscalls/copy_file_range02: increase coverage and remove EXDEV test Yang Xu
2019-07-10  7:57               ` Amir Goldstein
2019-07-10  9:31                 ` Yang Xu
2019-07-10 10:53                   ` [LTP] [PATCH v4 1/3] lib: alter find_free_loopdev() Yang Xu
2019-07-10 10:53                     ` [LTP] [PATCH v4 2/3] syscalls/copy_file_range01: add cross-device test Yang Xu
2019-07-10 15:56                       ` Xiao Yang
2019-07-11  6:18                         ` Yang Xu
2019-07-10 10:53                     ` [LTP] [PATCH v4 3/3] syscalls/copy_file_range02: increase coverage and remove EXDEV test Yang Xu
2019-07-10 13:57                     ` [LTP] [PATCH v4 1/3] lib: alter find_free_loopdev() Cyril Hrubis
2019-07-11  4:00                       ` Yang Xu
2019-07-11 12:51                         ` Cyril Hrubis
2019-07-12  5:25                           ` Yang Xu
2019-07-17  5:29                             ` Yang Xu
2019-07-17  6:10                             ` Amir Goldstein
2019-07-17  9:44                           ` [LTP] [PATCH v5 " Yang Xu
2019-07-17  9:44                             ` [LTP] [PATCH v5 2/3] syscalls/copy_file_range01: add cross-device test Yang Xu
2019-07-29 13:56                               ` Petr Vorel
2019-07-29 13:59                                 ` Petr Vorel
2019-07-17  9:44                             ` [LTP] [PATCH v5 3/3] syscalls/copy_file_range02: increase coverage and remove EXDEV test Yang Xu
2019-07-17 10:14                             ` [LTP] [PATCH v5 1/3] lib: alter find_free_loopdev() Amir Goldstein
2019-07-17 10:34                               ` Yang Xu [this message]
2019-07-17 10:54                                 ` Cyril Hrubis
2019-07-18  7:30                                   ` Yang Xu
2019-07-25  5:01                                     ` [LTP] [PATCH v6 " Yang Xu
2019-07-25  5:01                                       ` [LTP] [PATCH v6 2/3] syscalls/copy_file_range01: add cross-device test Yang Xu
2019-07-29 14:00                                         ` Petr Vorel
2019-07-29 14:10                                           ` Petr Vorel
2019-07-30  8:31                                             ` Yang Xu
2019-07-30 13:35                                               ` Petr Vorel
2019-07-31  7:01                                                 ` Yang Xu
2019-07-31  7:47                                                   ` Petr Vorel
2019-07-25  5:01                                       ` [LTP] [PATCH v6 3/3] syscalls/copy_file_range02: increase coverage and remove EXDEV test Yang Xu
2019-07-25  5:24                                         ` Amir Goldstein
2019-07-25  5:44                                           ` Yang Xu
2019-07-25  8:08                                             ` Amir Goldstein
2019-07-25 10:13                                               ` Yang Xu
2019-07-25 11:02                                                 ` Amir Goldstein
2019-07-30 13:26                                         ` Petr Vorel
2019-07-31  7:09                                           ` Yang Xu
2019-07-29 13:01                                       ` [LTP] [PATCH v6 1/3] lib: alter find_free_loopdev() Cyril Hrubis
2019-07-30 10:42                                         ` Yang Xu
2019-07-30 11:05                                           ` Cyril Hrubis
2019-07-31 10:40                                             ` [LTP] [PATCH v7 " Yang Xu
2019-07-31 10:40                                               ` [LTP] [PATCH v7 2/3] syscalls/copy_file_range01: add cross-device test Yang Xu
2019-07-31 12:28                                                 ` Petr Vorel
2019-07-31 10:40                                               ` [LTP] [PATCH v7 3/3] syscalls/copy_file_range02: increase coverage and remove EXDEV test Yang Xu
2019-07-31 12:28                                                 ` Petr Vorel
2019-08-05  6:58                                                 ` Murphy Zhou
2019-08-05  7:11                                                   ` Yang Xu
2019-08-05 10:22                                                     ` Murphy Zhou
2019-08-05 11:01                                                       ` Yang Xu
2019-08-06  6:37                                                         ` Yang Xu
2019-08-06  9:29                                                           ` Murphy Zhou
2019-08-06 16:27                                                       ` Petr Vorel
2019-08-07 10:17                                                         ` Murphy Zhou
2019-08-07 10:17                                                           ` Murphy Zhou
2019-08-07 12:12                                                           ` Dave Chinner
2019-08-07 12:12                                                             ` Dave Chinner
2019-08-08  3:46                                                             ` Murphy Zhou
2019-08-08  3:46                                                               ` Murphy Zhou
2019-08-08  3:11                                                           ` Yang Xu
2019-08-08  3:11                                                             ` Yang Xu
2019-08-08  3:11                                                             ` Yang Xu
2019-08-08  3:57                                                             ` Murphy Zhou
2019-08-08  3:57                                                               ` Murphy Zhou
2019-08-27 10:04                                                               ` Petr Vorel
2019-08-27 10:04                                                                 ` Petr Vorel
2019-07-31 12:05                                               ` [LTP] [PATCH v7 1/3] lib: alter find_free_loopdev() Petr Vorel
2019-07-31 12:28                                                 ` Cyril Hrubis
2019-07-31 12:48                                                   ` Petr Vorel
2019-07-31 13:25                                                     ` Cyril Hrubis
2019-07-31 21:06                                                       ` Petr Vorel
2019-07-31 12:07                                               ` Petr Vorel
2019-07-10  7:47             ` [LTP] [PATCH v3 " Amir Goldstein
2019-07-10  7:32           ` [LTP] [PATCH v2] syscalls/copy_file_range02: increase coverage and remove EXDEV test Yang Xu
2019-07-08 10:38   ` [LTP] [PATCH] " Yang Xu

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=5D2EF9D1.3020102@cn.fujitsu.com \
    --to=xuyang2018.jy@cn.fujitsu.com \
    --cc=ltp@lists.linux.it \
    /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.