blob: 882d0bc0cfd7e97ebe8c72c63df08e3c0d262e80 [file] [log] [blame]
WeiXiong Liao17639f62020-03-25 16:54:57 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Implements pstore backend driver that write to block (or non-block) storage
4 * devices, using the pstore/zone API.
5 */
6
7#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8
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>
14#include <linux/of.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>
20
21static long kmsg_size = CONFIG_PSTORE_BLK_KMSG_SIZE;
22module_param(kmsg_size, long, 0400);
23MODULE_PARM_DESC(kmsg_size, "kmsg dump record size in kbytes");
24
25static int max_reason = CONFIG_PSTORE_BLK_MAX_REASON;
26module_param(max_reason, int, 0400);
27MODULE_PARM_DESC(max_reason,
28 "maximum reason for kmsg dump (default 2: Oops and Panic)");
29
WeiXiong Liao0dc06822020-03-25 16:54:59 +080030#if IS_ENABLED(CONFIG_PSTORE_PMSG)
31static long pmsg_size = CONFIG_PSTORE_BLK_PMSG_SIZE;
32#else
33static long pmsg_size = -1;
34#endif
35module_param(pmsg_size, long, 0400);
36MODULE_PARM_DESC(pmsg_size, "pmsg size in kbytes");
37
WeiXiong Liaocc9c4d12020-03-25 16:55:00 +080038#if IS_ENABLED(CONFIG_PSTORE_CONSOLE)
39static long console_size = CONFIG_PSTORE_BLK_CONSOLE_SIZE;
40#else
41static long console_size = -1;
42#endif
43module_param(console_size, long, 0400);
44MODULE_PARM_DESC(console_size, "console size in kbytes");
45
WeiXiong Liao34327e92020-03-25 16:55:01 +080046#if IS_ENABLED(CONFIG_PSTORE_FTRACE)
47static long ftrace_size = CONFIG_PSTORE_BLK_FTRACE_SIZE;
48#else
49static long ftrace_size = -1;
50#endif
51module_param(ftrace_size, long, 0400);
52MODULE_PARM_DESC(ftrace_size, "ftrace size in kbytes");
53
Kees Cookf8feafe2020-05-08 08:34:01 -070054static bool best_effort;
55module_param(best_effort, bool, 0400);
56MODULE_PARM_DESC(best_effort, "use best effort to write (i.e. do not require storage driver pstore support, default: off)");
57
WeiXiong Liao17639f62020-03-25 16:54:57 +080058/*
59 * blkdev - the block device to use for pstore storage
60 *
61 * Usually, this will be a partition of a block device.
62 *
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
80 * a colon.
81 */
82static char blkdev[80] = CONFIG_PSTORE_BLK_BLKDEV;
83module_param_string(blkdev, blkdev, 80, 0400);
84MODULE_PARM_DESC(blkdev, "block device for pstore storage");
85
86/*
87 * All globals must only be accessed under the pstore_blk_lock
88 * during the register/unregister functions.
89 */
90static DEFINE_MUTEX(pstore_blk_lock);
91static struct block_device *psblk_bdev;
92static struct pstore_zone_info *pstore_zone_info;
WeiXiong Liao17639f62020-03-25 16:54:57 +080093
94struct bdev_info {
95 dev_t devt;
96 sector_t nr_sects;
97 sector_t start_sect;
98};
99
WeiXiong Liao1525fb32020-03-25 16:55:04 +0800100#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", \
105 (alignsize)); \
106 _##name_ = ALIGN(name, (alignsize)); \
107 } \
108 _##name_; \
109})
110
WeiXiong Liao7dcb7842020-03-25 16:55:05 +0800111static int __register_pstore_device(struct pstore_device_info *dev)
WeiXiong Liao17639f62020-03-25 16:54:57 +0800112{
113 int ret;
114
WeiXiong Liao7dcb7842020-03-25 16:55:05 +0800115 lockdep_assert_held(&pstore_blk_lock);
116
WeiXiong Liao17639f62020-03-25 16:54:57 +0800117 if (!dev || !dev->total_size || !dev->read || !dev->write)
118 return -EINVAL;
119
WeiXiong Liao17639f62020-03-25 16:54:57 +0800120 /* someone already registered before */
WeiXiong Liao7dcb7842020-03-25 16:55:05 +0800121 if (pstore_zone_info)
WeiXiong Liao17639f62020-03-25 16:54:57 +0800122 return -EBUSY;
WeiXiong Liao7dcb7842020-03-25 16:55:05 +0800123
WeiXiong Liao17639f62020-03-25 16:54:57 +0800124 pstore_zone_info = kzalloc(sizeof(struct pstore_zone_info), GFP_KERNEL);
WeiXiong Liao7dcb7842020-03-25 16:55:05 +0800125 if (!pstore_zone_info)
WeiXiong Liao17639f62020-03-25 16:54:57 +0800126 return -ENOMEM;
WeiXiong Liao17639f62020-03-25 16:54:57 +0800127
128 /* zero means not limit on which backends to attempt to store. */
129 if (!dev->flags)
130 dev->flags = UINT_MAX;
131
132#define verify_size(name, alignsize, enabled) { \
WeiXiong Liao1525fb32020-03-25 16:55:04 +0800133 long _##name_; \
134 if (enabled) \
135 _##name_ = check_size(name, alignsize); \
136 else \
137 _##name_ = 0; \
WeiXiong Liao17639f62020-03-25 16:54:57 +0800138 name = _##name_ / 1024; \
139 pstore_zone_info->name = _##name_; \
140 }
141
142 verify_size(kmsg_size, 4096, dev->flags & PSTORE_FLAGS_DMESG);
WeiXiong Liao0dc06822020-03-25 16:54:59 +0800143 verify_size(pmsg_size, 4096, dev->flags & PSTORE_FLAGS_PMSG);
WeiXiong Liaocc9c4d12020-03-25 16:55:00 +0800144 verify_size(console_size, 4096, dev->flags & PSTORE_FLAGS_CONSOLE);
WeiXiong Liao34327e92020-03-25 16:55:01 +0800145 verify_size(ftrace_size, 4096, dev->flags & PSTORE_FLAGS_FTRACE);
WeiXiong Liao17639f62020-03-25 16:54:57 +0800146#undef verify_size
147
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;
WeiXiong Liao7dcb7842020-03-25 16:55:05 +0800152 pstore_zone_info->erase = dev->erase;
WeiXiong Liao17639f62020-03-25 16:54:57 +0800153 pstore_zone_info->panic_write = dev->panic_write;
154 pstore_zone_info->name = KBUILD_MODNAME;
155 pstore_zone_info->owner = THIS_MODULE;
156
157 ret = register_pstore_zone(pstore_zone_info);
158 if (ret) {
159 kfree(pstore_zone_info);
160 pstore_zone_info = NULL;
161 }
WeiXiong Liao17639f62020-03-25 16:54:57 +0800162 return ret;
163}
WeiXiong Liao7dcb7842020-03-25 16:55:05 +0800164/**
165 * register_pstore_device() - register non-block device to pstore/blk
166 *
167 * @dev: non-block device information
168 *
169 * Return:
170 * * 0 - OK
171 * * Others - something error.
172 */
173int register_pstore_device(struct pstore_device_info *dev)
WeiXiong Liao17639f62020-03-25 16:54:57 +0800174{
WeiXiong Liao7dcb7842020-03-25 16:55:05 +0800175 int ret;
176
WeiXiong Liao17639f62020-03-25 16:54:57 +0800177 mutex_lock(&pstore_blk_lock);
WeiXiong Liao7dcb7842020-03-25 16:55:05 +0800178 ret = __register_pstore_device(dev);
179 mutex_unlock(&pstore_blk_lock);
180
181 return ret;
182}
183EXPORT_SYMBOL_GPL(register_pstore_device);
184
185static void __unregister_pstore_device(struct pstore_device_info *dev)
186{
187 lockdep_assert_held(&pstore_blk_lock);
WeiXiong Liao17639f62020-03-25 16:54:57 +0800188 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;
192 }
WeiXiong Liao7dcb7842020-03-25 16:55:05 +0800193}
194
195/**
196 * unregister_pstore_device() - unregister non-block device from pstore/blk
197 *
198 * @dev: non-block device information
199 */
200void unregister_pstore_device(struct pstore_device_info *dev)
201{
202 mutex_lock(&pstore_blk_lock);
203 __unregister_pstore_device(dev);
WeiXiong Liao17639f62020-03-25 16:54:57 +0800204 mutex_unlock(&pstore_blk_lock);
205}
WeiXiong Liao7dcb7842020-03-25 16:55:05 +0800206EXPORT_SYMBOL_GPL(unregister_pstore_device);
WeiXiong Liao17639f62020-03-25 16:54:57 +0800207
208/**
209 * psblk_get_bdev() - open block device
210 *
211 * @holder: Exclusive holder identifier
212 * @info: Information about bdev to fill in
213 *
214 * Return: pointer to block device on success and others on error.
215 *
216 * On success, the returned block_device has reference count of one.
217 */
218static struct block_device *psblk_get_bdev(void *holder,
219 struct bdev_info *info)
220{
221 struct block_device *bdev = ERR_PTR(-ENODEV);
222 fmode_t mode = FMODE_READ | FMODE_WRITE;
223 sector_t nr_sects;
224
225 lockdep_assert_held(&pstore_blk_lock);
226
227 if (pstore_zone_info)
228 return ERR_PTR(-EBUSY);
229
230 if (!blkdev[0])
231 return ERR_PTR(-ENODEV);
232
233 if (holder)
234 mode |= FMODE_EXCL;
235 bdev = blkdev_get_by_path(blkdev, mode, holder);
236 if (IS_ERR(bdev)) {
237 dev_t devt;
238
239 devt = name_to_dev_t(blkdev);
240 if (devt == 0)
241 return ERR_PTR(-ENODEV);
242 bdev = blkdev_get_by_dev(devt, mode, holder);
243 if (IS_ERR(bdev))
244 return bdev;
245 }
246
247 nr_sects = part_nr_sects_read(bdev->bd_part);
248 if (!nr_sects) {
249 pr_err("not enough space for '%s'\n", blkdev);
250 blkdev_put(bdev, mode);
251 return ERR_PTR(-ENOSPC);
252 }
253
254 if (info) {
255 info->devt = bdev->bd_dev;
256 info->nr_sects = nr_sects;
257 info->start_sect = get_start_sect(bdev);
258 }
259
260 return bdev;
261}
262
263static void psblk_put_bdev(struct block_device *bdev, void *holder)
264{
265 fmode_t mode = FMODE_READ | FMODE_WRITE;
266
267 lockdep_assert_held(&pstore_blk_lock);
268
269 if (!bdev)
270 return;
271
272 if (holder)
273 mode |= FMODE_EXCL;
274 blkdev_put(bdev, mode);
275}
276
277static ssize_t psblk_generic_blk_read(char *buf, size_t bytes, loff_t pos)
278{
279 struct block_device *bdev = psblk_bdev;
280 struct file file;
281 struct kiocb kiocb;
282 struct iov_iter iter;
283 struct kvec iov = {.iov_base = buf, .iov_len = bytes};
284
285 if (!bdev)
286 return -ENODEV;
287
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);
293
294 init_sync_kiocb(&kiocb, &file);
295 kiocb.ki_pos = pos;
296 iov_iter_kvec(&iter, READ, &iov, 1, bytes);
297
298 return generic_file_read_iter(&kiocb, &iter);
299}
300
301static ssize_t psblk_generic_blk_write(const char *buf, size_t bytes,
302 loff_t pos)
303{
304 struct block_device *bdev = psblk_bdev;
305 struct iov_iter iter;
306 struct kiocb kiocb;
307 struct file file;
308 ssize_t ret;
309 struct kvec iov = {.iov_base = (void *)buf, .iov_len = bytes};
310
311 if (!bdev)
312 return -ENODEV;
313
314 /* Console/Ftrace backend may handle buffer until flush dirty zones */
315 if (in_interrupt() || irqs_disabled())
316 return -EBUSY;
317
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;
322
323 init_sync_kiocb(&kiocb, &file);
324 kiocb.ki_pos = pos;
325 iov_iter_kvec(&iter, WRITE, &iov, 1, bytes);
326
327 inode_lock(bdev->bd_inode);
328 ret = generic_write_checks(&kiocb, &iter);
329 if (ret > 0)
330 ret = generic_perform_write(&file, &iter, pos);
331 inode_unlock(bdev->bd_inode);
332
333 if (likely(ret > 0)) {
334 const struct file_operations f_op = {.fsync = blkdev_fsync};
335
336 file.f_op = &f_op;
337 kiocb.ki_pos += ret;
338 ret = generic_write_sync(&kiocb, ret);
339 }
340 return ret;
341}
342
Christoph Hellwigb6f8ed32020-10-16 15:20:41 +0200343/*
344 * This takes its configuration only from the module parameters now.
345 * See psblk_get_bdev() and blkdev.
346 */
347static int __register_pstore_blk(void)
WeiXiong Liao17639f62020-03-25 16:54:57 +0800348{
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;
354 int ret = -ENODEV;
355
356 lockdep_assert_held(&pstore_blk_lock);
357
358 /* hold bdev exclusively */
359 memset(&binfo, 0, sizeof(binfo));
360 bdev = psblk_get_bdev(holder, &binfo);
361 if (IS_ERR(bdev)) {
362 pr_err("failed to open '%s'!\n", blkdev);
363 return PTR_ERR(bdev);
364 }
365
366 /* only allow driver matching the @blkdev */
Christoph Hellwigb6f8ed32020-10-16 15:20:41 +0200367 if (!binfo.devt) {
368 pr_debug("no major\n");
WeiXiong Liao17639f62020-03-25 16:54:57 +0800369 ret = -ENODEV;
370 goto err_put_bdev;
371 }
372
373 /* psblk_bdev must be assigned before register to pstore/blk */
374 psblk_bdev = bdev;
WeiXiong Liao17639f62020-03-25 16:54:57 +0800375
376 memset(&dev, 0, sizeof(dev));
Christoph Hellwigb6f8ed32020-10-16 15:20:41 +0200377 dev.total_size = binfo.nr_sects << SECTOR_SHIFT;
WeiXiong Liao17639f62020-03-25 16:54:57 +0800378 dev.read = psblk_generic_blk_read;
379 dev.write = psblk_generic_blk_write;
WeiXiong Liao17639f62020-03-25 16:54:57 +0800380
WeiXiong Liao7dcb7842020-03-25 16:55:05 +0800381 ret = __register_pstore_device(&dev);
WeiXiong Liao17639f62020-03-25 16:54:57 +0800382 if (ret)
383 goto err_put_bdev;
384
385 bdevname(bdev, bdev_name);
Christoph Hellwigb6f8ed32020-10-16 15:20:41 +0200386 pr_info("attached %s (no dedicated panic_write!)\n", bdev_name);
WeiXiong Liao17639f62020-03-25 16:54:57 +0800387 return 0;
388
389err_put_bdev:
390 psblk_bdev = NULL;
WeiXiong Liao17639f62020-03-25 16:54:57 +0800391 psblk_put_bdev(bdev, holder);
392 return ret;
393}
394
WeiXiong Liao17639f62020-03-25 16:54:57 +0800395static void __unregister_pstore_blk(unsigned int major)
396{
397 struct pstore_device_info dev = { .read = psblk_generic_blk_read };
398 void *holder = blkdev;
399
400 lockdep_assert_held(&pstore_blk_lock);
401 if (psblk_bdev && MAJOR(psblk_bdev->bd_dev) == major) {
WeiXiong Liao7dcb7842020-03-25 16:55:05 +0800402 __unregister_pstore_device(&dev);
WeiXiong Liao17639f62020-03-25 16:54:57 +0800403 psblk_put_bdev(psblk_bdev, holder);
WeiXiong Liao17639f62020-03-25 16:54:57 +0800404 psblk_bdev = NULL;
405 }
406}
407
WeiXiong Liao1525fb32020-03-25 16:55:04 +0800408/* get information of pstore/blk */
409int pstore_blk_get_config(struct pstore_blk_config *info)
410{
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);
417
418 return 0;
419}
420EXPORT_SYMBOL_GPL(pstore_blk_get_config);
421
Kees Cookf8feafe2020-05-08 08:34:01 -0700422static int __init pstore_blk_init(void)
423{
Kees Cookf8feafe2020-05-08 08:34:01 -0700424 int ret = 0;
425
426 mutex_lock(&pstore_blk_lock);
427 if (!pstore_zone_info && best_effort && blkdev[0])
Christoph Hellwigb6f8ed32020-10-16 15:20:41 +0200428 ret = __register_pstore_blk();
Kees Cookf8feafe2020-05-08 08:34:01 -0700429 mutex_unlock(&pstore_blk_lock);
430
431 return ret;
432}
433late_initcall(pstore_blk_init);
434
WeiXiong Liao17639f62020-03-25 16:54:57 +0800435static void __exit pstore_blk_exit(void)
436{
437 mutex_lock(&pstore_blk_lock);
438 if (psblk_bdev)
439 __unregister_pstore_blk(MAJOR(psblk_bdev->bd_dev));
WeiXiong Liao7dcb7842020-03-25 16:55:05 +0800440 else {
441 struct pstore_device_info dev = { };
442
443 if (pstore_zone_info)
444 dev.read = pstore_zone_info->read;
445 __unregister_pstore_device(&dev);
446 }
WeiXiong Liao17639f62020-03-25 16:54:57 +0800447 mutex_unlock(&pstore_blk_lock);
448}
449module_exit(pstore_blk_exit);
450
451MODULE_LICENSE("GPL");
452MODULE_AUTHOR("WeiXiong Liao <liaoweixiong@allwinnertech.com>");
453MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
454MODULE_DESCRIPTION("pstore backend for block devices");