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 /* 55 * blkdev - the block device to use for pstore storage 56 * 57 * Usually, this will be a partition of a block device. 58 * 59 * blkdev accepts the following variants: 60 * 1) <hex_major><hex_minor> device number in hexadecimal representation, 61 * with no leading 0x, for example b302. 62 * 2) /dev/<disk_name> represents the device number of disk 63 * 3) /dev/<disk_name><decimal> represents the device number 64 * of partition - device number of disk plus the partition number 65 * 4) /dev/<disk_name>p<decimal> - same as the above, that form is 66 * used when disk name of partitioned disk ends on a digit. 67 * 5) PARTUUID=00112233-4455-6677-8899-AABBCCDDEEFF representing the 68 * unique id of a partition if the partition table provides it. 69 * The UUID may be either an EFI/GPT UUID, or refer to an MSDOS 70 * partition using the format SSSSSSSS-PP, where SSSSSSSS is a zero- 71 * filled hex representation of the 32-bit "NT disk signature", and PP 72 * is a zero-filled hex representation of the 1-based partition number. 73 * 6) PARTUUID=<UUID>/PARTNROFF=<int> to select a partition in relation to 74 * a partition with a known unique id. 75 * 7) <major>:<minor> major and minor number of the device separated by 76 * a colon. 77 */ 78 static char blkdev[80] = CONFIG_PSTORE_BLK_BLKDEV; 79 module_param_string(blkdev, blkdev, 80, 0400); 80 MODULE_PARM_DESC(blkdev, "block device for pstore storage"); 81 82 /* 83 * All globals must only be accessed under the pstore_blk_lock 84 * during the register/unregister functions. 85 */ 86 static DEFINE_MUTEX(pstore_blk_lock); 87 static struct block_device *psblk_bdev; 88 static struct pstore_zone_info *pstore_zone_info; 89 static pstore_blk_panic_write_op blkdev_panic_write; 90 91 struct bdev_info { 92 dev_t devt; 93 sector_t nr_sects; 94 sector_t start_sect; 95 }; 96 97 #define check_size(name, alignsize) ({ \ 98 long _##name_ = (name); \ 99 _##name_ = _##name_ <= 0 ? 0 : (_##name_ * 1024); \ 100 if (_##name_ & ((alignsize) - 1)) { \ 101 pr_info(#name " must align to %d\n", \ 102 (alignsize)); \ 103 _##name_ = ALIGN(name, (alignsize)); \ 104 } \ 105 _##name_; \ 106 }) 107 108 static int __register_pstore_device(struct pstore_device_info *dev) 109 { 110 int ret; 111 112 lockdep_assert_held(&pstore_blk_lock); 113 114 if (!dev || !dev->total_size || !dev->read || !dev->write) 115 return -EINVAL; 116 117 /* someone already registered before */ 118 if (pstore_zone_info) 119 return -EBUSY; 120 121 pstore_zone_info = kzalloc(sizeof(struct pstore_zone_info), GFP_KERNEL); 122 if (!pstore_zone_info) 123 return -ENOMEM; 124 125 /* zero means not limit on which backends to attempt to store. */ 126 if (!dev->flags) 127 dev->flags = UINT_MAX; 128 129 #define verify_size(name, alignsize, enabled) { \ 130 long _##name_; \ 131 if (enabled) \ 132 _##name_ = check_size(name, alignsize); \ 133 else \ 134 _##name_ = 0; \ 135 name = _##name_ / 1024; \ 136 pstore_zone_info->name = _##name_; \ 137 } 138 139 verify_size(kmsg_size, 4096, dev->flags & PSTORE_FLAGS_DMESG); 140 verify_size(pmsg_size, 4096, dev->flags & PSTORE_FLAGS_PMSG); 141 verify_size(console_size, 4096, dev->flags & PSTORE_FLAGS_CONSOLE); 142 verify_size(ftrace_size, 4096, dev->flags & PSTORE_FLAGS_FTRACE); 143 #undef verify_size 144 145 pstore_zone_info->total_size = dev->total_size; 146 pstore_zone_info->max_reason = max_reason; 147 pstore_zone_info->read = dev->read; 148 pstore_zone_info->write = dev->write; 149 pstore_zone_info->erase = dev->erase; 150 pstore_zone_info->panic_write = dev->panic_write; 151 pstore_zone_info->name = KBUILD_MODNAME; 152 pstore_zone_info->owner = THIS_MODULE; 153 154 ret = register_pstore_zone(pstore_zone_info); 155 if (ret) { 156 kfree(pstore_zone_info); 157 pstore_zone_info = NULL; 158 } 159 return ret; 160 } 161 /** 162 * register_pstore_device() - register non-block device to pstore/blk 163 * 164 * @dev: non-block device information 165 * 166 * Return: 167 * * 0 - OK 168 * * Others - something error. 169 */ 170 int register_pstore_device(struct pstore_device_info *dev) 171 { 172 int ret; 173 174 mutex_lock(&pstore_blk_lock); 175 ret = __register_pstore_device(dev); 176 mutex_unlock(&pstore_blk_lock); 177 178 return ret; 179 } 180 EXPORT_SYMBOL_GPL(register_pstore_device); 181 182 static void __unregister_pstore_device(struct pstore_device_info *dev) 183 { 184 lockdep_assert_held(&pstore_blk_lock); 185 if (pstore_zone_info && pstore_zone_info->read == dev->read) { 186 unregister_pstore_zone(pstore_zone_info); 187 kfree(pstore_zone_info); 188 pstore_zone_info = NULL; 189 } 190 } 191 192 /** 193 * unregister_pstore_device() - unregister non-block device from pstore/blk 194 * 195 * @dev: non-block device information 196 */ 197 void unregister_pstore_device(struct pstore_device_info *dev) 198 { 199 mutex_lock(&pstore_blk_lock); 200 __unregister_pstore_device(dev); 201 mutex_unlock(&pstore_blk_lock); 202 } 203 EXPORT_SYMBOL_GPL(unregister_pstore_device); 204 205 /** 206 * psblk_get_bdev() - open block device 207 * 208 * @holder: Exclusive holder identifier 209 * @info: Information about bdev to fill in 210 * 211 * Return: pointer to block device on success and others on error. 212 * 213 * On success, the returned block_device has reference count of one. 214 */ 215 static struct block_device *psblk_get_bdev(void *holder, 216 struct bdev_info *info) 217 { 218 struct block_device *bdev = ERR_PTR(-ENODEV); 219 fmode_t mode = FMODE_READ | FMODE_WRITE; 220 sector_t nr_sects; 221 222 lockdep_assert_held(&pstore_blk_lock); 223 224 if (pstore_zone_info) 225 return ERR_PTR(-EBUSY); 226 227 if (!blkdev[0]) 228 return ERR_PTR(-ENODEV); 229 230 if (holder) 231 mode |= FMODE_EXCL; 232 bdev = blkdev_get_by_path(blkdev, mode, holder); 233 if (IS_ERR(bdev)) { 234 dev_t devt; 235 236 devt = name_to_dev_t(blkdev); 237 if (devt == 0) 238 return ERR_PTR(-ENODEV); 239 bdev = blkdev_get_by_dev(devt, mode, holder); 240 if (IS_ERR(bdev)) 241 return bdev; 242 } 243 244 nr_sects = part_nr_sects_read(bdev->bd_part); 245 if (!nr_sects) { 246 pr_err("not enough space for '%s'\n", blkdev); 247 blkdev_put(bdev, mode); 248 return ERR_PTR(-ENOSPC); 249 } 250 251 if (info) { 252 info->devt = bdev->bd_dev; 253 info->nr_sects = nr_sects; 254 info->start_sect = get_start_sect(bdev); 255 } 256 257 return bdev; 258 } 259 260 static void psblk_put_bdev(struct block_device *bdev, void *holder) 261 { 262 fmode_t mode = FMODE_READ | FMODE_WRITE; 263 264 lockdep_assert_held(&pstore_blk_lock); 265 266 if (!bdev) 267 return; 268 269 if (holder) 270 mode |= FMODE_EXCL; 271 blkdev_put(bdev, mode); 272 } 273 274 static ssize_t psblk_generic_blk_read(char *buf, size_t bytes, loff_t pos) 275 { 276 struct block_device *bdev = psblk_bdev; 277 struct file file; 278 struct kiocb kiocb; 279 struct iov_iter iter; 280 struct kvec iov = {.iov_base = buf, .iov_len = bytes}; 281 282 if (!bdev) 283 return -ENODEV; 284 285 memset(&file, 0, sizeof(struct file)); 286 file.f_mapping = bdev->bd_inode->i_mapping; 287 file.f_flags = O_DSYNC | __O_SYNC | O_NOATIME; 288 file.f_inode = bdev->bd_inode; 289 file_ra_state_init(&file.f_ra, file.f_mapping); 290 291 init_sync_kiocb(&kiocb, &file); 292 kiocb.ki_pos = pos; 293 iov_iter_kvec(&iter, READ, &iov, 1, bytes); 294 295 return generic_file_read_iter(&kiocb, &iter); 296 } 297 298 static ssize_t psblk_generic_blk_write(const char *buf, size_t bytes, 299 loff_t pos) 300 { 301 struct block_device *bdev = psblk_bdev; 302 struct iov_iter iter; 303 struct kiocb kiocb; 304 struct file file; 305 ssize_t ret; 306 struct kvec iov = {.iov_base = (void *)buf, .iov_len = bytes}; 307 308 if (!bdev) 309 return -ENODEV; 310 311 /* Console/Ftrace backend may handle buffer until flush dirty zones */ 312 if (in_interrupt() || irqs_disabled()) 313 return -EBUSY; 314 315 memset(&file, 0, sizeof(struct file)); 316 file.f_mapping = bdev->bd_inode->i_mapping; 317 file.f_flags = O_DSYNC | __O_SYNC | O_NOATIME; 318 file.f_inode = bdev->bd_inode; 319 320 init_sync_kiocb(&kiocb, &file); 321 kiocb.ki_pos = pos; 322 iov_iter_kvec(&iter, WRITE, &iov, 1, bytes); 323 324 inode_lock(bdev->bd_inode); 325 ret = generic_write_checks(&kiocb, &iter); 326 if (ret > 0) 327 ret = generic_perform_write(&file, &iter, pos); 328 inode_unlock(bdev->bd_inode); 329 330 if (likely(ret > 0)) { 331 const struct file_operations f_op = {.fsync = blkdev_fsync}; 332 333 file.f_op = &f_op; 334 kiocb.ki_pos += ret; 335 ret = generic_write_sync(&kiocb, ret); 336 } 337 return ret; 338 } 339 340 static ssize_t psblk_blk_panic_write(const char *buf, size_t size, 341 loff_t off) 342 { 343 int ret; 344 345 if (!blkdev_panic_write) 346 return -EOPNOTSUPP; 347 348 /* size and off must align to SECTOR_SIZE for block device */ 349 ret = blkdev_panic_write(buf, off >> SECTOR_SHIFT, 350 size >> SECTOR_SHIFT); 351 /* try next zone */ 352 if (ret == -ENOMSG) 353 return ret; 354 return ret ? -EIO : size; 355 } 356 357 static int __register_pstore_blk(struct pstore_blk_info *info) 358 { 359 char bdev_name[BDEVNAME_SIZE]; 360 struct block_device *bdev; 361 struct pstore_device_info dev; 362 struct bdev_info binfo; 363 void *holder = blkdev; 364 int ret = -ENODEV; 365 366 lockdep_assert_held(&pstore_blk_lock); 367 368 /* hold bdev exclusively */ 369 memset(&binfo, 0, sizeof(binfo)); 370 bdev = psblk_get_bdev(holder, &binfo); 371 if (IS_ERR(bdev)) { 372 pr_err("failed to open '%s'!\n", blkdev); 373 return PTR_ERR(bdev); 374 } 375 376 /* only allow driver matching the @blkdev */ 377 if (!binfo.devt || MAJOR(binfo.devt) != info->major) { 378 pr_debug("invalid major %u (expect %u)\n", 379 info->major, MAJOR(binfo.devt)); 380 ret = -ENODEV; 381 goto err_put_bdev; 382 } 383 384 /* psblk_bdev must be assigned before register to pstore/blk */ 385 psblk_bdev = bdev; 386 blkdev_panic_write = info->panic_write; 387 388 /* Copy back block device details. */ 389 info->devt = binfo.devt; 390 info->nr_sects = binfo.nr_sects; 391 info->start_sect = binfo.start_sect; 392 393 memset(&dev, 0, sizeof(dev)); 394 dev.total_size = info->nr_sects << SECTOR_SHIFT; 395 dev.flags = info->flags; 396 dev.read = psblk_generic_blk_read; 397 dev.write = psblk_generic_blk_write; 398 dev.erase = NULL; 399 dev.panic_write = info->panic_write ? psblk_blk_panic_write : NULL; 400 401 ret = __register_pstore_device(&dev); 402 if (ret) 403 goto err_put_bdev; 404 405 bdevname(bdev, bdev_name); 406 pr_info("attached %s%s\n", bdev_name, 407 info->panic_write ? "" : " (no dedicated panic_write!)"); 408 return 0; 409 410 err_put_bdev: 411 psblk_bdev = NULL; 412 blkdev_panic_write = NULL; 413 psblk_put_bdev(bdev, holder); 414 return ret; 415 } 416 417 /** 418 * register_pstore_blk() - register block device to pstore/blk 419 * 420 * @info: details on the desired block device interface 421 * 422 * Return: 423 * * 0 - OK 424 * * Others - something error. 425 */ 426 int register_pstore_blk(struct pstore_blk_info *info) 427 { 428 int ret; 429 430 mutex_lock(&pstore_blk_lock); 431 ret = __register_pstore_blk(info); 432 mutex_unlock(&pstore_blk_lock); 433 434 return ret; 435 } 436 EXPORT_SYMBOL_GPL(register_pstore_blk); 437 438 static void __unregister_pstore_blk(unsigned int major) 439 { 440 struct pstore_device_info dev = { .read = psblk_generic_blk_read }; 441 void *holder = blkdev; 442 443 lockdep_assert_held(&pstore_blk_lock); 444 if (psblk_bdev && MAJOR(psblk_bdev->bd_dev) == major) { 445 __unregister_pstore_device(&dev); 446 psblk_put_bdev(psblk_bdev, holder); 447 blkdev_panic_write = NULL; 448 psblk_bdev = NULL; 449 } 450 } 451 452 /** 453 * unregister_pstore_blk() - unregister block device from pstore/blk 454 * 455 * @major: the major device number of device 456 */ 457 void unregister_pstore_blk(unsigned int major) 458 { 459 mutex_lock(&pstore_blk_lock); 460 __unregister_pstore_blk(major); 461 mutex_unlock(&pstore_blk_lock); 462 } 463 EXPORT_SYMBOL_GPL(unregister_pstore_blk); 464 465 /* get information of pstore/blk */ 466 int pstore_blk_get_config(struct pstore_blk_config *info) 467 { 468 strncpy(info->device, blkdev, 80); 469 info->max_reason = max_reason; 470 info->kmsg_size = check_size(kmsg_size, 4096); 471 info->pmsg_size = check_size(pmsg_size, 4096); 472 info->ftrace_size = check_size(ftrace_size, 4096); 473 info->console_size = check_size(console_size, 4096); 474 475 return 0; 476 } 477 EXPORT_SYMBOL_GPL(pstore_blk_get_config); 478 479 static void __exit pstore_blk_exit(void) 480 { 481 mutex_lock(&pstore_blk_lock); 482 if (psblk_bdev) 483 __unregister_pstore_blk(MAJOR(psblk_bdev->bd_dev)); 484 else { 485 struct pstore_device_info dev = { }; 486 487 if (pstore_zone_info) 488 dev.read = pstore_zone_info->read; 489 __unregister_pstore_device(&dev); 490 } 491 mutex_unlock(&pstore_blk_lock); 492 } 493 module_exit(pstore_blk_exit); 494 495 MODULE_LICENSE("GPL"); 496 MODULE_AUTHOR("WeiXiong Liao <liaoweixiong@allwinnertech.com>"); 497 MODULE_AUTHOR("Kees Cook <keescook@chromium.org>"); 498 MODULE_DESCRIPTION("pstore backend for block devices"); 499