xref: /openbmc/linux/fs/pstore/blk.c (revision 6f4eaea2)
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 static int __register_pstore_device(struct pstore_device_info *dev)
112 {
113 	int ret;
114 
115 	lockdep_assert_held(&pstore_blk_lock);
116 
117 	if (!dev || !dev->total_size || !dev->read || !dev->write)
118 		return -EINVAL;
119 
120 	/* someone already registered before */
121 	if (pstore_zone_info)
122 		return -EBUSY;
123 
124 	pstore_zone_info = kzalloc(sizeof(struct pstore_zone_info), GFP_KERNEL);
125 	if (!pstore_zone_info)
126 		return -ENOMEM;
127 
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) {				\
133 		long _##name_;						\
134 		if (enabled)						\
135 			_##name_ = check_size(name, alignsize);		\
136 		else							\
137 			_##name_ = 0;					\
138 		name = _##name_ / 1024;					\
139 		pstore_zone_info->name = _##name_;			\
140 	}
141 
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);
146 #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;
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;
156 
157 	ret = register_pstore_zone(pstore_zone_info);
158 	if (ret) {
159 		kfree(pstore_zone_info);
160 		pstore_zone_info = NULL;
161 	}
162 	return ret;
163 }
164 /**
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  */
173 int register_pstore_device(struct pstore_device_info *dev)
174 {
175 	int ret;
176 
177 	mutex_lock(&pstore_blk_lock);
178 	ret = __register_pstore_device(dev);
179 	mutex_unlock(&pstore_blk_lock);
180 
181 	return ret;
182 }
183 EXPORT_SYMBOL_GPL(register_pstore_device);
184 
185 static void __unregister_pstore_device(struct pstore_device_info *dev)
186 {
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;
192 	}
193 }
194 
195 /**
196  * unregister_pstore_device() - unregister non-block device from pstore/blk
197  *
198  * @dev: non-block device information
199  */
200 void unregister_pstore_device(struct pstore_device_info *dev)
201 {
202 	mutex_lock(&pstore_blk_lock);
203 	__unregister_pstore_device(dev);
204 	mutex_unlock(&pstore_blk_lock);
205 }
206 EXPORT_SYMBOL_GPL(unregister_pstore_device);
207 
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  */
218 static 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 = bdev_nr_sectors(bdev);
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 
263 static 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 
277 static 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 
301 static 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 
343 /*
344  * This takes its configuration only from the module parameters now.
345  * See psblk_get_bdev() and blkdev.
346  */
347 static int __register_pstore_blk(void)
348 {
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 */
367 	if (!binfo.devt) {
368 		pr_debug("no major\n");
369 		ret = -ENODEV;
370 		goto err_put_bdev;
371 	}
372 
373 	/* psblk_bdev must be assigned before register to pstore/blk */
374 	psblk_bdev = bdev;
375 
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;
380 
381 	ret = __register_pstore_device(&dev);
382 	if (ret)
383 		goto err_put_bdev;
384 
385 	bdevname(bdev, bdev_name);
386 	pr_info("attached %s (no dedicated panic_write!)\n", bdev_name);
387 	return 0;
388 
389 err_put_bdev:
390 	psblk_bdev = NULL;
391 	psblk_put_bdev(bdev, holder);
392 	return ret;
393 }
394 
395 static 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) {
402 		__unregister_pstore_device(&dev);
403 		psblk_put_bdev(psblk_bdev, holder);
404 		psblk_bdev = NULL;
405 	}
406 }
407 
408 /* get information of pstore/blk */
409 int 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 }
420 EXPORT_SYMBOL_GPL(pstore_blk_get_config);
421 
422 static int __init pstore_blk_init(void)
423 {
424 	int ret = 0;
425 
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);
430 
431 	return ret;
432 }
433 late_initcall(pstore_blk_init);
434 
435 static 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));
440 	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 	}
447 	mutex_unlock(&pstore_blk_lock);
448 }
449 module_exit(pstore_blk_exit);
450 
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");
455