]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
libxfs: stash away the device fd in struct xfs_buftarg
authorChristoph Hellwig <hch@lst.de>
Mon, 11 Dec 2023 16:37:41 +0000 (17:37 +0100)
committerCarlos Maiolino <cem@kernel.org>
Mon, 18 Dec 2023 13:57:49 +0000 (14:57 +0100)
Cache the open file descriptor for each device in the buftarg
structure and remove the now unused dev_map infrastructure.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Carlos Maiolino <cmaiolino@redhat.com>
Signed-off-by: Carlos Maiolino <cem@kernel.org>
include/libxfs.h
libxfs/init.c
libxfs/libxfs_io.h
libxfs/rdwr.c
repair/prefetch.c

index 68efe9caa8b03b823cad9b0a858e6f40d09f2f1f..058217c2a42cb4fe798a81d2f3c3acfb7586e0af 100644 (file)
@@ -147,7 +147,6 @@ extern xfs_lsn_t libxfs_max_lsn;
 int            libxfs_init(struct libxfs_init *);
 void           libxfs_destroy(struct libxfs_init *li);
 
-extern int     libxfs_device_to_fd (dev_t);
 extern int     libxfs_device_alignment (void);
 extern void    libxfs_report(FILE *);
 
index 866e5f425f7209542e0032388231a1b407a54cb4..320e4d63f9959f2f4eaf08fe89a8ed01890d9317 100644 (file)
@@ -36,15 +36,7 @@ int libxfs_bhash_size;               /* #buckets in bcache */
 
 int    use_xfs_buf_lock;       /* global flag: use xfs_buf locks for MT */
 
-/*
- * dev_map - map open devices to fd.
- */
-#define MAX_DEVS 10    /* arbitary maximum */
 static int nextfakedev = -1;   /* device number to give to next fake device */
-static struct dev_to_fd {
-       dev_t   dev;
-       int     fd;
-} dev_map[MAX_DEVS]={{0}};
 
 /*
  * Checks whether a given device has a mounted, writable
@@ -70,33 +62,13 @@ check_isactive(char *name, char *block, int fatal)
        return 0;
 }
 
-/* libxfs_device_to_fd:
- *     lookup a device number in the device map
- *     return the associated fd
- */
-int
-libxfs_device_to_fd(dev_t device)
-{
-       int     d;
-
-       for (d = 0; d < MAX_DEVS; d++)
-               if (dev_map[d].dev == device)
-                       return dev_map[d].fd;
-
-       fprintf(stderr, _("%s: %s: device %lld is not open\n"),
-               progname, __FUNCTION__, (long long)device);
-       exit(1);
-       /* NOTREACHED */
-}
-
 /* libxfs_device_open:
  *     open a device and return its device number
  */
 static dev_t
 libxfs_device_open(char *path, int creat, int xflags, int setblksize, int *fdp)
 {
-       dev_t           dev;
-       int             fd, d, flags;
+       int             fd, flags;
        int             readonly, dio, excl;
        struct stat     statb;
 
@@ -134,61 +106,28 @@ retry:
        }
 
        /*
-        * Get the device number from the stat buf - unless
-        * we're not opening a real device, in which case
-        * choose a new fake device number.
+        * Get the device number from the stat buf - unless we're not opening a
+        * real device, in which case choose a new fake device number.
         */
-       dev = (statb.st_rdev) ? (statb.st_rdev) : (nextfakedev--);
-
-       for (d = 0; d < MAX_DEVS; d++)
-               if (dev_map[d].dev == dev) {
-                       fprintf(stderr, _("%s: device %lld is already open\n"),
-                           progname, (long long)dev);
-                       exit(1);
-               }
-
-       for (d = 0; d < MAX_DEVS; d++)
-               if (!dev_map[d].dev) {
-                       dev_map[d].dev = dev;
-                       dev_map[d].fd = fd;
-                       *fdp = fd;
-
-                       return dev;
-               }
-
-       fprintf(stderr, _("%s: %s: too many open devices\n"),
-               progname, __FUNCTION__);
-       exit(1);
-       /* NOTREACHED */
+       *fdp = fd;
+       if (statb.st_rdev)
+               return statb.st_rdev;
+       return nextfakedev--;
 }
 
 static void
-libxfs_device_close(dev_t dev)
+libxfs_device_close(int fd, dev_t dev)
 {
-       int     d;
+       int     ret;
 
-       for (d = 0; d < MAX_DEVS; d++)
-               if (dev_map[d].dev == dev) {
-                       int     fd, ret;
-
-                       fd = dev_map[d].fd;
-                       dev_map[d].dev = dev_map[d].fd = 0;
-
-                       ret = platform_flush_device(fd, dev);
-                       if (ret) {
-                               ret = -errno;
-                               fprintf(stderr,
+       ret = platform_flush_device(fd, dev);
+       if (ret) {
+               ret = -errno;
+               fprintf(stderr,
        _("%s: flush of device %lld failed, err=%d"),
-                                               progname, (long long)dev, ret);
-                       }
-                       close(fd);
-
-                       return;
-               }
-
-       fprintf(stderr, _("%s: %s: device %lld is not open\n"),
-                       progname, __FUNCTION__, (long long)dev);
-       exit(1);
+                       progname, (long long)dev, ret);
+       }
+       close(fd);
 }
 
 static int
