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 /** 98 * struct pstore_device_info - back-end pstore/blk driver structure. 99 * 100 * @total_size: The total size in bytes pstore/blk can use. It must be greater 101 * than 4096 and be multiple of 4096. 102 * @flags: Refer to macro starting with PSTORE_FLAGS defined in 103 * linux/pstore.h. It means what front-ends this device support. 104 * Zero means all backends for compatible. 105 * @read: The general read operation. Both of the function parameters 106 * @size and @offset are relative value to bock device (not the 107 * whole disk). 108 * On success, the number of bytes should be returned, others 109 * means error. 110 * @write: The same as @read, but the following error number: 111 * -EBUSY means try to write again later. 112 * @panic_write:The write operation only used for panic case. It's optional 113 * if you do not care panic log. The parameters and return value 114 * are the same as @read. 115 */ 116 struct pstore_device_info { 117 unsigned long total_size; 118 unsigned int flags; 119 pstore_zone_read_op read; 120 pstore_zone_write_op write; 121 pstore_zone_write_op panic_write; 122 }; 123 124 static int psblk_register_do(struct pstore_device_info *dev) 125 { 126 int ret; 127 128 if (!dev || !dev->total_size || !dev->read || !dev->write) 129 return -EINVAL; 130 131 mutex_lock(&pstore_blk_lock); 132 133 /* someone already registered before */ 134 if (pstore_zone_info) { 135 mutex_unlock(&pstore_blk_lock); 136 return -EBUSY; 137 } 138 pstore_zone_info = kzalloc(sizeof(struct pstore_zone_info), GFP_KERNEL); 139 if (!pstore_zone_info) { 140 mutex_unlock(&pstore_blk_lock); 141 return -ENOMEM; 142 } 143 144 /* zero means not limit on which backends to attempt to store. */ 145 if (!dev->flags) 146 dev->flags = UINT_MAX; 147 148 #define verify_size(name, alignsize, enabled) { \ 149 long _##name_ = (enabled) ? (name) : 0; \ 150 _##name_ = _##name_ <= 0 ? 0 : (_##name_ * 1024); \ 151 if (_##name_ & ((alignsize) - 1)) { \ 152 pr_info(#name " must align to %d\n", \ 153 (alignsize)); \ 154 _##name_ = ALIGN(name, (alignsize)); \ 155 } \ 156 name = _##name_ / 1024; \ 157 pstore_zone_info->name = _##name_; \ 158 } 159 160 verify_size(kmsg_size, 4096, dev->flags & PSTORE_FLAGS_DMESG); 161 verify_size(pmsg_size, 4096, dev->flags & PSTORE_FLAGS_PMSG); 162 verify_size(console_size, 4096, dev->flags & PSTORE_FLAGS_CONSOLE); 163 verify_size(ftrace_size, 4096, dev->flags & PSTORE_FLAGS_FTRACE); 164 #undef verify_size 165 166 pstore_zone_info->total_size = dev->total_size; 167 pstore_zone_info->max_reason = max_reason; 168 pstore_zone_info->read = dev->read; 169 pstore_zone_info->write = dev->write; 170 pstore_zone_info->panic_write = dev->panic_write; 171 pstore_zone_info->name = KBUILD_MODNAME; 172 pstore_zone_info->owner = THIS_MODULE; 173 174 ret = register_pstore_zone(pstore_zone_info); 175 if (ret) { 176 kfree(pstore_zone_info); 177 pstore_zone_info = NULL; 178 } 179 mutex_unlock(&pstore_blk_lock); 180 return ret; 181 } 182 183 static void psblk_unregister_do(struct pstore_device_info *dev) 184 { 185 mutex_lock(&pstore_blk_lock); 186 if (pstore_zone_info && pstore_zone_info->read == dev->read) { 187 unregister_pstore_zone(pstore_zone_info); 188 kfree(pstore_zone_info); 189 pstore_zone_info = NULL; 190 } 191 mutex_unlock(&pstore_blk_lock); 192 } 193 194 /** 195 * psblk_get_bdev() - open block device 196 * 197 * @holder: Exclusive holder identifier 198 * @info: Information about bdev to fill in 199 * 200 * Return: pointer to block device on success and others on error. 201 * 202 * On success, the returned block_device has reference count of one. 203 */ 204 static struct block_device *psblk_get_bdev(void *holder, 205 struct bdev_info *info) 206 { 207 struct block_device *bdev = ERR_PTR(-ENODEV); 208 fmode_t mode = FMODE_READ | FMODE_WRITE; 209 sector_t nr_sects; 210 211 lockdep_assert_held(&pstore_blk_lock); 212 213 if (pstore_zone_info) 214 return ERR_PTR(-EBUSY); 215 216 if (!blkdev[0]) 217 return ERR_PTR(-ENODEV); 218 219 if (holder) 220 mode |= FMODE_EXCL; 221 bdev = blkdev_get_by_path(blkdev, mode, holder); 222 if (IS_ERR(bdev)) { 223 dev_t devt; 224 225 devt = name_to_dev_t(blkdev); 226 if (devt == 0) 227 return ERR_PTR(-ENODEV); 228 bdev = blkdev_get_by_dev(devt, mode, holder); 229 if (IS_ERR(bdev)) 230 return bdev; 231 } 232 233 nr_sects = part_nr_sects_read(bdev->bd_part); 234 if (!nr_sects) { 235 pr_err("not enough space for '%s'\n", blkdev); 236 blkdev_put(bdev, mode); 237 return ERR_PTR(-ENOSPC); 238 } 239 240 if (info) { 241 info->devt = bdev->bd_dev; 242 info->nr_sects = nr_sects; 243 info->start_sect = get_start_sect(bdev); 244 } 245 246 return bdev; 247 } 248 249 static void psblk_put_bdev(struct block_device *bdev, void *holder) 250 { 251 fmode_t mode = FMODE_READ | FMODE_WRITE; 252 253 lockdep_assert_held(&pstore_blk_lock); 254 255 if (!bdev) 256 return; 257 258 if (holder) 259 mode |= FMODE_EXCL; 260 blkdev_put(bdev, mode); 261 } 262 263 static ssize_t psblk_generic_blk_read(char *buf, size_t bytes, loff_t pos) 264 { 265 struct block_device *bdev = psblk_bdev; 266 struct file file; 267 struct kiocb kiocb; 268 struct iov_iter iter; 269 struct kvec iov = {.iov_base = buf, .iov_len = bytes}; 270 271 if (!bdev) 272 return -ENODEV; 273 274 memset(&file, 0, sizeof(struct file)); 275 file.f_mapping = bdev->bd_inode->i_mapping; 276 file.f_flags = O_DSYNC | __O_SYNC | O_NOATIME; 277 file.f_inode = bdev->bd_inode; 278 file_ra_state_init(&file.f_ra, file.f_mapping); 279 280 init_sync_kiocb(&kiocb, &file); 281 kiocb.ki_pos = pos; 282 iov_iter_kvec(&iter, READ, &iov, 1, bytes); 283 284 return generic_file_read_iter(&kiocb, &iter); 285 } 286 287 static ssize_t psblk_generic_blk_write(const char *buf, size_t bytes, 288 loff_t pos) 289 { 290 struct block_device *bdev = psblk_bdev; 291 struct iov_iter iter; 292 struct kiocb kiocb; 293 struct file file; 294 ssize_t ret; 295 struct kvec iov = {.iov_base = (void *)buf, .iov_len = bytes}; 296 297 if (!bdev) 298 return -ENODEV; 299 300 /* Console/Ftrace backend may handle buffer until flush dirty zones */ 301 if (in_interrupt() || irqs_disabled()) 302 return -EBUSY; 303 304 memset(&file, 0, sizeof(struct file)); 305 file.f_mapping = bdev->bd_inode->i_mapping; 306 file.f_flags = O_DSYNC | __O_SYNC | O_NOATIME; 307 file.f_inode = bdev->bd_inode; 308 309 init_sync_kiocb(&kiocb, &file); 310 kiocb.ki_pos = pos; 311 iov_iter_kvec(&iter, WRITE, &iov, 1, bytes); 312 313 inode_lock(bdev->bd_inode); 314 ret = generic_write_checks(&kiocb, &iter); 315 if (ret > 0) 316 ret = generic_perform_write(&file, &iter, pos); 317 inode_unlock(bdev->bd_inode); 318 319 if (likely(ret > 0)) { 320 const struct file_operations f_op = {.fsync = blkdev_fsync}; 321 322 file.f_op = &f_op; 323 kiocb.ki_pos += ret; 324 ret = generic_write_sync(&kiocb, ret); 325 } 326 return ret; 327 } 328 329 static ssize_t psblk_blk_panic_write(const char *buf, size_t size, 330 loff_t off) 331 { 332 int ret; 333 334 if (!blkdev_panic_write) 335 return -EOPNOTSUPP; 336 337 /* size and off must align to SECTOR_SIZE for block device */ 338 ret = blkdev_panic_write(buf, off >> SECTOR_SHIFT, 339 size >> SECTOR_SHIFT); 340 return ret ? -EIO : size; 341 } 342 343 static int __register_pstore_blk(struct pstore_blk_info *info) 344 { 345 char bdev_name[BDEVNAME_SIZE]; 346 struct block_device *bdev; 347 struct pstore_device_info dev; 348 struct bdev_info binfo; 349 void *holder = blkdev; 350 int ret = -ENODEV; 351 352 lockdep_assert_held(&pstore_blk_lock); 353 354 /* hold bdev exclusively */ 355 memset(&binfo, 0, sizeof(binfo)); 356 bdev = psblk_get_bdev(holder, &binfo); 357 if (IS_ERR(bdev)) { 358 pr_err("failed to open '%s'!\n", blkdev); 359 return PTR_ERR(bdev); 360 } 361 362 /* only allow driver matching the @blkdev */ 363 if (!binfo.devt || MAJOR(binfo.devt) != info->major) { 364 pr_debug("invalid major %u (expect %u)\n", 365 info->major, MAJOR(binfo.devt)); 366 ret = -ENODEV; 367 goto err_put_bdev; 368 } 369 370 /* psblk_bdev must be assigned before register to pstore/blk */ 371 psblk_bdev = bdev; 372 blkdev_panic_write = info->panic_write; 373 374 /* Copy back block device details. */ 375 info->devt = binfo.devt; 376 info->nr_sects = binfo.nr_sects; 377 info->start_sect = binfo.start_sect; 378 379 memset(&dev, 0, sizeof(dev)); 380 dev.total_size = info->nr_sects << SECTOR_SHIFT; 381 dev.flags = info->flags; 382 dev.read = psblk_generic_blk_read; 383 dev.write = psblk_generic_blk_write; 384 dev.panic_write = info->panic_write ? psblk_blk_panic_write : NULL; 385 386 ret = psblk_register_do(&dev); 387 if (ret) 388 goto err_put_bdev; 389 390 bdevname(bdev, bdev_name); 391 pr_info("attached %s%s\n", bdev_name, 392 info->panic_write ? "" : " (no dedicated panic_write!)"); 393 return 0; 394 395 err_put_bdev: 396 psblk_bdev = NULL; 397 blkdev_panic_write = NULL; 398 psblk_put_bdev(bdev, holder); 399 return ret; 400 } 401 402 /** 403 * register_pstore_blk() - register block device to pstore/blk 404 * 405 * @info: details on the desired block device interface 406 * 407 * Return: 408 * * 0 - OK 409 * * Others - something error. 410 */ 411 int register_pstore_blk(struct pstore_blk_info *info) 412 { 413 int ret; 414 415 mutex_lock(&pstore_blk_lock); 416 ret = __register_pstore_blk(info); 417 mutex_unlock(&pstore_blk_lock); 418 419 return ret; 420 } 421 EXPORT_SYMBOL_GPL(register_pstore_blk); 422 423 static void __unregister_pstore_blk(unsigned int major) 424 { 425 struct pstore_device_info dev = { .read = psblk_generic_blk_read }; 426 void *holder = blkdev; 427 428 lockdep_assert_held(&pstore_blk_lock); 429 if (psblk_bdev && MAJOR(psblk_bdev->bd_dev) == major) { 430 psblk_unregister_do(&dev); 431 psblk_put_bdev(psblk_bdev, holder); 432 blkdev_panic_write = NULL; 433 psblk_bdev = NULL; 434 } 435 } 436 437 /** 438 * unregister_pstore_blk() - unregister block device from pstore/blk 439 * 440 * @major: the major device number of device 441 */ 442 void unregister_pstore_blk(unsigned int major) 443 { 444 mutex_lock(&pstore_blk_lock); 445 __unregister_pstore_blk(major); 446 mutex_unlock(&pstore_blk_lock); 447 } 448 EXPORT_SYMBOL_GPL(unregister_pstore_blk); 449 450 static void __exit pstore_blk_exit(void) 451 { 452 mutex_lock(&pstore_blk_lock); 453 if (psblk_bdev) 454 __unregister_pstore_blk(MAJOR(psblk_bdev->bd_dev)); 455 mutex_unlock(&pstore_blk_lock); 456 } 457 module_exit(pstore_blk_exit); 458 459 MODULE_LICENSE("GPL"); 460 MODULE_AUTHOR("WeiXiong Liao <liaoweixiong@allwinnertech.com>"); 461 MODULE_AUTHOR("Kees Cook <keescook@chromium.org>"); 462 MODULE_DESCRIPTION("pstore backend for block devices"); 463