1 // SPDX-License-Identifier: GPL-2.0
3 * Implements pstore backend driver that write to block (or non-block) storage
4 * devices, using the pstore/zone API.
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include "../../block/blk.h"
12 #include <linux/blkdev.h>
13 #include <linux/string.h>
15 #include <linux/of_address.h>
16 #include <linux/platform_device.h>
17 #include <linux/pstore_blk.h>
18 #include <linux/mount.h>
19 #include <linux/uio.h>
21 static long kmsg_size = CONFIG_PSTORE_BLK_KMSG_SIZE;
22 module_param(kmsg_size, long, 0400);
23 MODULE_PARM_DESC(kmsg_size, "kmsg dump record size in kbytes");
25 static int max_reason = CONFIG_PSTORE_BLK_MAX_REASON;
26 module_param(max_reason, int, 0400);
27 MODULE_PARM_DESC(max_reason,
28 "maximum reason for kmsg dump (default 2: Oops and Panic)");
30 #if IS_ENABLED(CONFIG_PSTORE_PMSG)
31 static long pmsg_size = CONFIG_PSTORE_BLK_PMSG_SIZE;
33 static long pmsg_size = -1;
35 module_param(pmsg_size, long, 0400);
36 MODULE_PARM_DESC(pmsg_size, "pmsg size in kbytes");
38 #if IS_ENABLED(CONFIG_PSTORE_CONSOLE)
39 static long console_size = CONFIG_PSTORE_BLK_CONSOLE_SIZE;
41 static long console_size = -1;
43 module_param(console_size, long, 0400);
44 MODULE_PARM_DESC(console_size, "console size in kbytes");
46 #if IS_ENABLED(CONFIG_PSTORE_FTRACE)
47 static long ftrace_size = CONFIG_PSTORE_BLK_FTRACE_SIZE;
49 static long ftrace_size = -1;
51 module_param(ftrace_size, long, 0400);
52 MODULE_PARM_DESC(ftrace_size, "ftrace size in kbytes");
54 static bool best_effort;
55 module_param(best_effort, bool, 0400);
56 MODULE_PARM_DESC(best_effort, "use best effort to write (i.e. do not require storage driver pstore support, default: off)");
59 * blkdev - the block device to use for pstore storage
61 * Usually, this will be a partition of a block device.
63 * blkdev accepts the following variants:
64 * 1) <hex_major><hex_minor> device number in hexadecimal representation,
65 * with no leading 0x, for example b302.
66 * 2) /dev/<disk_name> represents the device number of disk
67 * 3) /dev/<disk_name><decimal> represents the device number
68 * of partition - device number of disk plus the partition number
69 * 4) /dev/<disk_name>p<decimal> - same as the above, that form is
70 * used when disk name of partitioned disk ends on a digit.
71 * 5) PARTUUID=00112233-4455-6677-8899-AABBCCDDEEFF representing the
72 * unique id of a partition if the partition table provides it.
73 * The UUID may be either an EFI/GPT UUID, or refer to an MSDOS
74 * partition using the format SSSSSSSS-PP, where SSSSSSSS is a zero-
75 * filled hex representation of the 32-bit "NT disk signature", and PP
76 * is a zero-filled hex representation of the 1-based partition number.
77 * 6) PARTUUID=<UUID>/PARTNROFF=<int> to select a partition in relation to
78 * a partition with a known unique id.
79 * 7) <major>:<minor> major and minor number of the device separated by
82 static char blkdev[80] = CONFIG_PSTORE_BLK_BLKDEV;
83 module_param_string(blkdev, blkdev, 80, 0400);
84 MODULE_PARM_DESC(blkdev, "block device for pstore storage");
87 * All globals must only be accessed under the pstore_blk_lock
88 * during the register/unregister functions.
90 static DEFINE_MUTEX(pstore_blk_lock);
91 static struct block_device *psblk_bdev;
92 static struct pstore_zone_info *pstore_zone_info;
100 #define check_size(name, alignsize) ({ \
101 long _##name_ = (name); \
102 _##name_ = _##name_ <= 0 ? 0 : (_##name_ * 1024); \
103 if (_##name_ & ((alignsize) - 1)) { \
104 pr_info(#name " must align to %d\n", \
106 _##name_ = ALIGN(name, (alignsize)); \
111 static int __register_pstore_device(struct pstore_device_info *dev)
115 lockdep_assert_held(&pstore_blk_lock);
117 if (!dev || !dev->total_size || !dev->read || !dev->write)
120 /* someone already registered before */
121 if (pstore_zone_info)
124 pstore_zone_info = kzalloc(sizeof(struct pstore_zone_info), GFP_KERNEL);
125 if (!pstore_zone_info)
128 /* zero means not limit on which backends to attempt to store. */
130 dev->flags = UINT_MAX;
132 #define verify_size(name, alignsize, enabled) { \
135 _##name_ = check_size(name, alignsize); \
138 name = _##name_ / 1024; \
139 pstore_zone_info->name = _##name_; \
142 verify_size(kmsg_size, 4096, dev->flags & PSTORE_FLAGS_DMESG);
143 verify_size(pmsg_size, 4096, dev->flags & PSTORE_FLAGS_PMSG);
144 verify_size(console_size, 4096, dev->flags & PSTORE_FLAGS_CONSOLE);
145 verify_size(ftrace_size, 4096, dev->flags & PSTORE_FLAGS_FTRACE);
148 pstore_zone_info->total_size = dev->total_size;
149 pstore_zone_info->max_reason = max_reason;
150 pstore_zone_info->read = dev->read;
151 pstore_zone_info->write = dev->write;
152 pstore_zone_info->erase = dev->erase;
153 pstore_zone_info->panic_write = dev->panic_write;
154 pstore_zone_info->name = KBUILD_MODNAME;
155 pstore_zone_info->owner = THIS_MODULE;
157 ret = register_pstore_zone(pstore_zone_info);
159 kfree(pstore_zone_info);
160 pstore_zone_info = NULL;
165 * register_pstore_device() - register non-block device to pstore/blk
167 * @dev: non-block device information
171 * * Others - something error.
173 int register_pstore_device(struct pstore_device_info *dev)
177 mutex_lock(&pstore_blk_lock);
178 ret = __register_pstore_device(dev);
179 mutex_unlock(&pstore_blk_lock);
183 EXPORT_SYMBOL_GPL(register_pstore_device);
185 static void __unregister_pstore_device(struct pstore_device_info *dev)
187 lockdep_assert_held(&pstore_blk_lock);
188 if (pstore_zone_info && pstore_zone_info->read == dev->read) {
189 unregister_pstore_zone(pstore_zone_info);
190 kfree(pstore_zone_info);
191 pstore_zone_info = NULL;
196 * unregister_pstore_device() - unregister non-block device from pstore/blk
198 * @dev: non-block device information
200 void unregister_pstore_device(struct pstore_device_info *dev)
202 mutex_lock(&pstore_blk_lock);
203 __unregister_pstore_device(dev);
204 mutex_unlock(&pstore_blk_lock);
206 EXPORT_SYMBOL_GPL(unregister_pstore_device);
209 * psblk_get_bdev() - open block device
211 * @holder: Exclusive holder identifier
212 * @info: Information about bdev to fill in
214 * Return: pointer to block device on success and others on error.
216 * On success, the returned block_device has reference count of one.
218 static struct block_device *psblk_get_bdev(void *holder,
219 struct bdev_info *info)
221 struct block_device *bdev = ERR_PTR(-ENODEV);
222 fmode_t mode = FMODE_READ | FMODE_WRITE;
225 lockdep_assert_held(&pstore_blk_lock);
227 if (pstore_zone_info)
228 return ERR_PTR(-EBUSY);
231 return ERR_PTR(-ENODEV);
235 bdev = blkdev_get_by_path(blkdev, mode, holder);
239 devt = name_to_dev_t(blkdev);
241 return ERR_PTR(-ENODEV);
242 bdev = blkdev_get_by_dev(devt, mode, holder);
247 nr_sects = bdev_nr_sectors(bdev);
249 pr_err("not enough space for '%s'\n", blkdev);
250 blkdev_put(bdev, mode);
251 return ERR_PTR(-ENOSPC);
255 info->devt = bdev->bd_dev;
256 info->nr_sects = nr_sects;
257 info->start_sect = get_start_sect(bdev);
263 static void psblk_put_bdev(struct block_device *bdev, void *holder)
265 fmode_t mode = FMODE_READ | FMODE_WRITE;
267 lockdep_assert_held(&pstore_blk_lock);
274 blkdev_put(bdev, mode);
277 static ssize_t psblk_generic_blk_read(char *buf, size_t bytes, loff_t pos)
279 struct block_device *bdev = psblk_bdev;
282 struct iov_iter iter;
283 struct kvec iov = {.iov_base = buf, .iov_len = bytes};
288 memset(&file, 0, sizeof(struct file));
289 file.f_mapping = bdev->bd_inode->i_mapping;
290 file.f_flags = O_DSYNC | __O_SYNC | O_NOATIME;
291 file.f_inode = bdev->bd_inode;
292 file_ra_state_init(&file.f_ra, file.f_mapping);
294 init_sync_kiocb(&kiocb, &file);
296 iov_iter_kvec(&iter, READ, &iov, 1, bytes);
298 return generic_file_read_iter(&kiocb, &iter);
301 static ssize_t psblk_generic_blk_write(const char *buf, size_t bytes,
304 struct block_device *bdev = psblk_bdev;
305 struct iov_iter iter;
309 struct kvec iov = {.iov_base = (void *)buf, .iov_len = bytes};
314 /* Console/Ftrace backend may handle buffer until flush dirty zones */
315 if (in_interrupt() || irqs_disabled())
318 memset(&file, 0, sizeof(struct file));
319 file.f_mapping = bdev->bd_inode->i_mapping;
320 file.f_flags = O_DSYNC | __O_SYNC | O_NOATIME;
321 file.f_inode = bdev->bd_inode;
323 init_sync_kiocb(&kiocb, &file);
325 iov_iter_kvec(&iter, WRITE, &iov, 1, bytes);
327 inode_lock(bdev->bd_inode);
328 ret = generic_write_checks(&kiocb, &iter);
330 ret = generic_perform_write(&file, &iter, pos);
331 inode_unlock(bdev->bd_inode);
333 if (likely(ret > 0)) {
334 const struct file_operations f_op = {.fsync = blkdev_fsync};
338 ret = generic_write_sync(&kiocb, ret);
344 * This takes its configuration only from the module parameters now.
345 * See psblk_get_bdev() and blkdev.
347 static int __register_pstore_blk(void)
349 char bdev_name[BDEVNAME_SIZE];
350 struct block_device *bdev;
351 struct pstore_device_info dev;
352 struct bdev_info binfo;
353 void *holder = blkdev;
356 lockdep_assert_held(&pstore_blk_lock);
358 /* hold bdev exclusively */
359 memset(&binfo, 0, sizeof(binfo));
360 bdev = psblk_get_bdev(holder, &binfo);
362 pr_err("failed to open '%s'!\n", blkdev);
363 return PTR_ERR(bdev);
366 /* only allow driver matching the @blkdev */
368 pr_debug("no major\n");
373 /* psblk_bdev must be assigned before register to pstore/blk */
376 memset(&dev, 0, sizeof(dev));
377 dev.total_size = binfo.nr_sects << SECTOR_SHIFT;
378 dev.read = psblk_generic_blk_read;
379 dev.write = psblk_generic_blk_write;
381 ret = __register_pstore_device(&dev);
385 bdevname(bdev, bdev_name);
386 pr_info("attached %s (no dedicated panic_write!)\n", bdev_name);
391 psblk_put_bdev(bdev, holder);
395 static void __unregister_pstore_blk(unsigned int major)
397 struct pstore_device_info dev = { .read = psblk_generic_blk_read };
398 void *holder = blkdev;
400 lockdep_assert_held(&pstore_blk_lock);
401 if (psblk_bdev && MAJOR(psblk_bdev->bd_dev) == major) {
402 __unregister_pstore_device(&dev);
403 psblk_put_bdev(psblk_bdev, holder);
408 /* get information of pstore/blk */
409 int pstore_blk_get_config(struct pstore_blk_config *info)
411 strncpy(info->device, blkdev, 80);
412 info->max_reason = max_reason;
413 info->kmsg_size = check_size(kmsg_size, 4096);
414 info->pmsg_size = check_size(pmsg_size, 4096);
415 info->ftrace_size = check_size(ftrace_size, 4096);
416 info->console_size = check_size(console_size, 4096);
420 EXPORT_SYMBOL_GPL(pstore_blk_get_config);
422 static int __init pstore_blk_init(void)
426 mutex_lock(&pstore_blk_lock);
427 if (!pstore_zone_info && best_effort && blkdev[0])
428 ret = __register_pstore_blk();
429 mutex_unlock(&pstore_blk_lock);
433 late_initcall(pstore_blk_init);
435 static void __exit pstore_blk_exit(void)
437 mutex_lock(&pstore_blk_lock);
439 __unregister_pstore_blk(MAJOR(psblk_bdev->bd_dev));
441 struct pstore_device_info dev = { };
443 if (pstore_zone_info)
444 dev.read = pstore_zone_info->read;
445 __unregister_pstore_device(&dev);
447 mutex_unlock(&pstore_blk_lock);
449 module_exit(pstore_blk_exit);
451 MODULE_LICENSE("GPL");
452 MODULE_AUTHOR("WeiXiong Liao <liaoweixiong@allwinnertech.com>");
453 MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
454 MODULE_DESCRIPTION("pstore backend for block devices");