@@ -271,11 +210,11 @@ libxfs_close_devices(
        struct libxfs_init      *li)
 {
        if (li->ddev)
-               libxfs_device_close(li->ddev);
+               libxfs_device_close(li->dfd, li->ddev);
        if (li->logdev && li->logdev != li->ddev)
-               libxfs_device_close(li->logdev);
+               libxfs_device_close(li->logfd, li->logdev);
        if (li->rtdev)
-               libxfs_device_close(li->rtdev);
+               libxfs_device_close(li->rtfd, li->rtdev);
 
        li->ddev = li->logdev = li->rtdev = 0;
        li->dfd = li->logfd = li->rtfd = -1;
@@ -514,6 +453,7 @@ static struct xfs_buftarg *
 libxfs_buftarg_alloc(
        struct xfs_mount        *mp,
        dev_t                   dev,
+       int                     fd,
        unsigned long           write_fails)
 {
        struct xfs_buftarg      *btp;
@@ -526,6 +466,7 @@ libxfs_buftarg_alloc(
        }
        btp->bt_mount = mp;
        btp->bt_bdev = dev;
+       btp->bt_bdev_fd = fd;
        btp->flags = 0;
        if (write_fails) {
                btp->writes_left = write_fails;
@@ -629,13 +570,14 @@ libxfs_buftarg_init(
                return;
        }
 
-       mp->m_ddev_targp = libxfs_buftarg_alloc(mp, xi->ddev, dfail);
+       mp->m_ddev_targp = libxfs_buftarg_alloc(mp, xi->ddev, xi->dfd, dfail);
        if (!xi->logdev || xi->logdev == xi->ddev)
                mp->m_logdev_targp = mp->m_ddev_targp;
        else
                mp->m_logdev_targp = libxfs_buftarg_alloc(mp, xi->logdev,
-                               lfail);
-       mp->m_rtdev_targp = libxfs_buftarg_alloc(mp, xi->rtdev, rfail);
+                               xi->logfd, lfail);
+       mp->m_rtdev_targp = libxfs_buftarg_alloc(mp, xi->rtdev, xi->rtfd,
+                       rfail);
 }
 
 /* Compute maximum possible height for per-AG btree types for this fs. */
index bf4d4ecd9771054a0e1731ac38e195a963ba4156..267ea979656fa3189f11644096936ce5d8c6c317 100644 (file)
@@ -26,6 +26,7 @@ struct xfs_buftarg {
        pthread_mutex_t         lock;
        unsigned long           writes_left;
        dev_t                   bt_bdev;
+       int                     bt_bdev_fd;
        unsigned int            flags;
 };
 
index ccd1501ab39eaf15474ca31158fb08a74c6e4321..0e332110b685973a0eeac0694d925c1db1c53a87 100644 (file)
@@ -62,13 +62,13 @@ static void libxfs_brelse(struct cache_node *node);
 int
 libxfs_device_zero(struct xfs_buftarg *btp, xfs_daddr_t start, uint len)
 {
+       int             fd = btp->bt_bdev_fd;
        xfs_off_t       start_offset, end_offset, offset;
        ssize_t         zsize, bytes;
        size_t          len_bytes;
        char            *z;
-       int             error, fd;
+       int             error;
 
-       fd = libxfs_device_to_fd(btp->bt_bdev);
        start_offset = LIBXFS_BBTOOFF64(start);
 
        /* try to use special zeroing methods, fall back to writes if needed */
@@ -598,7 +598,7 @@ int
 libxfs_readbufr(struct xfs_buftarg *btp, xfs_daddr_t blkno, struct xfs_buf *bp,
                int len, int flags)
 {
-       int     fd = libxfs_device_to_fd(btp->bt_bdev);
+       int     fd = btp->bt_bdev_fd;
        int     bytes = BBTOB(len);
        int     error;
 
@@ -631,12 +631,11 @@ libxfs_readbuf_verify(
 int
 libxfs_readbufr_map(struct xfs_buftarg *btp, struct xfs_buf *bp, int flags)
 {
-       int     fd;
+       int     fd = btp->bt_bdev_fd;
        int     error = 0;
        void    *buf;
        int     i;
 
-       fd = libxfs_device_to_fd(btp->bt_bdev);
        buf = bp->b_addr;
        for (i = 0; i < bp->b_nmaps; i++) {
                off64_t offset = LIBXFS_BBTOOFF64(bp->b_maps[i].bm_bn);
@@ -820,7 +819,7 @@ int
 libxfs_bwrite(
        struct xfs_buf  *bp)
 {
-       int             fd = libxfs_device_to_fd(bp->b_target->bt_bdev);
+       int             fd = bp->b_target->bt_bdev_fd;
 
        /*
         * we never write buffers that are marked stale. This indicates they
@@ -1171,13 +1170,12 @@ int
 libxfs_blkdev_issue_flush(
        struct xfs_buftarg      *btp)
 {
-       int                     fd, ret;
+       int                     ret;
 
        if (btp->bt_bdev == 0)
                return 0;
 
-       fd = libxfs_device_to_fd(btp->bt_bdev);
-       ret = platform_flush_device(fd, btp->bt_bdev);
+       ret = platform_flush_device(btp->bt_bdev_fd, btp->bt_bdev);
        return ret ? -errno : 0;
 }
 
index 017750e9a92a037d7daaa0d740797900970a19bf..78c1e397433a608e91c56796f9f581933e9cfdd8 100644 (file)
@@ -876,7 +876,7 @@ init_prefetch(
        xfs_mount_t             *pmp)
 {
        mp = pmp;
-       mp_fd = libxfs_device_to_fd(mp->m_ddev_targp->bt_bdev);
+       mp_fd = mp->m_ddev_targp->bt_bdev_fd;;
        pf_max_bytes = sysconf(_SC_PAGE_SIZE) << 7;
        pf_max_bbs = pf_max_bytes >> BBSHIFT;
        pf_max_fsbs = pf_max_bytes >> mp->m_sb.sb_blocklog;