btrfs: free-space-cache: use unaligned helpers to access data
authorDavid Sterba <dsterba@suse.com>
Tue, 15 Sep 2020 11:32:34 +0000 (13:32 +0200)
committerDavid Sterba <dsterba@suse.com>
Wed, 7 Oct 2020 10:13:23 +0000 (12:13 +0200)
The free space inode stores the tracking data, checksums etc, using the
io_ctl structure and moving the pointers. The data are generally aligned
to at least 4 bytes (u32 for CRC) so it's not completely unaligned but
for clarity we should use the proper helpers whenever a struct is
initialized from io_ctl->cur pointer.

Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/free-space-cache.c

index 8759f5a..af0013d 100644 (file)
@@ -413,8 +413,6 @@ static int io_ctl_prepare_pages(struct btrfs_io_ctl *io_ctl, bool uptodate)
 
 static void io_ctl_set_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
 {
-       __le64 *val;
-
        io_ctl_map_page(io_ctl, 1);
 
        /*
@@ -429,14 +427,13 @@ static void io_ctl_set_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
                io_ctl->size -= sizeof(u64) * 2;
        }
 
-       val = io_ctl->cur;
-       *val = cpu_to_le64(generation);
+       put_unaligned_le64(generation, io_ctl->cur);
        io_ctl->cur += sizeof(u64);
 }
 
 static int io_ctl_check_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
 {
-       __le64 *gen;
+       u64 cache_gen;
 
        /*
         * Skip the crc area.  If we don't check crcs then we just have a 64bit
@@ -451,11 +448,11 @@ static int io_ctl_check_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
                io_ctl->size -= sizeof(u64) * 2;
        }
 
-       gen = io_ctl->cur;
-       if (le64_to_cpu(*gen) != generation) {
+       cache_gen = get_unaligned_le64(io_ctl->cur);
+       if (cache_gen != generation) {
                btrfs_err_rl(io_ctl->fs_info,
                        "space cache generation (%llu) does not match inode (%llu)",
-                               *gen, generation);
+                               cache_gen, generation);
                io_ctl_unmap_page(io_ctl);
                return -EIO;
        }
@@ -525,8 +522,8 @@ static int io_ctl_add_entry(struct btrfs_io_ctl *io_ctl, u64 offset, u64 bytes,
                return -ENOSPC;
 
        entry = io_ctl->cur;
-       entry->offset = cpu_to_le64(offset);
-       entry->bytes = cpu_to_le64(bytes);
+       put_unaligned_le64(offset, &entry->offset);
+       put_unaligned_le64(bytes, &entry->bytes);
        entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP :
                BTRFS_FREE_SPACE_EXTENT;
        io_ctl->cur += sizeof(struct btrfs_free_space_entry);
@@ -599,8 +596,8 @@ static int io_ctl_read_entry(struct btrfs_io_ctl *io_ctl,
        }
 
        e = io_ctl->cur;
-       entry->offset = le64_to_cpu(e->offset);
-       entry->bytes = le64_to_cpu(e->bytes);
+       entry->offset = get_unaligned_le64(&e->offset);
+       entry->bytes = get_unaligned_le64(&e->bytes);
        *type = e->type;
        io_ctl->cur += sizeof(struct btrfs_free_space_entry);
        io_ctl->size -= sizeof(struct btrfs_free_space_entry);