From 84eba7e8d6052f59f946f0b73e60fa8d461f1e3a Mon Sep 17 00:00:00 2001 From: Manuel Bentele Date: Mon, 14 Sep 2020 12:40:32 +0200 Subject: Changed printing in kernel modules to support dynamic debug --- kernel/Kbuild | 4 +- kernel/xloop_file_fmt.c | 71 +++++++++++++++------------ kernel/xloop_file_fmt.h | 8 +++ kernel/xloop_file_fmt_qcow_cache.c | 17 ++++--- kernel/xloop_file_fmt_qcow_cluster.c | 34 ++++++------- kernel/xloop_file_fmt_qcow_main.c | 94 ++++++++++++++++++------------------ kernel/xloop_file_fmt_raw.c | 16 +++--- kernel/xloop_main.c | 28 +++++++---- kernel/xloop_main.h | 2 + 9 files changed, 149 insertions(+), 125 deletions(-) diff --git a/kernel/Kbuild b/kernel/Kbuild index e3b34ac..0f99bb5 100644 --- a/kernel/Kbuild +++ b/kernel/Kbuild @@ -2,11 +2,11 @@ # Linux kernel module xloop obj-$(CONFIG_BLK_DEV_XLOOP) += xloop.o -xloop-objs += xloop_main.o xloop_file_fmt.o +xloop-y += xloop_main.o xloop_file_fmt.o # Linux kernel module loop_file_fmt_raw obj-$(CONFIG_BLK_DEV_XLOOP_FILE_FMT_RAW) += xloop_file_fmt_raw.o # Linux kernel module loop_file_fmt_qcow obj-$(CONFIG_BLK_DEV_XLOOP_FILE_FMT_QCOW) += xloop_file_fmt_qcow.o -xloop_file_fmt_qcow-objs += xloop_file_fmt_qcow_main.o xloop_file_fmt_qcow_cluster.o xloop_file_fmt_qcow_cache.o \ No newline at end of file +xloop_file_fmt_qcow-y += xloop_file_fmt_qcow_main.o xloop_file_fmt_qcow_cluster.o xloop_file_fmt_qcow_cache.o \ No newline at end of file diff --git a/kernel/xloop_file_fmt.c b/kernel/xloop_file_fmt.c index 18672f7..6c4902f 100644 --- a/kernel/xloop_file_fmt.c +++ b/kernel/xloop_file_fmt.c @@ -7,10 +7,13 @@ * Copyright (C) 2019 Manuel Bentele */ +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + #include #include #include "xloop_file_fmt.h" +#include "xloop_main.h" /* storage for all registered file format drivers */ static struct xloop_file_fmt_driver *xloop_file_fmt_drivers[MAX_XLO_FILE_FMT] = { @@ -29,11 +32,9 @@ int xloop_file_fmt_register_driver(struct xloop_file_fmt_driver *drv) if (xloop_file_fmt_drivers[drv->file_fmt_type] == NULL) { xloop_file_fmt_drivers[drv->file_fmt_type] = drv; - printk(KERN_INFO "xloop_file_fmt: successfully registered file " - "format driver %s", drv->name); + pr_info("successfully registered file format driver %s\n", drv->name); } else { - printk(KERN_WARNING "xloop_file_fmt: driver for file format " - "already registered"); + pr_warn("driver for file format already registered\n"); ret = -EBUSY; } @@ -50,8 +51,7 @@ void xloop_file_fmt_unregister_driver(struct xloop_file_fmt_driver *drv) return; xloop_file_fmt_drivers[drv->file_fmt_type] = NULL; - printk(KERN_INFO "xloop_file_fmt: successfully unregistered file " - "format driver %s", drv->name); + pr_info("successfully unregistered file format driver %s\n", drv->name); } EXPORT_SYMBOL(xloop_file_fmt_unregister_driver); @@ -82,6 +82,13 @@ struct xloop_device *xloop_file_fmt_get_xlo(struct xloop_file_fmt *xlo_fmt) } EXPORT_SYMBOL(xloop_file_fmt_get_xlo); +struct device *xloop_file_fmt_to_dev(struct xloop_file_fmt *xlo_fmt) +{ + struct xloop_device *xlo = xloop_file_fmt_get_xlo(xlo_fmt); + return xloop_device_to_dev(xlo); +} +EXPORT_SYMBOL(xloop_file_fmt_to_dev); + int xloop_file_fmt_init(struct xloop_file_fmt *xlo_fmt, u32 file_fmt_type) { @@ -95,25 +102,25 @@ int xloop_file_fmt_init(struct xloop_file_fmt *xlo_fmt, xlo_fmt->file_fmt_type = file_fmt_type; if (xlo_fmt->file_fmt_state != file_fmt_uninitialized) { - printk(KERN_WARNING "xloop_file_fmt: file format is " - "initialized already"); + dev_warn(xloop_file_fmt_to_dev(xlo_fmt), "file format is " + "initialized already\n"); return -EINVAL; } /* check if new file format driver is registered */ if (xloop_file_fmt_drivers[xlo_fmt->file_fmt_type] == NULL) { - printk(KERN_ERR "xloop_file_fmt: file format driver is not " - "available"); + dev_err(xloop_file_fmt_to_dev(xlo_fmt), "file format driver is " + "not available\n"); return -ENODEV; } - printk(KERN_INFO "xloop_file_fmt: use file format driver %s", + dev_info(xloop_file_fmt_to_dev(xlo_fmt), "use file format driver %s\n", xloop_file_fmt_drivers[xlo_fmt->file_fmt_type]->name); drv = xloop_file_fmt_drivers[xlo_fmt->file_fmt_type]->owner; if (!try_module_get(drv)) { - printk(KERN_ERR "xloop_file_fmt: file format driver %s can not " - "be accessed", + dev_err(xloop_file_fmt_to_dev(xlo_fmt), "file format driver %s can " + "not be accessed\n", xloop_file_fmt_drivers[xlo_fmt->file_fmt_type]->name); return -ENODEV; } @@ -143,8 +150,8 @@ void xloop_file_fmt_exit(struct xloop_file_fmt *xlo_fmt) struct module *drv; if (xlo_fmt->file_fmt_state != file_fmt_initialized) { - printk(KERN_WARNING "xloop_file_fmt: file format is " - "uninitialized already"); + dev_warn(xloop_file_fmt_to_dev(xlo_fmt), "file format is " + "uninitialized already\n"); return; } @@ -166,8 +173,8 @@ int xloop_file_fmt_read(struct xloop_file_fmt *xlo_fmt, struct xloop_file_fmt_ops *ops; if (unlikely(xlo_fmt->file_fmt_state != file_fmt_initialized)) { - printk(KERN_ERR "xloop_file_fmt: file format is " - "not initialized, can not read"); + dev_err_ratelimited(xloop_file_fmt_to_dev(xlo_fmt), "file format " + "is not initialized, can not read\n"); return -EINVAL; } @@ -184,8 +191,8 @@ int xloop_file_fmt_read_aio(struct xloop_file_fmt *xlo_fmt, struct xloop_file_fmt_ops *ops; if (unlikely(xlo_fmt->file_fmt_state != file_fmt_initialized)) { - printk(KERN_ERR "xloop_file_fmt: file format is " - "not initialized, can not read aio"); + dev_err_ratelimited(xloop_file_fmt_to_dev(xlo_fmt), "file format " + "is not initialized, can not read aio\n"); return -EINVAL; } @@ -202,8 +209,8 @@ int xloop_file_fmt_write(struct xloop_file_fmt *xlo_fmt, struct xloop_file_fmt_ops *ops; if (unlikely(xlo_fmt->file_fmt_state != file_fmt_initialized)) { - printk(KERN_ERR "xloop_file_fmt: file format is " - "not initialized, can not write"); + dev_err_ratelimited(xloop_file_fmt_to_dev(xlo_fmt), "file format " + "is not initialized, can not write\n"); return -EINVAL; } @@ -220,8 +227,8 @@ int xloop_file_fmt_write_aio(struct xloop_file_fmt *xlo_fmt, struct xloop_file_fmt_ops *ops; if (unlikely(xlo_fmt->file_fmt_state != file_fmt_initialized)) { - printk(KERN_ERR "xloop_file_fmt: file format is " - "not initialized, can not write aio"); + dev_err_ratelimited(xloop_file_fmt_to_dev(xlo_fmt), "file format " + "is not initialized, can not write aio\n"); return -EINVAL; } @@ -238,8 +245,8 @@ int xloop_file_fmt_write_zeros(struct xloop_file_fmt *xlo_fmt, struct xloop_file_fmt_ops *ops; if (unlikely(xlo_fmt->file_fmt_state != file_fmt_initialized)) { - printk(KERN_ERR "xloop_file_fmt: file format is " - "not initialized, can not write zeros"); + dev_err_ratelimited(xloop_file_fmt_to_dev(xlo_fmt), "file format " + "is not initialized, can not write zeros\n"); return -EINVAL; } @@ -256,8 +263,8 @@ int xloop_file_fmt_discard(struct xloop_file_fmt *xlo_fmt, struct xloop_file_fmt_ops *ops; if (unlikely(xlo_fmt->file_fmt_state != file_fmt_initialized)) { - printk(KERN_ERR "xloop_file_fmt: file format is " - "not initialized, can not discard"); + dev_err_ratelimited(xloop_file_fmt_to_dev(xlo_fmt), "file format " + "is not initialized, can not discard\n"); return -EINVAL; } @@ -273,8 +280,8 @@ int xloop_file_fmt_flush(struct xloop_file_fmt *xlo_fmt) struct xloop_file_fmt_ops *ops; if (unlikely(xlo_fmt->file_fmt_state != file_fmt_initialized)) { - printk(KERN_ERR "xloop_file_fmt: file format is " - "not initialized, can not flush"); + dev_err_ratelimited(xloop_file_fmt_to_dev(xlo_fmt), "file format " + "is not initialized, can not flush\n"); return -EINVAL; } @@ -291,8 +298,8 @@ loff_t xloop_file_fmt_sector_size(struct xloop_file_fmt *xlo_fmt, struct xloop_file_fmt_ops *ops; if (unlikely(xlo_fmt->file_fmt_state != file_fmt_initialized)) { - printk(KERN_ERR "xloop_file_fmt: file format is " - "not initialized, can not read sector size"); + dev_err_ratelimited(xloop_file_fmt_to_dev(xlo_fmt), "file format " + "is not initialized, can not read sector size\n"); return 0; } @@ -309,6 +316,8 @@ int xloop_file_fmt_change(struct xloop_file_fmt *xlo_fmt, if (file_fmt_type_new > MAX_XLO_FILE_FMT) return -EINVAL; + dev_info(xloop_file_fmt_to_dev(xlo_fmt), "change file format\n"); + /* Unload the old file format driver if the file format is * initialized */ if (xlo_fmt->file_fmt_state == file_fmt_initialized) diff --git a/kernel/xloop_file_fmt.h b/kernel/xloop_file_fmt.h index a419f33..20589c1 100644 --- a/kernel/xloop_file_fmt.h +++ b/kernel/xloop_file_fmt.h @@ -231,6 +231,14 @@ extern int xloop_file_fmt_set_xlo(struct xloop_file_fmt *xlo_fmt, */ extern struct xloop_device *xloop_file_fmt_get_xlo(struct xloop_file_fmt *xlo_fmt); +/** + * xloop_file_fmt_to_dev - Get the xloop file format's disk device + * @xlo_fmt: xloop file format + * + * Returns a pointer to the disk device of the xloop file format's xloop device. + */ +extern inline struct device *xloop_file_fmt_to_dev(struct xloop_file_fmt *xlo_fmt); + /** * xloop_file_fmt_init - Initialize a xloop file format * @xlo_fmt: xloop file format diff --git a/kernel/xloop_file_fmt_qcow_cache.c b/kernel/xloop_file_fmt_qcow_cache.c index c0e19fb..236add5 100644 --- a/kernel/xloop_file_fmt_qcow_cache.c +++ b/kernel/xloop_file_fmt_qcow_cache.c @@ -13,6 +13,8 @@ * Copyright (C) 2019 Manuel Bentele */ +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + #include #include #include @@ -103,13 +105,13 @@ void xloop_file_fmt_qcow_cache_destroy(struct xloop_file_fmt *xlo_fmt) } static int __xloop_file_fmt_qcow_cache_entry_flush( - struct xloop_file_fmt_qcow_cache *c, int i) + struct xloop_file_fmt *xlo_fmt, struct xloop_file_fmt_qcow_cache *c, int i) { if (!c->entries[i].dirty || !c->entries[i].offset) { return 0; } else { - printk(KERN_ERR "xloop_file_fmt_qcow: Flush dirty cache tables " - "is not supported yet\n"); + dev_err_ratelimited(xloop_file_fmt_to_dev(xlo_fmt), "flush dirty " + "cache tables is not supported yet\n"); return -ENOSYS; } } @@ -130,10 +132,9 @@ static int __xloop_file_fmt_qcow_cache_do_get(struct xloop_file_fmt *xlo_fmt, ASSERT(offset != 0); if (!IS_ALIGNED(offset, c->table_size)) { - printk_ratelimited(KERN_ERR "xloop_file_fmt_qcow: Cannot get " - "entry from %s cache: offset %llx is unaligned\n", - __xloop_file_fmt_qcow_cache_get_name(xlo_fmt, c), - offset); + dev_err_ratelimited(xloop_file_fmt_to_dev(xlo_fmt), "cannot get entry " + "from %s cache: offset %llx is unaligned\n", + __xloop_file_fmt_qcow_cache_get_name(xlo_fmt, c), offset); return -EIO; } @@ -164,7 +165,7 @@ static int __xloop_file_fmt_qcow_cache_do_get(struct xloop_file_fmt *xlo_fmt, /* Cache miss: write a table back and replace it */ i = min_lru_index; - ret = __xloop_file_fmt_qcow_cache_entry_flush(c, i); + ret = __xloop_file_fmt_qcow_cache_entry_flush(xlo_fmt, c, i); if (ret < 0) { return ret; } diff --git a/kernel/xloop_file_fmt_qcow_cluster.c b/kernel/xloop_file_fmt_qcow_cluster.c index 82fbc4f..deef22b 100644 --- a/kernel/xloop_file_fmt_qcow_cluster.c +++ b/kernel/xloop_file_fmt_qcow_cluster.c @@ -10,6 +10,8 @@ * Copyright (C) 2019 Manuel Bentele */ +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + #include #include @@ -160,9 +162,8 @@ int xloop_file_fmt_qcow_cluster_get_offset(struct xloop_file_fmt *xlo_fmt, } if (xloop_file_fmt_qcow_offset_into_cluster(qcow_data, l2_offset)) { - printk_ratelimited(KERN_ERR "xloop_file_fmt_qcow: L2 table " - "offset %llx unaligned (L1 index: %llx)", l2_offset, - l1_index); + dev_err_ratelimited(xloop_file_fmt_to_dev(xlo_fmt), "L2 table offset " + "%llx unaligned (L1 index: %llx)", l2_offset, l1_index); return -EIO; } @@ -189,19 +190,18 @@ int xloop_file_fmt_qcow_cluster_get_offset(struct xloop_file_fmt *xlo_fmt, if (qcow_data->qcow_version < 3 && ( type == QCOW_CLUSTER_ZERO_PLAIN || type == QCOW_CLUSTER_ZERO_ALLOC)) { - printk_ratelimited(KERN_ERR "xloop_file_fmt_qcow: zero cluster " - "entry found in pre-v3 image (L2 offset: %llx, " - "L2 index: %x)\n", l2_offset, l2_index); + dev_err_ratelimited(xloop_file_fmt_to_dev(xlo_fmt), "zero cluster " + "entry found in pre-v3 image (L2 offset: %llx, L2 index: %x)\n", + l2_offset, l2_index); ret = -EIO; goto fail; } switch (type) { case QCOW_CLUSTER_COMPRESSED: if (xloop_file_fmt_qcow_has_data_file(xlo_fmt)) { - printk_ratelimited(KERN_ERR "xloop_file_fmt_qcow: " - "compressed cluster entry found in image with " - "external data file (L2 offset: %llx, " - "L2 index: %x)", l2_offset, l2_index); + dev_err_ratelimited(xloop_file_fmt_to_dev(xlo_fmt), "compressed " + "cluster entry found in image with external data file " + "(L2 offset: %llx, L2 index: %x)\n", l2_offset, l2_index); ret = -EIO; goto fail; } @@ -225,19 +225,17 @@ int xloop_file_fmt_qcow_cluster_get_offset(struct xloop_file_fmt *xlo_fmt, *cluster_offset &= L2E_OFFSET_MASK; if (xloop_file_fmt_qcow_offset_into_cluster(qcow_data, *cluster_offset)) { - printk_ratelimited(KERN_ERR "xloop_file_fmt_qcow: " - "cluster allocation offset %llx unaligned " - "(L2 offset: %llx, L2 index: %x)\n", - *cluster_offset, l2_offset, l2_index); + dev_err_ratelimited(xloop_file_fmt_to_dev(xlo_fmt), "cluster " + "allocation offset %llx unaligned (L2 offset: %llx, " + "L2 index: %x)\n", *cluster_offset, l2_offset, l2_index); ret = -EIO; goto fail; } if (xloop_file_fmt_qcow_has_data_file(xlo_fmt) && *cluster_offset != offset - offset_in_cluster) { - printk_ratelimited(KERN_ERR "xloop_file_fmt_qcow: " - "external data file host cluster offset %llx " - "does not match guest cluster offset: %llx, " - "L2 index: %x)", *cluster_offset, + dev_err_ratelimited(xloop_file_fmt_to_dev(xlo_fmt), "external " + "data file host cluster offset %llx does not match guest " + "cluster offset: %llx, L2 index: %x)\n", *cluster_offset, offset - offset_in_cluster, l2_index); ret = -EIO; goto fail; diff --git a/kernel/xloop_file_fmt_qcow_main.c b/kernel/xloop_file_fmt_qcow_main.c index c1fe34c..55d2f32 100644 --- a/kernel/xloop_file_fmt_qcow_main.c +++ b/kernel/xloop_file_fmt_qcow_main.c @@ -7,6 +7,8 @@ * Copyright (C) 2019 Manuel Bentele */ +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + #include #include #include @@ -27,8 +29,8 @@ #include "xloop_file_fmt_qcow_cache.h" #include "xloop_file_fmt_qcow_cluster.h" -static int __qcow_file_fmt_header_read(struct file *file, - struct xloop_file_fmt_qcow_header *header) +static int __qcow_file_fmt_header_read(struct xloop_file_fmt *xlo_fmt, + struct file *file, struct xloop_file_fmt_qcow_header *header) { ssize_t len; loff_t offset; @@ -38,8 +40,8 @@ static int __qcow_file_fmt_header_read(struct file *file, offset = 0; len = kernel_read(file, header, sizeof(*header), &offset); if (len < 0) { - printk(KERN_ERR "xloop_file_fmt_qcow: could not read QCOW " - "header"); + dev_err(xloop_file_fmt_to_dev(xlo_fmt), "could not read QCOW " + "header\n"); return len; } @@ -61,14 +63,14 @@ static int __qcow_file_fmt_header_read(struct file *file, /* check QCOW file format and header version */ if (header->magic != QCOW_MAGIC) { - printk(KERN_ERR "xloop_file_fmt_qcow: image is not in QCOW " - "format"); + dev_err(xloop_file_fmt_to_dev(xlo_fmt), "image is not in QCOW " + "format\n"); return -EINVAL; } if (header->version < 2 || header->version > 3) { - printk(KERN_ERR "xloop_file_fmt_qcow: unsupported QCOW version " - "%d", header->version); + dev_err(xloop_file_fmt_to_dev(xlo_fmt), "unsupported QCOW " + "version %d\n", header->version); return -ENOTSUPP; } @@ -90,8 +92,7 @@ static int __qcow_file_fmt_header_read(struct file *file, header->header_length = be32_to_cpu(header->header_length); if (header->header_length < 104) { - printk(KERN_ERR "xloop_file_fmt_qcow: QCOW header too " - "short"); + dev_err(xloop_file_fmt_to_dev(xlo_fmt), "QCOW header too short\n"); return -EINVAL; } } @@ -106,8 +107,7 @@ static int __qcow_file_fmt_validate_table(struct xloop_file_fmt *xlo_fmt, struct xloop_file_fmt_qcow_data *qcow_data = xlo_fmt->private_data; if (entries > max_size_bytes / entry_len) { - printk(KERN_INFO "xloop_file_fmt_qcow: %s too large", - table_name); + dev_err(xloop_file_fmt_to_dev(xlo_fmt), "%s too large\n", table_name); return -EFBIG; } @@ -116,7 +116,7 @@ static int __qcow_file_fmt_validate_table(struct xloop_file_fmt *xlo_fmt, if ((S64_MAX - entries * entry_len < offset) || ( xloop_file_fmt_qcow_offset_into_cluster(qcow_data, offset) != 0) ) { - printk(KERN_INFO "xloop_file_fmt_qcow: %s offset invalid", + dev_err(xloop_file_fmt_to_dev(xlo_fmt), "%s offset invalid", table_name); return -EINVAL; } @@ -405,7 +405,7 @@ static int qcow_file_fmt_init(struct xloop_file_fmt *xlo_fmt) xlo_fmt->private_data = qcow_data; /* read the QCOW file header */ - ret = __qcow_file_fmt_header_read(xlo->xlo_backing_file, &header); + ret = __qcow_file_fmt_header_read(xlo_fmt, xlo->xlo_backing_file, &header); if (ret) goto free_qcow_data; @@ -420,8 +420,8 @@ static int qcow_file_fmt_init(struct xloop_file_fmt *xlo_fmt) /* Initialise cluster size */ if (header.cluster_bits < QCOW_MIN_CLUSTER_BITS || header.cluster_bits > QCOW_MAX_CLUSTER_BITS) { - printk(KERN_ERR "xloop_file_fmt_qcow: unsupported cluster " - "size: 2^%d", header.cluster_bits); + dev_err(xloop_file_fmt_to_dev(xlo_fmt), "unsupported cluster size: " + "2^%d\n", header.cluster_bits); ret = -EINVAL; goto free_qcow_data; } @@ -432,22 +432,22 @@ static int qcow_file_fmt_init(struct xloop_file_fmt *xlo_fmt) (qcow_data->cluster_bits - SECTOR_SHIFT); if (header.header_length > qcow_data->cluster_size) { - printk(KERN_ERR "xloop_file_fmt_qcow: QCOW header exceeds " - "cluster size"); + dev_err(xloop_file_fmt_to_dev(xlo_fmt), "QCOW header exceeds cluster " + "size\n"); ret = -EINVAL; goto free_qcow_data; } if (header.backing_file_offset > qcow_data->cluster_size) { - printk(KERN_ERR "xloop_file_fmt_qcow: invalid backing file " - "offset"); + dev_err(xloop_file_fmt_to_dev(xlo_fmt), "invalid backing file " + "offset\n"); ret = -EINVAL; goto free_qcow_data; } if (header.backing_file_offset) { - printk(KERN_ERR "xloop_file_fmt_qcow: backing file support not " - "available"); + dev_err(xloop_file_fmt_to_dev(xlo_fmt), "backing file support not " + "available\n"); ret = -ENOTSUPP; goto free_qcow_data; } @@ -458,30 +458,30 @@ static int qcow_file_fmt_init(struct xloop_file_fmt *xlo_fmt) qcow_data->autoclear_features = header.autoclear_features; if (qcow_data->incompatible_features & QCOW_INCOMPAT_DIRTY) { - printk(KERN_ERR "xloop_file_fmt_qcow: image contains " - "inconsistent refcounts"); + dev_err(xloop_file_fmt_to_dev(xlo_fmt), "image contains inconsistent " + "refcounts\n"); ret = -EACCES; goto free_qcow_data; } if (qcow_data->incompatible_features & QCOW_INCOMPAT_CORRUPT) { - printk(KERN_ERR "xloop_file_fmt_qcow: image is corrupt; cannot " - "be opened read/write"); + dev_err(xloop_file_fmt_to_dev(xlo_fmt), "image is corrupt; cannot be " + "opened read/write\n"); ret = -EACCES; goto free_qcow_data; } if (qcow_data->incompatible_features & QCOW_INCOMPAT_DATA_FILE) { - printk(KERN_ERR "xloop_file_fmt_qcow: clusters in the external " - "data file are not refcounted"); + dev_err(xloop_file_fmt_to_dev(xlo_fmt), "clusters in the external " + "data file are not refcounted\n"); ret = -EACCES; goto free_qcow_data; } /* Check support for various header values */ if (header.refcount_order > 6) { - printk(KERN_ERR "xloop_file_fmt_qcow: reference count entry " - "width too large; may not exceed 64 bits"); + dev_err(xloop_file_fmt_to_dev(xlo_fmt), "reference count entry width " + "too large; may not exceed 64 bits\n"); ret = -EINVAL; goto free_qcow_data; } @@ -492,8 +492,8 @@ static int qcow_file_fmt_init(struct xloop_file_fmt *xlo_fmt) qcow_data->crypt_method_header = header.crypt_method; if (qcow_data->crypt_method_header) { - printk(KERN_ERR "xloop_file_fmt_qcow: encryption support not " - "available"); + dev_err(xloop_file_fmt_to_dev(xlo_fmt), "encryption support not " + "available\n"); ret = -ENOTSUPP; goto free_qcow_data; } @@ -515,8 +515,8 @@ static int qcow_file_fmt_init(struct xloop_file_fmt *xlo_fmt) (qcow_data->cluster_bits - 3); if (header.refcount_table_clusters == 0) { - printk(KERN_ERR "xloop_file_fmt_qcow: image does not contain a " - "reference count table"); + dev_err(xloop_file_fmt_to_dev(xlo_fmt), "image does not contain a " + "reference count table\n"); ret = -EINVAL; goto free_qcow_data; } @@ -555,7 +555,7 @@ static int qcow_file_fmt_init(struct xloop_file_fmt *xlo_fmt) l1_vm_state_index = xloop_file_fmt_qcow_size_to_l1(qcow_data, header.size); if (l1_vm_state_index > INT_MAX) { - printk(KERN_ERR "xloop_file_fmt_qcow: image is too big"); + dev_err(xloop_file_fmt_to_dev(xlo_fmt), "image is too big\n"); ret = -EFBIG; goto free_qcow_data; } @@ -564,7 +564,7 @@ static int qcow_file_fmt_init(struct xloop_file_fmt *xlo_fmt) /* the L1 table must contain at least enough entries to put header.size * bytes */ if (qcow_data->l1_size < qcow_data->l1_vm_state_index) { - printk(KERN_ERR "xloop_file_fmt_qcow: L1 table is too small"); + dev_err(xloop_file_fmt_to_dev(xlo_fmt), "L1 table is too small\n"); ret = -EINVAL; goto free_qcow_data; } @@ -573,8 +573,8 @@ static int qcow_file_fmt_init(struct xloop_file_fmt *xlo_fmt) qcow_data->l1_table = vzalloc(round_up(qcow_data->l1_size * sizeof(u64), 512)); if (qcow_data->l1_table == NULL) { - printk(KERN_ERR "xloop_file_fmt_qcow: could not " - "allocate L1 table"); + dev_err(xloop_file_fmt_to_dev(xlo_fmt), "could not allocate " + "L1 table\n"); ret = -ENOMEM; goto free_qcow_data; } @@ -582,8 +582,8 @@ static int qcow_file_fmt_init(struct xloop_file_fmt *xlo_fmt) qcow_data->l1_size * sizeof(u64), &qcow_data->l1_table_offset); if (len < 0) { - printk(KERN_ERR "xloop_file_fmt_qcow: could not read L1 " - "table"); + dev_err(xloop_file_fmt_to_dev(xlo_fmt), "could not read " + "L1 table\n"); ret = len; goto free_l1_table; } @@ -598,8 +598,8 @@ static int qcow_file_fmt_init(struct xloop_file_fmt *xlo_fmt) qcow_data->nb_snapshots = header.nb_snapshots; if (qcow_data->nb_snapshots > 0) { - printk(KERN_ERR "xloop_file_fmt_qcow: snapshots support not " - "available"); + dev_err(xloop_file_fmt_to_dev(xlo_fmt), "snapshots support not " + "available\n"); ret = -ENOTSUPP; goto free_l1_table; } @@ -619,7 +619,7 @@ static int qcow_file_fmt_init(struct xloop_file_fmt *xlo_fmt) } if (l2_cache_size > INT_MAX) { - printk(KERN_ERR "xloop_file_fmt_qcow: L2 cache size too big"); + dev_err(xloop_file_fmt_to_dev(xlo_fmt), "L2 cache size too big\n"); ret = -EINVAL; goto free_l1_table; } @@ -889,7 +889,7 @@ static loff_t qcow_file_fmt_sector_size(struct xloop_file_fmt *xlo_fmt, int ret; /* temporary read the QCOW file header of other QCOW image file */ - ret = __qcow_file_fmt_header_read(file, &header); + ret = __qcow_file_fmt_header_read(xlo_fmt, file, &header); if (ret) return 0; @@ -932,15 +932,13 @@ static struct xloop_file_fmt_driver qcow_file_fmt_driver = { static int __init xloop_file_fmt_qcow_init(void) { - printk(KERN_INFO "xloop_file_fmt_qcow: init xloop device QCOW file " - "format driver"); + pr_info("init xloop device QCOW file format driver\n"); return xloop_file_fmt_register_driver(&qcow_file_fmt_driver); } static void __exit xloop_file_fmt_qcow_exit(void) { - printk(KERN_INFO "xloop_file_fmt_qcow: exit xloop device QCOW file " - "format driver"); + pr_info("exit xloop device QCOW file format driver\n"); xloop_file_fmt_unregister_driver(&qcow_file_fmt_driver); } diff --git a/kernel/xloop_file_fmt_raw.c b/kernel/xloop_file_fmt_raw.c index 6c205ee..c8e90ef 100644 --- a/kernel/xloop_file_fmt_raw.c +++ b/kernel/xloop_file_fmt_raw.c @@ -7,6 +7,8 @@ * Copyright (C) 2019 Manuel Bentele */ +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + #include #include #include @@ -42,8 +44,7 @@ static inline int __raw_file_fmt_do_transfer(struct xloop_device *xlo, int cmd, if (likely(!ret)) return 0; - printk_ratelimited(KERN_ERR - "xloop_file_fmt_raw: Transfer error at byte offset %llu, length %i.\n", + pr_err_ratelimited("transfer error at byte offset %llu, length %i.\n", (unsigned long long)rblock << 9, size); return ret; } @@ -259,9 +260,8 @@ static int __raw_file_fmt_write_bvec(struct file *file, if (likely(bw == bvec->bv_len)) return 0; - printk_ratelimited(KERN_ERR - "xloop_file_fmt_raw: Write error at byte offset %llu, length " - "%i.\n", (unsigned long long)*ppos, bvec->bv_len); + pr_err_ratelimited("write error at byte offset %llu, length %i.\n", + (unsigned long long)*ppos, bvec->bv_len); if (bw >= 0) bw = -EIO; return bw; @@ -444,15 +444,13 @@ static struct xloop_file_fmt_driver raw_file_fmt_driver = { static int __init xloop_file_fmt_raw_init(void) { - printk(KERN_INFO "xloop_file_fmt_raw: init xloop device RAW file format " - "driver"); + pr_info("init xloop device RAW file format driver\n"); return xloop_file_fmt_register_driver(&raw_file_fmt_driver); } static void __exit xloop_file_fmt_raw_exit(void) { - printk(KERN_INFO "xloop_file_fmt_raw: exit xloop device RAW file format " - "driver"); + pr_info("exit xloop device RAW file format driver\n"); xloop_file_fmt_unregister_driver(&raw_file_fmt_driver); } diff --git a/kernel/xloop_main.c b/kernel/xloop_main.c index a29b644..ef7478e 100644 --- a/kernel/xloop_main.c +++ b/kernel/xloop_main.c @@ -52,6 +52,8 @@ * */ +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + #include #include #include @@ -97,6 +99,12 @@ static DEFINE_MUTEX(xloop_ctl_mutex); static int max_part; static int part_shift; +struct device *xloop_device_to_dev(struct xloop_device *xlo) +{ + return disk_to_dev(xlo->xlo_disk); +} +EXPORT_SYMBOL(xloop_device_to_dev); + static int transfer_xor(struct xloop_device *xlo, int cmd, struct page *raw_page, unsigned raw_off, struct page *xloop_page, unsigned xloop_off, @@ -349,8 +357,8 @@ static void xloop_reread_partitions(struct xloop_device *xlo, #endif mutex_unlock(&bdev->bd_mutex); if (rc) - pr_warn("%s: partition scan of xloop%d (%s) failed (rc=%d)\n", - __func__, xlo->xlo_number, xlo->xlo_file_name, rc); + dev_warn(xloop_device_to_dev(xlo), "partition scan failed (rc=%d)\n", + rc); } static inline int is_xloop_device(struct file *file) @@ -1007,8 +1015,8 @@ out_unlock: if (!release) mutex_unlock(&bdev->bd_mutex); if (err) - pr_warn("%s: partition scan of xloop%d failed (rc=%d)\n", - __func__, xlo_number, err); + dev_warn(xloop_device_to_dev(xlo), "partition scan failed " + "(rc=%d)\n", err); /* Device is gone, no point in returning error */ err = 0; } @@ -1109,8 +1117,8 @@ xloop_set_status(struct xloop_device *xlo, const struct xloop_info64 *info) if (size_changed && xlo->xlo_device->bd_inode->i_mapping->nrpages) { /* If any pages were dirtied after invalidate_bdev(), try again */ err = -EAGAIN; - pr_warn("%s: xloop%d (%s) has still dirty pages (nrpages=%lu)\n", - __func__, xlo->xlo_number, xlo->xlo_file_name, + dev_warn(xloop_device_to_dev(xlo), "xloop device has still dirty " + "pages (nrpages=%lu)\n", xlo->xlo_device->bd_inode->i_mapping->nrpages); goto out_unfreeze; } @@ -1366,8 +1374,8 @@ static int xloop_set_block_size(struct xloop_device *xlo, unsigned long arg) /* invalidate_bdev should have truncated all the pages */ if (xlo->xlo_device->bd_inode->i_mapping->nrpages) { err = -EAGAIN; - pr_warn("%s: xloop%d (%s) has still dirty pages (nrpages=%lu)\n", - __func__, xlo->xlo_number, xlo->xlo_file_name, + dev_warn(xloop_device_to_dev(xlo), "xloop device has still dirty " + "pages (nrpages=%lu)\n", xlo->xlo_device->bd_inode->i_mapping->nrpages); goto out_unfreeze; } @@ -2167,7 +2175,7 @@ static int __init xloop_init(void) xloop_add(&xlo, i); mutex_unlock(&xloop_ctl_mutex); - printk(KERN_INFO "xloop: module loaded\n"); + pr_info("module loaded\n"); return 0; misc_out: @@ -2205,6 +2213,8 @@ static void __exit xloop_exit(void) misc_deregister(&xloop_misc); mutex_unlock(&xloop_ctl_mutex); + + pr_info("exit module\n"); } module_init(xloop_init); diff --git a/kernel/xloop_main.h b/kernel/xloop_main.h index b9d0405..e45a891 100644 --- a/kernel/xloop_main.h +++ b/kernel/xloop_main.h @@ -99,6 +99,8 @@ struct xloop_func_table { struct module *owner; }; +extern inline struct device *xloop_device_to_dev(struct xloop_device *xlo); + int xloop_register_transfer(struct xloop_func_table *funcs); int xloop_unregister_transfer(int number); -- cgit v1.2.3-55-g7522