]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
db: separate out straight buffer IO from map based IO.
authorDave Chinner <dchinner@redhat.com>
Wed, 13 Nov 2013 06:40:41 +0000 (06:40 +0000)
committerRich Johnston <rjohnston@sgi.com>
Wed, 13 Nov 2013 17:08:20 +0000 (11:08 -0600)
Libxfs has two different interfaces for getting and reading buffers.
The first is a block/length interface for reading contiguous
regions, and the second is based on extent based xfs_buf_map arrays
for discontiguous regions. The xfs-db code is solely based on a
basic block array interface regardless of the type of region being
read, and so doesn't match to either libxfs interface.

As a first step to converting xfs_db to the libxfs interfaces, add a
simple block/length buffer API and implement it using pread/pwrite.
Then remove the single region conditionals from the basic block array
based interfaces, and convert all the contiguous block read cases to
use the new API.

This new API is temporary - it will be replaced by the equivalent
libxfs interface calls once all the infrastructure preparation for
the changeover has been completed.

Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Rich Johnston <rjohnston@sgi.com>
db/init.c
db/io.c
db/io.h

index d73d5490d57870058fd4034a5d5a075a1e180d8b..489c9fbcb7bbba7e10c7d2a15e2de105e91b570b 100644 (file)
--- a/db/init.c
+++ b/db/init.c
@@ -55,7 +55,7 @@ init(
        char            **argv)
 {
        xfs_sb_t        *sbp;
-       void            *bufp = NULL;
+       char            bufp[BBSIZE];
        int             c;
 
        setlocale(LC_ALL, "");
@@ -115,15 +115,14 @@ init(
                exit(1);
        }
 
-       if (read_bbs(XFS_SB_DADDR, 1, &bufp, NULL)) {
+       if (read_buf(XFS_SB_DADDR, 1, bufp)) {
                fprintf(stderr, _("%s: %s is invalid (cannot read first 512 "
                        "bytes)\n"), progname, fsdevice);
                exit(1);
        }
 
        /* copy SB from buffer to in-core, converting architecture as we go */
-       libxfs_sb_from_disk(&xmount.m_sb, bufp);
-       xfree(bufp);
+       libxfs_sb_from_disk(&xmount.m_sb, (struct xfs_dsb *)bufp);
 
        sbp = &xmount.m_sb;
        if (sbp->sb_magicnum != XFS_SB_MAGIC) {
diff --git a/db/io.c b/db/io.c
index 39a1827cf05d7e32833a38c5a914d1104e7720b2..fa1164635fdd22aaf159694f29f94c41fb90bd15 100644 (file)
--- a/db/io.c
+++ b/db/io.c
@@ -417,8 +417,61 @@ ring_add(void)
        }
 }
 
-
 int
+read_buf(
+       xfs_daddr_t     bbno,
+       int             count,
+       void            *bufp)
+{
+       int             err;
+
+       err = pread64(x.dfd, bufp, BBTOB(count), BBTOB(bbno));
+       if (err < 0)
+               err = errno;
+       else if (err < count)
+               err = -1;
+       return err;
+}
+
+static int
+write_buf(
+       xfs_daddr_t     bbno,
+       int             count,
+       void            *bufp)
+{
+       int             err;
+
+       err = pwrite64(x.dfd, bufp, BBTOB(count), BBTOB(bbno));
+       if (err < 0)
+               err = errno;
+       else if (err < count)
+               err = -1;
+       return err;
+}
+
+static void
+write_cur_buf(void)
+{
+       int ret;
+
+       ret = write_buf(iocur_top->bb, iocur_top->blen, iocur_top->buf);
+
+       if (ret == -1)
+               dbprintf(_("incomplete write, block: %lld\n"),
+                        (iocur_base + iocur_sp)->bb);
+       else if (ret != 0)
+               dbprintf(_("write error: %s\n"), strerror(ret));
+
+       /* re-read buffer from disk */
+       ret = read_buf(iocur_top->bb, iocur_top->blen, iocur_top->buf);
+       if (ret == -1)
+               dbprintf(_("incomplete read, block: %lld\n"),
+                        (iocur_base + iocur_sp)->bb);
+       else if (ret != 0)
+               dbprintf(_("read error: %s\n"), strerror(ret));
+}
+
+static int
 write_bbs(
        __int64_t       bbno,
        int             count,
@@ -430,15 +483,14 @@ write_bbs(
        int             j;
        int             rval = EINVAL;  /* initialize for zero `count' case */
 
-       for (j = 0; j < count; j += bbmap ? 1 : count) {
-               if (bbmap)
-                       bbno = bbmap->b[j];
+       for (j = 0; j < count; j++) {
+               bbno = bbmap->b[j];
                if (lseek64(x.dfd, bbno << BBSHIFT, SEEK_SET) < 0) {
                        rval = errno;
                        dbprintf(_("can't seek in filesystem at bb %lld\n"), bbno);
                        return rval;
                }
-               c = BBTOB(bbmap ? 1 : count);
+               c = BBTOB(1);
                i = (int)write(x.dfd, (char *)bufp + BBTOB(j), c);
                if (i < 0) {
                        rval = errno;
@@ -452,7 +504,7 @@ write_bbs(
        return rval;
 }
 
-int
+static int
 read_bbs(
        __int64_t       bbno,
        int             count,
@@ -473,9 +525,8 @@ read_bbs(
                buf = xmalloc(c);
        else
                buf = *bufp;
-       for (j = 0; j < count; j += bbmap ? 1 : count) {
-               if (bbmap)
-                       bbno = bbmap->b[j];
+       for (j = 0; j < count; j++) {
+               bbno = bbmap->b[j];
                if (lseek64(x.dfd, bbno << BBSHIFT, SEEK_SET) < 0) {
                        rval = errno;
                        dbprintf(_("can't seek in filesystem at bb %lld\n"), bbno);
@@ -483,7 +534,7 @@ read_bbs(
                                xfree(buf);
                        buf = NULL;
                } else {
-                       c = BBTOB(bbmap ? 1 : count);
+                       c = BBTOB(1);
                        i = (int)read(x.dfd, (char *)buf + BBTOB(j), c);
                        if (i < 0) {
                                rval = errno;
@@ -506,22 +557,19 @@ read_bbs(
        return rval;
 }
 
-void
-write_cur(void)
+static void
+write_cur_bbs(void)
 {
        int ret;
 
-       if (iocur_sp < 0) {
-               dbprintf(_("nothing to write\n"));
-               return;
-       }
        ret = write_bbs(iocur_top->bb, iocur_top->blen, iocur_top->buf,
-               iocur_top->use_bbmap ? &iocur_top->bbmap : NULL);
+                       &iocur_top->bbmap);
        if (ret == -1)
                dbprintf(_("incomplete write, block: %lld\n"),
                         (iocur_base + iocur_sp)->bb);
        else if (ret != 0)
                dbprintf(_("write error: %s\n"), strerror(ret));
+
        /* re-read buffer from disk */
        ret = read_bbs(iocur_top->bb, iocur_top->blen, &iocur_top->buf,
                iocur_top->use_bbmap ? &iocur_top->bbmap : NULL);
@@ -532,6 +580,20 @@ write_cur(void)
                dbprintf(_("read error: %s\n"), strerror(ret));
 }
 
+void
+write_cur(void)
+{
+       if (iocur_sp < 0) {
+               dbprintf(_("nothing to write\n"));
+               return;
+       }
+
+       if (iocur_top->use_bbmap)
+               write_cur_bbs();
+       else
+               write_cur_buf();
+}
+
 void
 set_cur(
        const typ_t     *t,
@@ -549,17 +611,32 @@ set_cur(
                return;
        }
 
-#ifdef DEBUG
-       if (bbmap)
-               printf(_("xfs_db got a bbmap for %lld\n"), (long long)d);
-#endif
        ino = iocur_top->ino;
        dirino = iocur_top->dirino;
        mode = iocur_top->mode;
        pop_cur();
        push_cur();
-       if (read_bbs(d, c, &iocur_top->buf, bbmap))
-               return;
+
+       if (bbmap) {
+#ifdef DEBUG
+               printf(_("xfs_db got a bbmap for %lld\n"), (long long)d);
+#endif
+
+               if (read_bbs(d, c, &iocur_top->buf, bbmap))
+                       return;
+               iocur_top->bbmap = *bbmap;
+               iocur_top->use_bbmap = 1;
+       } else {
+               if (!iocur_top->buf) {
+                       iocur_top->buf = malloc(BBTOB(c));
+                       if (!iocur_top->buf)
+                               return;
+               }
+               if (read_buf(d, c, iocur_top->buf))
+                       return;
+               iocur_top->use_bbmap = 0;
+       }
+
        iocur_top->bb = d;
        iocur_top->blen = c;
        iocur_top->boff = 0;
@@ -570,8 +647,6 @@ set_cur(
        iocur_top->ino = ino;
        iocur_top->dirino = dirino;
        iocur_top->mode = mode;
-       if ((iocur_top->use_bbmap = (bbmap != NULL)))
-               iocur_top->bbmap = *bbmap;
 
        /* store location in ring */
        if (ring_flag)
diff --git a/db/io.h b/db/io.h
index 549aad915c567af76c16e51a9371f1072ccc69d1..9ea6223890137722c59667cdc576d22dcbb975d4 100644 (file)
--- a/db/io.h
+++ b/db/io.h
@@ -52,10 +52,7 @@ extern void  off_cur(int off, int len);
 extern void    pop_cur(void);
 extern void    print_iocur(char *tag, iocur_t *ioc);
 extern void    push_cur(void);
-extern int     read_bbs(__int64_t daddr, int count, void **bufp,
-                        bbmap_t *bbmap);
-extern int     write_bbs(__int64_t daddr, int count, void *bufp,
-                         bbmap_t *bbmap);
+extern int     read_buf(__int64_t daddr, int count, void *bufp);
 extern void     write_cur(void);
 extern void    set_cur(const struct typ *t, __int64_t d, int c, int ring_add,
                        bbmap_t *bbmap);