xref: /openbmc/linux/fs/pstore/blk.c (revision 2a03ddbd)
1 // 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 
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");
24 
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)");
29 
30 #if IS_ENABLED(CONFIG_PSTORE_PMSG)
31 static long pmsg_size = CONFIG_PSTORE_BLK_PMSG_SIZE;
32 #else
33 static long pmsg_size = -1;
34 #endif
35 module_param(pmsg_size, long, 0400);
36 MODULE_PARM_DESC(pmsg_size, "pmsg size in kbytes");
37 
38 #if IS_ENABLED(CONFIG_PSTORE_CONSOLE)
39 static long console_size = CONFIG_PSTORE_BLK_CONSOLE_SIZE;
40 #else
41 static long console_size = -1;
42 #endif
43 module_param(console_size, long, 0400);
44 MODULE_PARM_DESC(console_size, "console size in kbytes");
45 
46 #if IS_ENABLED(CONFIG_PSTORE_FTRACE)
47 static long ftrace_size = CONFIG_PSTORE_BLK_FTRACE_SIZE;
48 #else
49 static long ftrace_size = -1;
50 #endif
51 module_param(ftrace_size, long, 0400);
52 MODULE_PARM_DESC(ftrace_size, "ftrace size in kbytes");
53 
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)");
57 
58 /*
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  */
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");
85 
86 /*
87  * All globals must only be accessed under the pstore_blk_lock
88  * during the register/unregister functions.
89  */
90 static DEFINE_MUTEX(pstore_blk_lock);
91 static struct block_device *psblk_bdev;
92 static struct pstore_zone_info *pstore_zone_info;
93 
94 struct bdev_info {
95 	dev_t devt;
96 	sector_t nr_sects;
97 	sector_t start_sect;
98 };
99 
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",		\
105 				(alignsize));			\
106 		_##name_ = ALIGN(name, (alignsize));		\
107 	}							\
108 	_##name_;						\
109 })
110 
111 #define verify_size(name, alignsize, enabled) {			\
112 	long _##name_;						\
113 	if (enabled)						\
114 		_##name_ = check_size(name, alignsize);		\
115 	else							\
116 		_##name_ = 0;					\
117 	/* Synchronize module parameters with resuls. */	\
118 	name = _##name_ / 1024;					\
119 	pstore_zone_info->name = _##name_;			\
120 }
121 
122 static int __register_pstore_device(struct pstore_device_info *dev)
123 {
124 	int ret;
125 
126 	lockdep_assert_held(&pstore_blk_lock);
127 
128 	if (!dev) {
129 		pr_err("NULL device info\n");
130 		return -EINVAL;
131 	}
132 	if (!dev->total_size) {
133 		pr_err("zero sized device\n");
134 		return -EINVAL;
135 	}
136 	if (!dev->read) {
137 		pr_err("no read handler for device\n");
138 		return -EINVAL;
139 	}
140 	if (!dev->write) {
141 		pr_err("no write handler for device\n");
142 		return -EINVAL;
143 	}
144 
145 	/* someone already registered before */
146 	if (pstore_zone_info)
147 		return -EBUSY;
148 
149 	pstore_zone_info = kzalloc(sizeof(struct pstore_zone_info), GFP_KERNEL);
150 	if (!pstore_zone_info)
151 		return -ENOMEM;
152 
153 	/* zero means not limit on which backends to attempt to store. */
154 	if (!dev->flags)
155 		dev->flags = UINT_MAX;
156 
157 	verify_size(kmsg_size, 4096, dev->flags & PSTORE_FLAGS_DMESG);
158 	verify_size(pmsg_size, 4096, dev->flags & PSTORE_FLAGS_PMSG);
159 	verify_size(console_size, 4096, dev->flags & PSTORE_FLAGS_CONSOLE);
160 	verify_size(ftrace_size, 4096, dev->flags & PSTORE_FLAGS_FTRACE);
161 
162 	pstore_zone_info->total_size = dev->total_size;
163 	pstore_zone_info->max_reason = max_reason;
164 	pstore_zone_info->read = dev->read;
165 	pstore_zone_info->write = dev->write;
166 	pstore_zone_info->erase = dev->erase;
167 	pstore_zone_info->panic_write = dev->panic_write;
168 	pstore_zone_info->name = KBUILD_MODNAME;
169 	pstore_zone_info->owner = THIS_MODULE;
170 
171 	ret = register_pstore_zone(pstore_zone_info);
172 	if (ret) {
173 		kfree(pstore_zone_info);
174 		pstore_zone_info = NULL;
175 	}
176 	return ret;
177 }
178 /**
179  * register_pstore_device() - register non-block device to pstore/blk
180  *
181  * @dev: non-block device information
182  *
183  * Return:
184  * * 0		- OK
185  * * Others	- something error.
186  */
187 int register_pstore_device(struct pstore_device_info *dev)
188 {
189 	int ret;
190 
191 	mutex_lock(&pstore_blk_lock);
192 	ret = __register_pstore_device(dev);
193 	mutex_unlock(&pstore_blk_lock);
194 
195 	return ret;
196 }
197 EXPORT_SYMBOL_GPL(register_pstore_device);
198 
199 static void __unregister_pstore_device(struct pstore_device_info *dev)
200 {
201 	lockdep_assert_held(&pstore_blk_lock);
202 	if (pstore_zone_info && pstore_zone_info->read == dev->read) {
203 		unregister_pstore_zone(pstore_zone_info);
204 		kfree(pstore_zone_info);
205 		pstore_zone_info = NULL;
206 	}
207 }
208 
209 /**
210  * unregister_pstore_device() - unregister non-block device from pstore/blk
211  *
212  * @dev: non-block device information
213  */
214 void unregister_pstore_device(struct pstore_device_info *dev)
215 {
216 	mutex_lock(&pstore_blk_lock);
217 	__unregister_pstore_device(dev);
218 	mutex_unlock(&pstore_blk_lock);
219 }
220 EXPORT_SYMBOL_GPL(unregister_pstore_device);
221 
222 /**
223  * psblk_get_bdev() - open block device
224  *
225  * @holder:	Exclusive holder identifier
226  * @info:	Information about bdev to fill in
227  *
228  * Return: pointer to block device on success and others on error.
229  *
230  * On success, the returned block_device has reference count of one.
231  */
232 static struct block_device *psblk_get_bdev(void *holder,
233 					   struct bdev_info *info)
234 {
235 	struct block_device *bdev = ERR_PTR(-ENODEV);
236 	fmode_t mode = FMODE_READ | FMODE_WRITE;
237 	sector_t nr_sects;
238 
239 	lockdep_assert_held(&pstore_blk_lock);
240 
241 	if (pstore_zone_info)
242 		return ERR_PTR(-EBUSY);
243 
244 	if (!blkdev[0])
245 		return ERR_PTR(-ENODEV);
246 
247 	if (holder)
248 		mode |= FMODE_EXCL;
249 	bdev = blkdev_get_by_path(blkdev, mode, holder);
250 	if (IS_ERR(bdev)) {
251 		dev_t devt;
252 
253 		devt = name_to_dev_t(blkdev);
254 		if (devt == 0)
255 			return ERR_PTR(-ENODEV);
256 		bdev = blkdev_get_by_dev(devt, mode, holder);
257 		if (IS_ERR(bdev))
258 			return bdev;
259 	}
260 
261 	nr_sects = bdev_nr_sectors(bdev);
262 	if (!nr_sects) {
263 		pr_err("not enough space for '%s'\n", blkdev);
264 		blkdev_put(bdev, mode);
265 		return ERR_PTR(-ENOSPC);
266 	}
267 
268 	if (info) {
269 		info->devt = bdev->bd_dev;
270 		info->nr_sects = nr_sects;
271 		info->start_sect = get_start_sect(bdev);
272 	}
273 
274 	return bdev;
275 }
276 
277 static void psblk_put_bdev(struct block_device *bdev, void *holder)
278 {
279 	fmode_t mode = FMODE_READ | FMODE_WRITE;
280 
281 	lockdep_assert_held(&pstore_blk_lock);
282 
283 	if (!bdev)
284 		return;
285 
286 	if (holder)
287 		mode |= FMODE_EXCL;
288 	blkdev_put(bdev, mode);
289 }
290 
291 static ssize_t psblk_generic_blk_read(char *buf, size_t bytes, loff_t pos)
292 {
293 	struct block_device *bdev = psblk_bdev;
294 	struct file file;
295 	struct kiocb kiocb;
296 	struct iov_iter iter;
297 	struct kvec iov = {.iov_base = buf, .iov_len = bytes};
298 
299 	if (!bdev)
300 		return -ENODEV;
301 
302 	memset(&file, 0, sizeof(struct file));
303 	file.f_mapping = bdev->bd_inode->i_mapping;
304 	file.f_flags = O_DSYNC | __O_SYNC | O_NOATIME;
305 	file.f_inode = bdev->bd_inode;
306 	file_ra_state_init(&file.f_ra, file.f_mapping);
307 
308 	init_sync_kiocb(&kiocb, &file);
309 	kiocb.ki_pos = pos;
310 	iov_iter_kvec(&iter, READ, &iov, 1, bytes);
311 
312 	return generic_file_read_iter(&kiocb, &iter);
313 }
314 
315 static ssize_t psblk_generic_blk_write(const char *buf, size_t bytes,
316 		loff_t pos)
317 {
318 	struct block_device *bdev = psblk_bdev;
319 	struct iov_iter iter;
320 	struct kiocb kiocb;
321 	struct file file;
322 	ssize_t ret;
323 	struct kvec iov = {.iov_base = (void *)buf, .iov_len = bytes};
324 
325 	if (!bdev)
326 		return -ENODEV;
327 
328 	/* Console/Ftrace backend may handle buffer until flush dirty zones */
329 	if (in_interrupt() || irqs_disabled())
330 		return -EBUSY;
331 
332 	memset(&file, 0, sizeof(struct file));
333 	file.f_mapping = bdev->bd_inode->i_mapping;
334 	file.f_flags = O_DSYNC | __O_SYNC | O_NOATIME;
335 	file.f_inode = bdev->bd_inode;
336 
337 	init_sync_kiocb(&kiocb, &file);
338 	kiocb.ki_pos = pos;
339 	iov_iter_kvec(&iter, WRITE, &iov, 1, bytes);
340 
341 	inode_lock(bdev->bd_inode);
342 	ret = generic_write_checks(&kiocb, &iter);
343 	if (ret > 0)
344 		ret = generic_perform_write(&file, &iter, pos);
345 	inode_unlock(bdev->bd_inode);
346 
347 	if (likely(ret > 0)) {
348 		const struct file_operations f_op = {.fsync = blkdev_fsync};
349 
350 		file.f_op = &f_op;
351 		kiocb.ki_pos += ret;
352 		ret = generic_write_sync(&kiocb, ret);
353 	}
354 	return ret;
355 }
356 
357 /*
358  * This takes its configuration only from the module parameters now.
359  * See psblk_get_bdev() and blkdev.
360  */
361 static int __register_pstore_blk(void)
362 {
363 	char bdev_name[BDEVNAME_SIZE];
364 	struct block_device *bdev;
365 	struct pstore_device_info dev;
366 	struct bdev_info binfo;
367 	void *holder = blkdev;
368 	int ret = -ENODEV;
369 
370 	lockdep_assert_held(&pstore_blk_lock);
371 
372 	/* hold bdev exclusively */
373 	memset(&binfo, 0, sizeof(binfo));
374 	bdev = psblk_get_bdev(holder, &binfo);
375 	if (IS_ERR(bdev)) {
376 		pr_err("failed to open '%s'!\n", blkdev);
377 		return PTR_ERR(bdev);
378 	}
379 
380 	/* only allow driver matching the @blkdev */
381 	if (!binfo.devt) {
382 		pr_debug("no major\n");
383 		ret = -ENODEV;
384 		goto err_put_bdev;
385 	}
386 
387 	/* psblk_bdev must be assigned before register to pstore/blk */
388 	psblk_bdev = bdev;
389 
390 	memset(&dev, 0, sizeof(dev));
391 	dev.total_size = binfo.nr_sects << SECTOR_SHIFT;
392 	dev.read = psblk_generic_blk_read;
393 	dev.write = psblk_generic_blk_write;
394 
395 	ret = __register_pstore_device(&dev);
396 	if (ret)
397 		goto err_put_bdev;
398 
399 	bdevname(bdev, bdev_name);
400 	pr_info("attached %s (no dedicated panic_write!)\n", bdev_name);
401 	return 0;
402 
403 err_put_bdev:
404 	psblk_bdev = NULL;
405 	psblk_put_bdev(bdev, holder);
406 	return ret;
407 }
408 
409 static void __unregister_pstore_blk(unsigned int major)
410 {
411 	struct pstore_device_info dev = { .read = psblk_generic_blk_read };
412 	void *holder = blkdev;
413 
414 	lockdep_assert_held(&pstore_blk_lock);
415 	if (psblk_bdev && MAJOR(psblk_bdev->bd_dev) == major) {
416 		__unregister_pstore_device(&dev);
417 		psblk_put_bdev(psblk_bdev, holder);
418 		psblk_bdev = NULL;
419 	}
420 }
421 
422 /* get information of pstore/blk */
423 int pstore_blk_get_config(struct pstore_blk_config *info)
424 {
425 	strncpy(info->device, blkdev, 80);
426 	info->max_reason = max_reason;
427 	info->kmsg_size = check_size(kmsg_size, 4096);
428 	info->pmsg_size = check_size(pmsg_size, 4096);
429 	info->ftrace_size = check_size(ftrace_size, 4096);
430 	info->console_size = check_size(console_size, 4096);
431 
432 	return 0;
433 }
434 EXPORT_SYMBOL_GPL(pstore_blk_get_config);
435 
436 static int __init pstore_blk_init(void)
437 {
438 	int ret = 0;
439 
440 	mutex_lock(&pstore_blk_lock);
441 	if (!pstore_zone_info && best_effort && blkdev[0])
442 		ret = __register_pstore_blk();
443 	mutex_unlock(&pstore_blk_lock);
444 
445 	return ret;
446 }
447 late_initcall(pstore_blk_init);
448 
449 static void __exit pstore_blk_exit(void)
450 {
451 	mutex_lock(&pstore_blk_lock);
452 	if (psblk_bdev)
453 		__unregister_pstore_blk(MAJOR(psblk_bdev->bd_dev));
454 	else {
455 		struct pstore_device_info dev = { };
456 
457 		if (pstore_zone_info)
458 			dev.read = pstore_zone_info->read;
459 		__unregister_pstore_device(&dev);
460 	}
461 	mutex_unlock(&pstore_blk_lock);
462 }
463 module_exit(pstore_blk_exit);
464 
465 MODULE_LICENSE("GPL");
466 MODULE_AUTHOR("WeiXiong Liao <liaoweixiong@allwinnertech.com>");
467 MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
468 MODULE_DESCRIPTION("pstore backend for block devices");
469