Skip to content

Commit 7dcb784

Browse files
WeiXiong Liaokees
authored andcommitted
pstore/blk: Support non-block storage devices
Add support for non-block devices (e.g. MTD). A non-block driver calls pstore_blk_register_device() to register iself. In addition, pstore/zone is updated to handle non-block devices, where an erase must be done before a write. Without this, there is no way to remove records stored to an MTD. Signed-off-by: WeiXiong Liao <[email protected]> Link: https://lore.kernel.org/lkml/[email protected]/ Co-developed-by: Kees Cook <[email protected]> Signed-off-by: Kees Cook <[email protected]>
1 parent 1525fb3 commit 7dcb784

File tree

5 files changed

+114
-48
lines changed

5 files changed

+114
-48
lines changed

Documentation/admin-guide/pstore-blk.rst

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ Introduction
77
------------
88

99
pstore block (pstore/blk) is an oops/panic logger that writes its logs to a
10-
block device before the system crashes. You can get these log files by
11-
mounting pstore filesystem like::
10+
block device and non-block device before the system crashes. You can get
11+
these log files by mounting pstore filesystem like::
1212

1313
mount -t pstore pstore /sys/fs/pstore
1414

@@ -24,8 +24,8 @@ Configurations for user determine how pstore/blk works, such as pmsg_size,
2424
kmsg_size and so on. All of them support both Kconfig and module parameters,
2525
but module parameters have priority over Kconfig.
2626

27-
Configurations for driver are all about block device, such as total_size
28-
of block device and read/write operations.
27+
Configurations for driver are all about block device and non-block device,
28+
such as total_size of block device and read/write operations.
2929

3030
Configurations for user
3131
-----------------------
@@ -152,6 +152,15 @@ driver uses ``register_pstore_blk`` to register to pstore/blk.
152152
.. kernel-doc:: fs/pstore/blk.c
153153
:identifiers: register_pstore_blk
154154

155+
A non-block device driver uses ``register_pstore_device`` with
156+
``struct pstore_device_info`` to register to pstore/blk.
157+
158+
.. kernel-doc:: fs/pstore/blk.c
159+
:identifiers: register_pstore_device
160+
161+
.. kernel-doc:: include/linux/pstore_blk.h
162+
:identifiers: pstore_device_info
163+
155164
Compression and header
156165
----------------------
157166

fs/pstore/blk.c

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -105,55 +105,22 @@ struct bdev_info {
105105
_##name_; \
106106
})
107107

