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 <linux/blkdev.h>
12 #include <linux/string.h>
14 #include <linux/of_address.h>
15 #include <linux/platform_device.h>
16 #include <linux/pstore_blk.h>
18 #include <linux/file.h>
19 #include <linux/init_syscalls.h>
20 #include <linux/mount.h>
22 static long kmsg_size = CONFIG_PSTORE_BLK_KMSG_SIZE;
23 module_param(kmsg_size, long, 0400);
24 MODULE_PARM_DESC(kmsg_size, "kmsg dump record size in kbytes");
26 static int max_reason = CONFIG_PSTORE_BLK_MAX_REASON;
27 module_param(max_reason, int, 0400);
28 MODULE_PARM_DESC(max_reason,
29 "maximum reason for kmsg dump (default 2: Oops and Panic)");
31 #if IS_ENABLED(CONFIG_PSTORE_PMSG)
32 static long pmsg_size = CONFIG_PSTORE_BLK_PMSG_SIZE;
34 static long pmsg_size = -1;
36 module_param(pmsg_size, long, 0400);
37 MODULE_PARM_DESC(pmsg_size, "pmsg size in kbytes");
39 #if IS_ENABLED(CONFIG_PSTORE_CONSOLE)
40 static long console_size = CONFIG_PSTORE_BLK_CONSOLE_SIZE;
42 static long console_size = -1;
44 module_param(console_size, long, 0400);
45 MODULE_PARM_DESC(console_size, "console size in kbytes");
47 #if IS_ENABLED(CONFIG_PSTORE_FTRACE)
48 static long ftrace_size = CONFIG_PSTORE_BLK_FTRACE_SIZE;
50 static long ftrace_size = -1;
52 module_param(ftrace_size, long, 0400);
53 MODULE_PARM_DESC(ftrace_size, "ftrace size in kbytes");
55 static bool best_effort;
56 module_param(best_effort, bool, 0400);
57 MODULE_PARM_DESC(best_effort, "use best effort to write (i.e. do not require storage driver pstore support, default: off)");
60 * blkdev - the block device to use for pstore storage
61 * See Documentation/admin-guide/pstore-blk.rst for details.
63 static char blkdev[80] = CONFIG_PSTORE_BLK_BLKDEV;
64 module_param_string(blkdev, blkdev, 80, 0400);
65 MODULE_PARM_DESC(blkdev, "block device for pstore storage");
68 * All globals must only be accessed under the pstore_blk_lock
69 * during the register/unregister functions.
71 static DEFINE_MUTEX(pstore_blk_lock);
72 static struct file *psblk_file;
73 static struct pstore_device_info *pstore_device_info;
75 #define check_size(name, alignsize) ({ \
76 long _##name_ = (name); \
77 _##name_ = _##name_ <= 0 ? 0 : (_##name_ * 1024); \
78 if (_##name_ & ((alignsize) - 1)) { \
79 pr_info(#name " must align to %d\n", \
81 _##name_ = ALIGN(name, (alignsize)); \
86 #define verify_size(name, alignsize, enabled) { \
89 _##name_ = check_size(name, alignsize); \
92 /* Synchronize module parameters with resuls. */ \
93 name = _##name_ / 1024; \
94 dev->zone.name = _##name_; \
97 static int __register_pstore_device(struct pstore_device_info *dev)
101 lockdep_assert_held(&pstore_blk_lock);
104 pr_err("NULL device info\n");
107 if (!dev->zone.total_size) {
108 pr_err("zero sized device\n");
111 if (!dev->zone.read) {
112 pr_err("no read handler for device\n");
115 if (!dev->zone.write) {
116 pr_err("no write handler for device\n");
120 /* someone already registered before */
121 if (pstore_device_info)
124 /* zero means not limit on which backends to attempt to store. */
126 dev->flags = UINT_MAX;
128 /* Copy in module parameters. */
129 verify_size(kmsg_size, 4096, dev->flags & PSTORE_FLAGS_DMESG);
130 verify_size(pmsg_size, 4096, dev->flags & PSTORE_FLAGS_PMSG);
131 verify_size(console_size, 4096, dev->flags & PSTORE_FLAGS_CONSOLE);
132 verify_size(ftrace_size, 4096, dev->flags & PSTORE_FLAGS_FTRACE);
133 dev->zone.max_reason = max_reason;
135 /* Initialize required zone ownership details. */
136 dev->zone.name = KBUILD_MODNAME;
137 dev->zone.owner = THIS_MODULE;
139 ret = register_pstore_zone(&dev->zone);
141 pstore_device_info = dev;
146 * register_pstore_device() - register non-block device to pstore/blk
148 * @dev: non-block device information
152 * * Others - something error.
154 int register_pstore_device(struct pstore_device_info *dev)
158 mutex_lock(&pstore_blk_lock);
159 ret = __register_pstore_device(dev);
160 mutex_unlock(&pstore_blk_lock);
164 EXPORT_SYMBOL_GPL(register_pstore_device);
166 static void __unregister_pstore_device(struct pstore_device_info *dev)
168 lockdep_assert_held(&pstore_blk_lock);
169 if (pstore_device_info && pstore_device_info == dev) {
170 unregister_pstore_zone(&dev->zone);
171 pstore_device_info = NULL;
176 * unregister_pstore_device() - unregister non-block device from pstore/blk
178 * @dev: non-block device information
180 void unregister_pstore_device(struct pstore_device_info *dev)
182 mutex_lock(&pstore_blk_lock);
183 __unregister_pstore_device(dev);
184 mutex_unlock(&pstore_blk_lock);
186 EXPORT_SYMBOL_GPL(unregister_pstore_device);
188 static ssize_t psblk_generic_blk_read(char *buf, size_t bytes, loff_t pos)
190 return kernel_read(psblk_file, buf, bytes, &pos);
193 static ssize_t psblk_generic_blk_write(const char *buf, size_t bytes,
196 /* Console/Ftrace backend may handle buffer until flush dirty zones */
197 if (in_interrupt() || irqs_disabled())
199 return kernel_write(psblk_file, buf, bytes, &pos);
203 * This takes its configuration only from the module parameters now.
205 static int __register_pstore_blk(struct pstore_device_info *dev,
211 lockdep_assert_held(&pstore_blk_lock);
213 psblk_file = filp_open(devpath, O_RDWR | O_DSYNC | O_NOATIME | O_EXCL, 0);
214 if (IS_ERR(psblk_file)) {
215 ret = PTR_ERR(psblk_file);
216 pr_err("failed to open '%s': %d!\n", devpath, ret);
220 inode = file_inode(psblk_file);
221 if (!S_ISBLK(inode->i_mode)) {
222 pr_err("'%s' is not block device!\n", devpath);
226 inode = I_BDEV(psblk_file->f_mapping->host)->bd_inode;
227 dev->zone.total_size = i_size_read(inode);
229 ret = __register_pstore_device(dev);
243 /* get information of pstore/blk */
244 int pstore_blk_get_config(struct pstore_blk_config *info)
246 strncpy(info->device, blkdev, 80);
247 info->max_reason = max_reason;
248 info->kmsg_size = check_size(kmsg_size, 4096);
249 info->pmsg_size = check_size(pmsg_size, 4096);
250 info->ftrace_size = check_size(ftrace_size, 4096);
251 info->console_size = check_size(console_size, 4096);
255 EXPORT_SYMBOL_GPL(pstore_blk_get_config);
259 static const char devname[] = "/dev/pstore-blk";
260 static __init const char *early_boot_devpath(const char *initial_devname)
263 * During early boot the real root file system hasn't been
264 * mounted yet, and no device nodes are present yet. Use the
265 * same scheme to find the device that we use for mounting
266 * the root file system.
268 dev_t dev = name_to_dev_t(initial_devname);
271 pr_err("failed to resolve '%s'!\n", initial_devname);
272 return initial_devname;
275 init_unlink(devname);
276 init_mknod(devname, S_IFBLK | 0600, new_encode_dev(dev));
281 static inline const char *early_boot_devpath(const char *initial_devname)
283 return initial_devname;
287 static int __init __best_effort_init(void)
289 struct pstore_device_info *best_effort_dev;
292 /* No best-effort mode requested. */
296 /* Reject an empty blkdev. */
298 pr_err("blkdev empty with best_effort=Y\n");
302 best_effort_dev = kzalloc(sizeof(*best_effort_dev), GFP_KERNEL);
303 if (!best_effort_dev)
306 best_effort_dev->zone.read = psblk_generic_blk_read;
307 best_effort_dev->zone.write = psblk_generic_blk_write;
309 ret = __register_pstore_blk(best_effort_dev,
310 early_boot_devpath(blkdev));
312 kfree(best_effort_dev);
314 pr_info("attached %s (%zu) (no dedicated panic_write!)\n",
315 blkdev, best_effort_dev->zone.total_size);
320 static void __exit __best_effort_exit(void)
323 * Currently, the only user of psblk_file is best_effort, so
324 * we can assume that pstore_device_info is associated with it.
325 * Once there are "real" blk devices, there will need to be a
326 * dedicated pstore_blk_info, etc.
329 struct pstore_device_info *dev = pstore_device_info;
331 __unregister_pstore_device(dev);
338 static int __init pstore_blk_init(void)
342 mutex_lock(&pstore_blk_lock);
343 ret = __best_effort_init();
344 mutex_unlock(&pstore_blk_lock);
348 late_initcall(pstore_blk_init);
350 static void __exit pstore_blk_exit(void)
352 mutex_lock(&pstore_blk_lock);
353 __best_effort_exit();
354 /* If we've been asked to unload, unregister any remaining device. */
355 __unregister_pstore_device(pstore_device_info);
356 mutex_unlock(&pstore_blk_lock);
358 module_exit(pstore_blk_exit);
360 MODULE_LICENSE("GPL");
361 MODULE_AUTHOR("WeiXiong Liao <liaoweixiong@allwinnertech.com>");
362 MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
363 MODULE_DESCRIPTION("pstore backend for block devices");