]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blobdiff - libxfs/rdwr.c
xfsprogs: make static things static
[thirdparty/xfsprogs-dev.git] / libxfs / rdwr.c
index 7b23394e397f1d5f2e9b7bca402452ab8a3756ec..753b9396fbe538dafe9cde701704827ab7cf1740 100644 (file)
@@ -1,19 +1,7 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  * All Rights Reserved.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write the Free Software Foundation,
- * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
 
@@ -67,7 +55,8 @@
 
 #define IO_BCOMPARE_CHECK
 
-void
+/* XXX: (dgc) Propagate errors, only exit if fail-on-error flag set */
+int
 libxfs_device_zero(struct xfs_buftarg *btp, xfs_daddr_t start, uint len)
 {
        xfs_off_t       start_offset, end_offset, offset;
@@ -87,7 +76,7 @@ libxfs_device_zero(struct xfs_buftarg *btp, xfs_daddr_t start, uint len)
        fd = libxfs_device_to_fd(btp->dev);
        start_offset = LIBXFS_BBTOOFF64(start);
 
-       if ((lseek64(fd, start_offset, SEEK_SET)) < 0) {
+       if ((lseek(fd, start_offset, SEEK_SET)) < 0) {
                fprintf(stderr, _("%s: %s seek to offset %llu failed: %s\n"),
                        progname, __FUNCTION__,
                        (unsigned long long)start_offset, strerror(errno));
@@ -109,6 +98,7 @@ libxfs_device_zero(struct xfs_buftarg *btp, xfs_daddr_t start, uint len)
                offset += bytes;
        }
        free(z);
+       return 0;
 }
 
 static void unmount_record(void *p)
@@ -116,9 +106,9 @@ static void unmount_record(void *p)
        xlog_op_header_t        *op = (xlog_op_header_t *)p;
        /* the data section must be 32 bit size aligned */
        struct {
-           __uint16_t magic;
-           __uint16_t pad1;
-           __uint32_t pad2; /* may as well make it 64 bits */
+           uint16_t magic;
+           uint16_t pad1;
+           uint32_t pad2; /* may as well make it 64 bits */
        } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
 
        memset(p, 0, BBSIZE);
@@ -141,7 +131,7 @@ static char *next(
        struct xfs_buf  *buf = (struct xfs_buf *)private;
 
        if (buf &&
-           (XFS_BUF_COUNT(buf) < (int)(ptr - XFS_BUF_PTR(buf)) + offset))
+           (buf->b_bcount < (int)(ptr - (char *)buf->b_addr) + offset))
                abort();
 
        return ptr + offset;
@@ -191,7 +181,7 @@ libxfs_log_clear(
         * previous cycle.
         */
        len = ((version == 2) && sunit) ? BTOBB(sunit) : 2;
-       len = MAX(len, 2);
+       len = max(len, 2);
        lsn = xlog_assign_lsn(cycle, 0);
        if (cycle == XLOG_INIT_CYCLE)
                tail_lsn = lsn;
@@ -202,7 +192,7 @@ libxfs_log_clear(
        ptr = dptr;
        if (btp) {
                bp = libxfs_getbufr(btp, start, len);
-               ptr = XFS_BUF_PTR(bp);
+               ptr = bp->b_addr;
        }
        libxfs_log_header(ptr, fs_uuid, version, sunit, fmt, lsn, tail_lsn,
                          next, bp);
@@ -250,7 +240,7 @@ libxfs_log_clear(
                ptr = dptr;
                if (btp) {
                        bp = libxfs_getbufr(btp, blk, len);
-                       ptr = XFS_BUF_PTR(bp);
+                       ptr = bp->b_addr;
                }
                /*
                 * Note: pass the full buffer length as the sunit to initialize
@@ -305,7 +295,7 @@ libxfs_log_header(
        head->h_prev_block = cpu_to_be32(-1);
        head->h_num_logops = cpu_to_be32(1);
        head->h_fmt = cpu_to_be32(fmt);
-       head->h_size = cpu_to_be32(MAX(sunit, XLOG_BIG_RECORD_BSIZE));
+       head->h_size = cpu_to_be32(max(sunit, XLOG_BIG_RECORD_BSIZE));
 
        head->h_lsn = cpu_to_be64(lsn);
        head->h_tail_lsn = cpu_to_be64(tail_lsn);
@@ -356,7 +346,7 @@ libxfs_log_header(
         * minimum (1 hdr blk + 1 data blk). The record length is the total
         * minus however many header blocks are required.
         */
-       head->h_len = cpu_to_be32(MAX(BBTOB(2), sunit) - hdrs * BBSIZE);
+       head->h_len = cpu_to_be32(max(BBTOB(2), sunit) - hdrs * BBSIZE);
 
        /*
         * Write out the unmount record, pack the first word into the record
@@ -373,7 +363,7 @@ libxfs_log_header(
         * the cycle. We don't need to pack any of these blocks because the
         * cycle data in the headers has already been zeroed.
         */
-       len = MAX(len, hdrs + 1);
+       len = max(len, hdrs + 1);
        for (i = hdrs + 1; i < len; i++) {
                p = nextfunc(p, BBSIZE, private);
                memset(p, 0, BBSIZE);
@@ -554,10 +544,10 @@ libxfs_bcompare(struct cache_node *node, cache_key_t key)
        return CACHE_MISS;
 }
 
-void
+static void
 libxfs_bprint(xfs_buf_t *bp)
 {
-       fprintf(stderr, "Buffer 0x%p blkno=%llu bytes=%u flags=0x%x count=%u\n",
+       fprintf(stderr, "Buffer %p blkno=%llu bytes=%u flags=0x%x count=%u\n",
                bp, (unsigned long long)bp->b_bn, (unsigned)bp->b_bcount,
                bp->b_flags, bp->b_node.cn_count);
 }
@@ -589,6 +579,13 @@ __initbuf(xfs_buf_t *bp, struct xfs_buftarg *btp, xfs_daddr_t bno,
        bp->b_holder = 0;
        bp->b_recur = 0;
        bp->b_ops = NULL;
+
+       if (!bp->b_maps) {
+               bp->b_nmaps = 1;
+               bp->b_maps = &bp->__b_map;
+               bp->b_maps[0].bm_bn = bp->b_bn;
+               bp->b_maps[0].bm_len = bp->b_length;
+       }
 }
 
 static void
@@ -606,8 +603,8 @@ libxfs_initbuf_map(xfs_buf_t *bp, struct xfs_buftarg *btp,
        int i;
 
        bytes = sizeof(struct xfs_buf_map) * nmaps;
-       bp->b_map = malloc(bytes);
-       if (!bp->b_map) {
+       bp->b_maps = malloc(bytes);
+       if (!bp->b_maps) {
                fprintf(stderr,
                        _("%s: %s can't malloc %u bytes: %s\n"),
                        progname, __FUNCTION__, bytes,
@@ -618,8 +615,8 @@ libxfs_initbuf_map(xfs_buf_t *bp, struct xfs_buftarg *btp,
 
        bytes = 0;
        for ( i = 0; i < nmaps; i++) {
-               bp->b_map[i].bm_bn = map[i].bm_bn;
-               bp->b_map[i].bm_len = map[i].bm_len;
+               bp->b_maps[i].bm_bn = map[i].bm_bn;
+               bp->b_maps[i].bm_len = map[i].bm_len;
                bytes += BBTOB(map[i].bm_len);
        }
 
@@ -627,7 +624,7 @@ libxfs_initbuf_map(xfs_buf_t *bp, struct xfs_buftarg *btp,
        bp->b_flags |= LIBXFS_B_DISCONTIG;
 }
 
-xfs_buf_t *
+static xfs_buf_t *
 __libxfs_getbufr(int blen)
 {
        xfs_buf_t       *bp;
@@ -652,13 +649,16 @@ __libxfs_getbufr(int blen)
                        list_del_init(&bp->b_node.cn_mru);
                        free(bp->b_addr);
                        bp->b_addr = NULL;
-                       free(bp->b_map);
-                       bp->b_map = NULL;
+                       if (bp->b_maps != &bp->__b_map)
+                               free(bp->b_maps);
+                       bp->b_maps = NULL;
                }
        } else
                bp = kmem_zone_zalloc(xfs_buf_zone, 0);
        pthread_mutex_unlock(&xfs_buf_freelist.cm_mutex);
        bp->b_ops = NULL;
+       if (bp->b_flags & LIBXFS_B_DIRTY)
+               fprintf(stderr, "found dirty buffer (bulk) on free list!");
 
        return bp;
 }
@@ -681,7 +681,7 @@ libxfs_getbufr(struct xfs_buftarg *btp, xfs_daddr_t blkno, int bblen)
        return bp;
 }
 
-xfs_buf_t *
+static xfs_buf_t *
 libxfs_getbufr_map(struct xfs_buftarg *btp, xfs_daddr_t blkno, int bblen,
                struct xfs_buf_map *map, int nmaps)
 {
@@ -779,7 +779,7 @@ struct xfs_buf *
 libxfs_getbuf_flags(struct xfs_buftarg *btp, xfs_daddr_t blkno, int len,
                unsigned int flags)
 {
-       struct xfs_bufkey key = {0};
+       struct xfs_bufkey key = {NULL};
 
        key.buftarg = btp;
        key.blkno = blkno;
@@ -822,7 +822,7 @@ static struct xfs_buf *
 __libxfs_getbuf_map(struct xfs_buftarg *btp, struct xfs_buf_map *map,
                    int nmaps, int flags)
 {
-       struct xfs_bufkey key = {0};
+       struct xfs_bufkey key = {NULL};
        int i;
 
        if (nmaps == 1)
@@ -882,7 +882,7 @@ libxfs_putbuf(xfs_buf_t *bp)
 void
 libxfs_purgebuf(xfs_buf_t *bp)
 {
-       struct xfs_bufkey key = {0};
+       struct xfs_bufkey key = {NULL};
 
        key.buftarg = bp->b_target;
        key.blkno = bp->b_bn;
@@ -911,7 +911,7 @@ __read_buf(int fd, void *buf, int len, off64_t offset, int flags)
 {
        int     sts;
 
-       sts = pread64(fd, buf, len, offset);
+       sts = pread(fd, buf, len, offset);
        if (sts < 0) {
                int error = errno;
                fprintf(stderr, _("%s: read failed: %s\n"),
@@ -1014,14 +1014,14 @@ libxfs_readbufr_map(struct xfs_buftarg *btp, struct xfs_buf *bp, int flags)
 {
        int     fd;
        int     error = 0;
-       char    *buf;
+       void    *buf;
        int     i;
 
        fd = libxfs_device_to_fd(btp->dev);
        buf = bp->b_addr;
        for (i = 0; i < bp->b_nmaps; i++) {
-               off64_t offset = LIBXFS_BBTOOFF64(bp->b_map[i].bm_bn);
-               int len = BBTOB(bp->b_map[i].bm_len);
+               off64_t offset = LIBXFS_BBTOOFF64(bp->b_maps[i].bm_bn);
+               int len = BBTOB(bp->b_maps[i].bm_len);
 
                error = __read_buf(fd, buf, len, offset, flags);
                if (error) {
@@ -1034,9 +1034,9 @@ libxfs_readbufr_map(struct xfs_buftarg *btp, struct xfs_buf *bp, int flags)
        if (!error)
                bp->b_flags |= LIBXFS_B_UPTODATE;
 #ifdef IO_DEBUG
-       printf("%lx: %s: read %u bytes, error %d, blkno=0x%llx(0x%llx), %p\n",
-               pthread_self(), __FUNCTION__, , error,
-               (long long)LIBXFS_BBTOOFF64(blkno), (long long)blkno, bp);
+       printf("%lx: %s: read %lu bytes, error %d, blkno=%llu(%llu), %p\n",
+               pthread_self(), __FUNCTION__, buf - (char *)bp->b_addr, error,
+               (long long)LIBXFS_BBTOOFF64(bp->b_bn), (long long)bp->b_bn, bp);
 #endif
        return error;
 }
@@ -1066,7 +1066,7 @@ libxfs_readbuf_map(struct xfs_buftarg *btp, struct xfs_buf_map *map, int nmaps,
        if (!error)
                libxfs_readbuf_verify(bp, ops);
 
-#ifdef IO_DEBUG
+#ifdef IO_DEBUGX
        printf("%lx: %s: read %lu bytes, error %d, blkno=%llu(%llu), %p\n",
                pthread_self(), __FUNCTION__, buf - (char *)bp->b_addr, error,
                (long long)LIBXFS_BBTOOFF64(bp->b_bn), (long long)bp->b_bn, bp);
@@ -1079,16 +1079,16 @@ __write_buf(int fd, void *buf, int len, off64_t offset, int flags)
 {
        int     sts;
 
-       sts = pwrite64(fd, buf, len, offset);
+       sts = pwrite(fd, buf, len, offset);
        if (sts < 0) {
                int error = errno;
-               fprintf(stderr, _("%s: pwrite64 failed: %s\n"),
+               fprintf(stderr, _("%s: pwrite failed: %s\n"),
                        progname, strerror(error));
                if (flags & LIBXFS_B_EXIT)
                        exit(1);
                return -error;
        } else if (sts != len) {
-               fprintf(stderr, _("%s: error - pwrite64 only %d of %d bytes\n"),
+               fprintf(stderr, _("%s: error - pwrite only %d of %d bytes\n"),
                        progname, sts, len);
                if (flags & LIBXFS_B_EXIT)
                        exit(1);
@@ -1101,7 +1101,6 @@ int
 libxfs_writebufr(xfs_buf_t *bp)
 {
        int     fd = libxfs_device_to_fd(bp->b_target->dev);
-       int     error = 0;
 
        /*
         * we never write buffers that are marked stale. This indicates they
@@ -1132,21 +1131,20 @@ libxfs_writebufr(xfs_buf_t *bp)
        }
 
        if (!(bp->b_flags & LIBXFS_B_DISCONTIG)) {
-               error = __write_buf(fd, bp->b_addr, bp->b_bcount,
+               bp->b_error = __write_buf(fd, bp->b_addr, bp->b_bcount,
                                    LIBXFS_BBTOOFF64(bp->b_bn), bp->b_flags);
        } else {
                int     i;
-               char    *buf = bp->b_addr;
+               void    *buf = bp->b_addr;
 
                for (i = 0; i < bp->b_nmaps; i++) {
-                       off64_t offset = LIBXFS_BBTOOFF64(bp->b_map[i].bm_bn);
-                       int len = BBTOB(bp->b_map[i].bm_len);
+                       off64_t offset = LIBXFS_BBTOOFF64(bp->b_maps[i].bm_bn);
+                       int len = BBTOB(bp->b_maps[i].bm_len);
 
-                       error = __write_buf(fd, buf, len, offset, bp->b_flags);
-                       if (error) {
-                               bp->b_error = error;
+                       bp->b_error = __write_buf(fd, buf, len, offset,
+                                                 bp->b_flags);
+                       if (bp->b_error)
                                break;
-                       }
                        buf += len;
                }
        }
@@ -1155,14 +1153,14 @@ libxfs_writebufr(xfs_buf_t *bp)
        printf("%lx: %s: wrote %u bytes, blkno=%llu(%llu), %p, error %d\n",
                        pthread_self(), __FUNCTION__, bp->b_bcount,
                        (long long)LIBXFS_BBTOOFF64(bp->b_bn),
-                       (long long)bp->b_bn, bp, error);
+                       (long long)bp->b_bn, bp, bp->b_error);
 #endif
-       if (!error) {
+       if (!bp->b_error) {
                bp->b_flags |= LIBXFS_B_UPTODATE;
                bp->b_flags &= ~(LIBXFS_B_DIRTY | LIBXFS_B_EXIT |
                                 LIBXFS_B_UNCHECKED);
        }
-       return error;
+       return bp->b_error;
 }
 
 int
@@ -1223,23 +1221,26 @@ libxfs_iomove(xfs_buf_t *bp, uint boff, int len, void *data, int flags)
 }
 
 static void
-libxfs_brelse(struct cache_node *node)
+libxfs_brelse(
+       struct cache_node       *node)
 {
-       xfs_buf_t               *bp = (xfs_buf_t *)node;
+       struct xfs_buf          *bp = (struct xfs_buf *)node;
 
-       if (bp != NULL) {
-               if (bp->b_flags & LIBXFS_B_DIRTY)
-                       libxfs_writebufr(bp);
-               pthread_mutex_lock(&xfs_buf_freelist.cm_mutex);
-               list_add(&bp->b_node.cn_mru, &xfs_buf_freelist.cm_list);
-               pthread_mutex_unlock(&xfs_buf_freelist.cm_mutex);
-       }
+       if (!bp)
+               return;
+       if (bp->b_flags & LIBXFS_B_DIRTY)
+               fprintf(stderr,
+                       "releasing dirty buffer to free list!");
+
+       pthread_mutex_lock(&xfs_buf_freelist.cm_mutex);
+       list_add(&bp->b_node.cn_mru, &xfs_buf_freelist.cm_list);
+       pthread_mutex_unlock(&xfs_buf_freelist.cm_mutex);
 }
 
 static unsigned int
 libxfs_bulkrelse(
-       struct cache            *cache,
-       struct list_head        *list)
+       struct cache            *cache,
+       struct list_head        *list)
 {
        xfs_buf_t               *bp;
        int                     count = 0;
@@ -1249,29 +1250,59 @@ libxfs_bulkrelse(
 
        list_for_each_entry(bp, list, b_node.cn_mru) {
                if (bp->b_flags & LIBXFS_B_DIRTY)
-                       libxfs_writebufr(bp);
+                       fprintf(stderr,
+                               "releasing dirty buffer (bulk) to free list!");
                count++;
        }
 
        pthread_mutex_lock(&xfs_buf_freelist.cm_mutex);
-       __list_splice(list, &xfs_buf_freelist.cm_list);
+       list_splice(list, &xfs_buf_freelist.cm_list);
        pthread_mutex_unlock(&xfs_buf_freelist.cm_mutex);
 
        return count;
 }
 
-static void
-libxfs_bflush(struct cache_node *node)
+/*
+ * Free everything from the xfs_buf_freelist MRU, used at final teardown
+ */
+void
+libxfs_bcache_free(void)
+{
+       struct list_head        *cm_list;
+       xfs_buf_t               *bp, *next;
+
+       cm_list = &xfs_buf_freelist.cm_list;
+       list_for_each_entry_safe(bp, next, cm_list, b_node.cn_mru) {
+               free(bp->b_addr);
+               if (bp->b_maps != &bp->__b_map)
+                       free(bp->b_maps);
+               kmem_zone_free(xfs_buf_zone, bp);
+       }
+}
+
+/*
+ * When a buffer is marked dirty, the error is cleared. Hence if we are trying
+ * to flush a buffer prior to cache reclaim that has an error on it it means
+ * we've already tried to flush it and it failed. Prevent repeated corruption
+ * errors from being reported by skipping such buffers - when the corruption is
+ * fixed the buffer will be marked dirty again and we can write it again.
+ */
+static int
+libxfs_bflush(
+       struct cache_node       *node)
 {
-       xfs_buf_t               *bp = (xfs_buf_t *)node;
+       struct xfs_buf          *bp = (struct xfs_buf *)node;
 
-       if ((bp != NULL) && (bp->b_flags & LIBXFS_B_DIRTY))
-               libxfs_writebufr(bp);
+       if (!bp->b_error && bp->b_flags & LIBXFS_B_DIRTY)
+               return libxfs_writebufr(bp);
+       return bp->b_error;
 }
 
 void
 libxfs_putbufr(xfs_buf_t *bp)
 {
+       if (bp->b_flags & LIBXFS_B_DIRTY)
+               libxfs_writebufr(bp);
        libxfs_brelse((struct cache_node *)bp);
 }
 
@@ -1308,15 +1339,54 @@ struct cache_operations libxfs_bcache_operations = {
  * Inode cache stubs.
  */
 
+kmem_zone_t            *xfs_inode_zone;
 extern kmem_zone_t     *xfs_ili_zone;
-extern kmem_zone_t     *xfs_inode_zone;
+
+/*
+ * If there are inline format data / attr forks attached to this inode,
+ * make sure they're not corrupt.
+ */
+bool
+libxfs_inode_verify_forks(
+       struct xfs_inode        *ip,
+       struct xfs_ifork_ops    *ops)
+{
+       struct xfs_ifork        *ifp;
+       xfs_failaddr_t          fa;
+
+       if (!ops)
+               return true;
+
+       fa = xfs_ifork_verify_data(ip, ops);
+       if (fa) {
+               ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
+               xfs_inode_verifier_error(ip, -EFSCORRUPTED, "data fork",
+                               ifp->if_u1.if_data, ifp->if_bytes, fa);
+               return false;
+       }
+
+       fa = xfs_ifork_verify_attr(ip, ops);
+       if (fa) {
+               ifp = XFS_IFORK_PTR(ip, XFS_ATTR_FORK);
+               xfs_inode_verifier_error(ip, -EFSCORRUPTED, "attr fork",
+                               ifp ? ifp->if_u1.if_data : NULL,
+                               ifp ? ifp->if_bytes : 0, fa);
+               return false;
+       }
+       return true;
+}
 
 int
-libxfs_iget(xfs_mount_t *mp, xfs_trans_t *tp, xfs_ino_t ino, uint lock_flags,
-               xfs_inode_t **ipp, xfs_daddr_t bno)
+libxfs_iget(
+       struct xfs_mount        *mp,
+       struct xfs_trans        *tp,
+       xfs_ino_t               ino,
+       uint                    lock_flags,
+       struct xfs_inode        **ipp,
+       struct xfs_ifork_ops    *ifork_ops)
 {
-       xfs_inode_t     *ip;
-       int             error = 0;
+       struct xfs_inode        *ip;
+       int                     error = 0;
 
        ip = kmem_zone_zalloc(xfs_inode_zone, 0);
        if (!ip)
@@ -1324,17 +1394,22 @@ libxfs_iget(xfs_mount_t *mp, xfs_trans_t *tp, xfs_ino_t ino, uint lock_flags,
 
        ip->i_ino = ino;
        ip->i_mount = mp;
-       error = xfs_iread(mp, tp, ip, bno);
+       error = xfs_iread(mp, tp, ip, 0);
        if (error) {
                kmem_zone_free(xfs_inode_zone, ip);
                *ipp = NULL;
                return error;
        }
 
+       if (!libxfs_inode_verify_forks(ip, ifork_ops)) {
+               libxfs_irele(ip);
+               return -EFSCORRUPTED;
+       }
+
        /*
         * set up the inode ops structure that the libxfs code relies on
         */
-       if (S_ISDIR(ip->i_d.di_mode))
+       if (XFS_ISDIR(ip))
                ip->d_ops = mp->m_dir_inode_ops;
        else
                ip->d_ops = mp->m_nondir_inode_ops;
@@ -1346,7 +1421,7 @@ libxfs_iget(xfs_mount_t *mp, xfs_trans_t *tp, xfs_ino_t ino, uint lock_flags,
 static void
 libxfs_idestroy(xfs_inode_t *ip)
 {
-       switch (ip->i_d.di_mode & S_IFMT) {
+       switch (VFS_I(ip)->i_mode & S_IFMT) {
                case S_IFREG:
                case S_IFDIR:
                case S_IFLNK:
@@ -1355,10 +1430,13 @@ libxfs_idestroy(xfs_inode_t *ip)
        }
        if (ip->i_afp)
                libxfs_idestroy_fork(ip, XFS_ATTR_FORK);
+       if (ip->i_cowfp)
+               xfs_idestroy_fork(ip, XFS_COW_FORK);
 }
 
 void
-libxfs_iput(xfs_inode_t *ip)
+libxfs_irele(
+       struct xfs_inode        *ip)
 {
        if (ip->i_itemp)
                kmem_zone_free(xfs_ili_zone, ip->i_itemp);