108-
/**
109-
* struct pstore_device_info - back-end pstore/blk driver structure.
110-
*
111-
* @total_size: The total size in bytes pstore/blk can use. It must be greater
112-
* than 4096 and be multiple of 4096.
113-
* @flags: Refer to macro starting with PSTORE_FLAGS defined in
114-
* linux/pstore.h. It means what front-ends this device support.
115-
* Zero means all backends for compatible.
116-
* @read: The general read operation. Both of the function parameters
117-
* @size and @offset are relative value to bock device (not the
118-
* whole disk).
119-
* On success, the number of bytes should be returned, others
120-
* means error.
121-
* @write: The same as @read, but the following error number:
122-
* -EBUSY means try to write again later.
123-
* -ENOMSG means to try next zone.
124-
* @panic_write:The write operation only used for panic case. It's optional
125-
* if you do not care panic log. The parameters are relative
126-
* value to storage.
127-
* On success, the number of bytes should be returned, others
128-
* excluding -ENOMSG mean error. -ENOMSG means to try next zone.
129-
*/
130-
struct pstore_device_info {
131-
unsigned long total_size;
132-
unsigned int flags;
133-
pstore_zone_read_op read;
134-
pstore_zone_write_op write;
135-
pstore_zone_write_op panic_write;
136-
};
137-
138-
static int psblk_register_do(struct pstore_device_info *dev)
108+
static int __register_pstore_device(struct pstore_device_info *dev)
139109
{
140110
int ret;
141111

112+
lockdep_assert_held(&pstore_blk_lock);
113+
142114
if (!dev || !dev->total_size || !dev->read || !dev->write)
143115
return -EINVAL;
144116

145-
mutex_lock(&pstore_blk_lock);
146-
147117
/* someone already registered before */
148-
if (pstore_zone_info) {
149-
mutex_unlock(&pstore_blk_lock);
118+
if (pstore_zone_info)
150119
return -EBUSY;
151-
}
120+
152121
pstore_zone_info = kzalloc(sizeof(struct pstore_zone_info), GFP_KERNEL);
153-
if (!pstore_zone_info) {
154-
mutex_unlock(&pstore_blk_lock);
122+
if (!pstore_zone_info)
155123
return -ENOMEM;
156-
}
157124

158125
/* zero means not limit on which backends to attempt to store. */
159126
if (!dev->flags)
@@ -179,6 +146,7 @@ static int psblk_register_do(struct pstore_device_info *dev)
179146
pstore_zone_info->max_reason = max_reason;
180147
pstore_zone_info->read = dev->read;
181148
pstore_zone_info->write = dev->write;
149+
pstore_zone_info->erase = dev->erase;
182150
pstore_zone_info->panic_write = dev->panic_write;
183151
pstore_zone_info->name = KBUILD_MODNAME;
184152
pstore_zone_info->owner = THIS_MODULE;
@@ -188,20 +156,51 @@ static int psblk_register_do(struct pstore_device_info *dev)
188156
kfree(pstore_zone_info);
189157
pstore_zone_info = NULL;
190158
}
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);
191176
mutex_unlock(&pstore_blk_lock);
177+
192178
return ret;
193179
}
180+
EXPORT_SYMBOL_GPL(register_pstore_device);
194181

195-
static void psblk_unregister_do(struct pstore_device_info *dev)
182+
static void __unregister_pstore_device(struct pstore_device_info *dev)
196183
{
197-
mutex_lock(&pstore_blk_lock);
184+
lockdep_assert_held(&pstore_blk_lock);
198185
if (pstore_zone_info && pstore_zone_info->read == dev->read) {
199186
unregister_pstore_zone(pstore_zone_info);
200187
kfree(pstore_zone_info);
201188
pstore_zone_info = NULL;
202189
}
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);
203201
mutex_unlock(&pstore_blk_lock);
204202
}
203+
EXPORT_SYMBOL_GPL(unregister_pstore_device);
205204

206205
/**
207206
* psblk_get_bdev() - open block device
@@ -396,9 +395,10 @@ static int __register_pstore_blk(struct pstore_blk_info *info)
396395
dev.flags = info->flags;
397396
dev.read = psblk_generic_blk_read;
398397
dev.write = psblk_generic_blk_write;
398+
dev.erase = NULL;
399399
dev.panic_write = info->panic_write ? psblk_blk_panic_write : NULL;
400400

401-
ret = psblk_register_do(&dev);
401+
ret = __register_pstore_device(&dev);
402402
if (ret)
403403
goto err_put_bdev;
404404

@@ -442,7 +442,7 @@ static void __unregister_pstore_blk(unsigned int major)
442442

443443
lockdep_assert_held(&pstore_blk_lock);
444444
if (psblk_bdev && MAJOR(psblk_bdev->bd_dev) == major) {
445-
psblk_unregister_do(&dev);
445+
__unregister_pstore_device(&dev);
446446
psblk_put_bdev(psblk_bdev, holder);
447447
blkdev_panic_write = NULL;
448448
psblk_bdev = NULL;
@@ -481,6 +481,13 @@ static void __exit pstore_blk_exit(void)
481481
mutex_lock(&pstore_blk_lock);
482482
if (psblk_bdev)
483483
__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+
}
484491
mutex_unlock(&pstore_blk_lock);
485492
}
486493
module_exit(pstore_blk_exit);

fs/pstore/zone.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,15 +660,21 @@ static inline int psz_kmsg_erase(struct psz_context *cxt,
660660
struct psz_buffer *buffer = zone->buffer;
661661
struct psz_kmsg_header *hdr =
662662
(struct psz_kmsg_header *)buffer->data;
663+
size_t size;
663664

664665
if (unlikely(!psz_ok(zone)))
665666
return 0;
667+
666668
/* this zone is already updated, no need to erase */
667669
if (record->count != hdr->counter)
668670
return 0;
669671

