0001
0002
0003
0004
0005
0006
0007 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0008
0009 #include <linux/kernel.h>
0010 #include <linux/module.h>
0011 #include <linux/blkdev.h>
0012 #include <linux/string.h>
0013 #include <linux/of.h>
0014 #include <linux/of_address.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/pstore_blk.h>
0017 #include <linux/fs.h>
0018 #include <linux/file.h>
0019 #include <linux/init_syscalls.h>
0020 #include <linux/mount.h>
0021
0022 static long kmsg_size = CONFIG_PSTORE_BLK_KMSG_SIZE;
0023 module_param(kmsg_size, long, 0400);
0024 MODULE_PARM_DESC(kmsg_size, "kmsg dump record size in kbytes");
0025
0026 static int max_reason = CONFIG_PSTORE_BLK_MAX_REASON;
0027 module_param(max_reason, int, 0400);
0028 MODULE_PARM_DESC(max_reason,
0029 "maximum reason for kmsg dump (default 2: Oops and Panic)");
0030
0031 #if IS_ENABLED(CONFIG_PSTORE_PMSG)
0032 static long pmsg_size = CONFIG_PSTORE_BLK_PMSG_SIZE;
0033 #else
0034 static long pmsg_size = -1;
0035 #endif
0036 module_param(pmsg_size, long, 0400);
0037 MODULE_PARM_DESC(pmsg_size, "pmsg size in kbytes");
0038
0039 #if IS_ENABLED(CONFIG_PSTORE_CONSOLE)
0040 static long console_size = CONFIG_PSTORE_BLK_CONSOLE_SIZE;
0041 #else
0042 static long console_size = -1;
0043 #endif
0044 module_param(console_size, long, 0400);
0045 MODULE_PARM_DESC(console_size, "console size in kbytes");
0046
0047 #if IS_ENABLED(CONFIG_PSTORE_FTRACE)
0048 static long ftrace_size = CONFIG_PSTORE_BLK_FTRACE_SIZE;
0049 #else
0050 static long ftrace_size = -1;
0051 #endif
0052 module_param(ftrace_size, long, 0400);
0053 MODULE_PARM_DESC(ftrace_size, "ftrace size in kbytes");
0054
0055 static bool best_effort;
0056 module_param(best_effort, bool, 0400);
0057 MODULE_PARM_DESC(best_effort, "use best effort to write (i.e. do not require storage driver pstore support, default: off)");
0058
0059
0060
0061
0062
0063 static char blkdev[80] = CONFIG_PSTORE_BLK_BLKDEV;
0064 module_param_string(blkdev, blkdev, 80, 0400);
0065 MODULE_PARM_DESC(blkdev, "block device for pstore storage");
0066
0067
0068
0069
0070
0071 static DEFINE_MUTEX(pstore_blk_lock);
0072 static struct file *psblk_file;
0073 static struct pstore_device_info *pstore_device_info;
0074
0075 #define check_size(name, alignsize) ({ \
0076 long _##name_ = (name); \
0077 _##name_ = _##name_ <= 0 ? 0 : (_##name_ * 1024); \
0078 if (_##name_ & ((alignsize) - 1)) { \
0079 pr_info(#name " must align to %d\n", \
0080 (alignsize)); \
0081 _##name_ = ALIGN(name, (alignsize)); \
0082 } \
0083 _##name_; \
0084 })
0085
0086 #define verify_size(name, alignsize, enabled) { \
0087 long _##name_; \
0088 if (enabled) \
0089 _##name_ = check_size(name, alignsize); \
0090 else \
0091 _##name_ = 0; \
0092 \
0093 name = _##name_ / 1024; \
0094 dev->zone.name = _##name_; \
0095 }
0096
0097 static int __register_pstore_device(struct pstore_device_info *dev)
0098 {
0099 int ret;
0100
0101 lockdep_assert_held(&pstore_blk_lock);
0102
0103 if (!dev) {
0104 pr_err("NULL device info\n");
0105 return -EINVAL;
0106 }
0107 if (!dev->zone.total_size) {
0108 pr_err("zero sized device\n");
0109 return -EINVAL;
0110 }
0111 if (!dev->zone.read) {
0112 pr_err("no read handler for device\n");
0113 return -EINVAL;
0114 }
0115 if (!dev->zone.write) {
0116 pr_err("no write handler for device\n");
0117 return -EINVAL;
0118 }
0119
0120
0121 if (pstore_device_info)
0122 return -EBUSY;
0123
0124
0125 if (!dev->flags)
0126 dev->flags = UINT_MAX;
0127
0128
0129 verify_size(kmsg_size, 4096, dev->flags & PSTORE_FLAGS_DMESG);
0130 verify_size(pmsg_size, 4096, dev->flags & PSTORE_FLAGS_PMSG);
0131 verify_size(console_size, 4096, dev->flags & PSTORE_FLAGS_CONSOLE);
0132 verify_size(ftrace_size, 4096, dev->flags & PSTORE_FLAGS_FTRACE);
0133 dev->zone.max_reason = max_reason;
0134
0135
0136 dev->zone.name = KBUILD_MODNAME;
0137 dev->zone.owner = THIS_MODULE;
0138
0139 ret = register_pstore_zone(&dev->zone);
0140 if (ret == 0)
0141 pstore_device_info = dev;
0142
0143 return ret;
0144 }
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154 int register_pstore_device(struct pstore_device_info *dev)
0155 {
0156 int ret;
0157
0158 mutex_lock(&pstore_blk_lock);
0159 ret = __register_pstore_device(dev);
0160 mutex_unlock(&pstore_blk_lock);
0161
0162 return ret;
0163 }
0164 EXPORT_SYMBOL_GPL(register_pstore_device);
0165
0166 static void __unregister_pstore_device(struct pstore_device_info *dev)
0167 {
0168 lockdep_assert_held(&pstore_blk_lock);
0169 if (pstore_device_info && pstore_device_info == dev) {
0170 unregister_pstore_zone(&dev->zone);
0171 pstore_device_info = NULL;
0172 }
0173 }
0174
0175
0176
0177
0178
0179
0180 void unregister_pstore_device(struct pstore_device_info *dev)
0181 {
0182 mutex_lock(&pstore_blk_lock);
0183 __unregister_pstore_device(dev);
0184 mutex_unlock(&pstore_blk_lock);
0185 }
0186 EXPORT_SYMBOL_GPL(unregister_pstore_device);
0187
0188 static ssize_t psblk_generic_blk_read(char *buf, size_t bytes, loff_t pos)
0189 {
0190 return kernel_read(psblk_file, buf, bytes, &pos);
0191 }
0192
0193 static ssize_t psblk_generic_blk_write(const char *buf, size_t bytes,
0194 loff_t pos)
0195 {
0196
0197 if (in_interrupt() || irqs_disabled())
0198 return -EBUSY;
0199 return kernel_write(psblk_file, buf, bytes, &pos);
0200 }
0201
0202
0203
0204
0205 static int __register_pstore_blk(struct pstore_device_info *dev,
0206 const char *devpath)
0207 {
0208 int ret = -ENODEV;
0209
0210 lockdep_assert_held(&pstore_blk_lock);
0211
0212 psblk_file = filp_open(devpath, O_RDWR | O_DSYNC | O_NOATIME | O_EXCL, 0);
0213 if (IS_ERR(psblk_file)) {
0214 ret = PTR_ERR(psblk_file);
0215 pr_err("failed to open '%s': %d!\n", devpath, ret);
0216 goto err;
0217 }
0218
0219 if (!S_ISBLK(file_inode(psblk_file)->i_mode)) {
0220 pr_err("'%s' is not block device!\n", devpath);
0221 goto err_fput;
0222 }
0223
0224 dev->zone.total_size =
0225 bdev_nr_bytes(I_BDEV(psblk_file->f_mapping->host));
0226
0227 ret = __register_pstore_device(dev);
0228 if (ret)
0229 goto err_fput;
0230
0231 return 0;
0232
0233 err_fput:
0234 fput(psblk_file);
0235 err:
0236 psblk_file = NULL;
0237
0238 return ret;
0239 }
0240
0241
0242 int pstore_blk_get_config(struct pstore_blk_config *info)
0243 {
0244 strncpy(info->device, blkdev, 80);
0245 info->max_reason = max_reason;
0246 info->kmsg_size = check_size(kmsg_size, 4096);
0247 info->pmsg_size = check_size(pmsg_size, 4096);
0248 info->ftrace_size = check_size(ftrace_size, 4096);
0249 info->console_size = check_size(console_size, 4096);
0250
0251 return 0;
0252 }
0253 EXPORT_SYMBOL_GPL(pstore_blk_get_config);
0254
0255
0256 #ifndef MODULE
0257 static const char devname[] = "/dev/pstore-blk";
0258 static __init const char *early_boot_devpath(const char *initial_devname)
0259 {
0260
0261
0262
0263
0264
0265
0266 dev_t dev = name_to_dev_t(initial_devname);
0267
0268 if (!dev) {
0269 pr_err("failed to resolve '%s'!\n", initial_devname);
0270 return initial_devname;
0271 }
0272
0273 init_unlink(devname);
0274 init_mknod(devname, S_IFBLK | 0600, new_encode_dev(dev));
0275
0276 return devname;
0277 }
0278 #else
0279 static inline const char *early_boot_devpath(const char *initial_devname)
0280 {
0281 return initial_devname;
0282 }
0283 #endif
0284
0285 static int __init __best_effort_init(void)
0286 {
0287 struct pstore_device_info *best_effort_dev;
0288 int ret;
0289
0290
0291 if (!best_effort)
0292 return 0;
0293
0294
0295 if (!blkdev[0]) {
0296 pr_err("blkdev empty with best_effort=Y\n");
0297 return -EINVAL;
0298 }
0299
0300 best_effort_dev = kzalloc(sizeof(*best_effort_dev), GFP_KERNEL);
0301 if (!best_effort_dev)
0302 return -ENOMEM;
0303
0304 best_effort_dev->zone.read = psblk_generic_blk_read;
0305 best_effort_dev->zone.write = psblk_generic_blk_write;
0306
0307 ret = __register_pstore_blk(best_effort_dev,
0308 early_boot_devpath(blkdev));
0309 if (ret)
0310 kfree(best_effort_dev);
0311 else
0312 pr_info("attached %s (%lu) (no dedicated panic_write!)\n",
0313 blkdev, best_effort_dev->zone.total_size);
0314
0315 return ret;
0316 }
0317
0318 static void __exit __best_effort_exit(void)
0319 {
0320
0321
0322
0323
0324
0325
0326 if (psblk_file) {
0327 struct pstore_device_info *dev = pstore_device_info;
0328
0329 __unregister_pstore_device(dev);
0330 kfree(dev);
0331 fput(psblk_file);
0332 psblk_file = NULL;
0333 }
0334 }
0335
0336 static int __init pstore_blk_init(void)
0337 {
0338 int ret;
0339
0340 mutex_lock(&pstore_blk_lock);
0341 ret = __best_effort_init();
0342 mutex_unlock(&pstore_blk_lock);
0343
0344 return ret;
0345 }
0346 late_initcall(pstore_blk_init);
0347
0348 static void __exit pstore_blk_exit(void)
0349 {
0350 mutex_lock(&pstore_blk_lock);
0351 __best_effort_exit();
0352
0353 __unregister_pstore_device(pstore_device_info);
0354 mutex_unlock(&pstore_blk_lock);
0355 }
0356 module_exit(pstore_blk_exit);
0357
0358 MODULE_LICENSE("GPL");
0359 MODULE_AUTHOR("WeiXiong Liao <liaoweixiong@allwinnertech.com>");
0360 MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
0361 MODULE_DESCRIPTION("pstore backend for block devices");