672+
size = buffer_datalen(zone) + sizeof(*zone->buffer);
670673
atomic_set(&zone->buffer->datalen, 0);
671-
return psz_zone_write(zone, FLUSH_META, NULL, 0, 0);
674+
if (cxt->pstore_zone_info->erase)
675+
return cxt->pstore_zone_info->erase(size, zone->off);
676+
else
677+
return psz_zone_write(zone, FLUSH_META, NULL, 0, 0);
672678
}
673679

674680
static inline int psz_record_erase(struct psz_context *cxt,

include/linux/pstore_blk.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,44 @@ struct pstore_blk_info {
4949
int register_pstore_blk(struct pstore_blk_info *info);
5050
void unregister_pstore_blk(unsigned int major);
5151

52+
/**
53+
* struct pstore_device_info - back-end pstore/blk driver structure.
54+
*
55+
* @total_size: The total size in bytes pstore/blk can use. It must be greater
56+
* than 4096 and be multiple of 4096.
57+
* @flags: Refer to macro starting with PSTORE_FLAGS defined in
58+
* linux/pstore.h. It means what front-ends this device support.
59+
* Zero means all backends for compatible.
60+
* @read: The general read operation. Both of the function parameters
61+
* @size and @offset are relative value to bock device (not the
62+
* whole disk).
63+
* On success, the number of bytes should be returned, others
64+
* means error.
65+
* @write: The same as @read, but the following error number:
66+
* -EBUSY means try to write again later.
67+
* -ENOMSG means to try next zone.
68+
* @erase: The general erase operation for device with special removing
69+
* job. Both of the function parameters @size and @offset are
70+
* relative value to storage.
71+
* Return 0 on success and others on failure.
72+
* @panic_write:The write operation only used for panic case. It's optional
73+
* if you do not care panic log. The parameters are relative
74+
* value to storage.
75+
* On success, the number of bytes should be returned, others
76+
* excluding -ENOMSG mean error. -ENOMSG means to try next zone.
77+
*/
78+
struct pstore_device_info {
79+
unsigned long total_size;
80+
unsigned int flags;
81+
pstore_zone_read_op read;
82+
pstore_zone_write_op write;
83+
pstore_zone_erase_op erase;
84+
pstore_zone_write_op panic_write;
85+
};
86+
87+
int register_pstore_device(struct pstore_device_info *dev);
88+
void unregister_pstore_device(struct pstore_device_info *dev);
89+
5290
/**
5391
* struct pstore_blk_config - the pstore_blk backend configuration
5492
*

include/linux/pstore_zone.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
typedef ssize_t (*pstore_zone_read_op)(char *, size_t, loff_t);
99
typedef ssize_t (*pstore_zone_write_op)(const char *, size_t, loff_t);
10+
typedef ssize_t (*pstore_zone_erase_op)(size_t, loff_t);
1011
/**
1112
* struct pstore_zone_info - pstore/zone back-end driver structure
1213
*
@@ -27,6 +28,10 @@ typedef ssize_t (*pstore_zone_write_op)(const char *, size_t, loff_t);
2728
* @write: The same as @read, but the following error number:
2829
* -EBUSY means try to write again later.
2930
* -ENOMSG means to try next zone.
31+
* @erase: The general erase operation for device with special removing
32+
* job. Both of the function parameters @size and @offset are
33+
* relative value to storage.
34+
* Return 0 on success and others on failure.
3035
* @panic_write:The write operation only used for panic case. It's optional
3136
* if you do not care panic log. The parameters are relative
3237
* value to storage.
@@ -45,6 +50,7 @@ struct pstore_zone_info {
4550
unsigned long ftrace_size;
4651
pstore_zone_read_op read;
4752
pstore_zone_write_op write;
53+
pstore_zone_erase_op erase;
4854
pstore_zone_write_op panic_write;
4955
};
5056

0 commit comments

Comments
 (0)