1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Persistent Storage - pstore.h
7 * This code is the generic layer to export data records from platform
8 * level persistent storage via a file system.
10 #ifndef _LINUX_PSTORE_H
11 #define _LINUX_PSTORE_H
13 #include <linux/compiler.h>
14 #include <linux/errno.h>
15 #include <linux/kmsg_dump.h>
16 #include <linux/mutex.h>
17 #include <linux/semaphore.h>
18 #include <linux/time.h>
19 #include <linux/types.h>
24 * pstore record types (see fs/pstore/platform.c for pstore_type_names[])
25 * These values may be written to storage (see EFI vars backend), so
26 * they are kind of an ABI. Be careful changing the mappings.
29 /* Frontend storage types */
30 PSTORE_TYPE_DMESG = 0,
32 PSTORE_TYPE_CONSOLE = 2,
33 PSTORE_TYPE_FTRACE = 3,
35 /* PPC64-specific partition types */
36 PSTORE_TYPE_PPC_RTAS = 4,
37 PSTORE_TYPE_PPC_OF = 5,
38 PSTORE_TYPE_PPC_COMMON = 6,
40 PSTORE_TYPE_PPC_OPAL = 8,
46 const char *pstore_type_to_name(enum pstore_type_id type);
47 enum pstore_type_id pstore_name_to_type(const char *name);
51 * struct pstore_record - details of a pstore record entry
52 * @psi: pstore backend driver information
53 * @type: pstore record type
54 * @id: per-type unique identifier for record
55 * @time: timestamp of the record
56 * @buf: pointer to record contents
59 * ECC information for @buf
61 * Valid for PSTORE_TYPE_DMESG @type:
63 * @count: Oops count since boot
64 * @reason: kdump reason for notification
65 * @part: position in a multipart record
66 * @compressed: whether the buffer is compressed
69 struct pstore_record {
70 struct pstore_info *psi;
71 enum pstore_type_id type;
73 struct timespec64 time;
76 ssize_t ecc_notice_size;
79 enum kmsg_dump_reason reason;
85 * struct pstore_info - backend pstore driver structure
87 * @owner: module which is responsible for this backend driver
88 * @name: name of the backend driver
90 * @buf_lock: semaphore to serialize access to @buf
91 * @buf: preallocated crash dump buffer
92 * @bufsize: size of @buf available for crash dump bytes (must match
93 * smallest number of bytes available for writing to a
94 * backend entry, since compressed bytes don't take kindly
97 * @read_mutex: serializes @open, @read, @close, and @erase callbacks
98 * @flags: bitfield of frontends the backend can accept writes for
99 * @data: backend-private pointer passed back during callbacks
104 * Notify backend that pstore is starting a full read of backend
105 * records. Followed by one or more @read calls, and a final @close.
107 * @psi: in: pointer to the struct pstore_info for the backend
109 * Returns 0 on success, and non-zero on error.
112 * Notify backend that pstore has finished a full read of backend
113 * records. Always preceded by an @open call and one or more @read
116 * @psi: in: pointer to the struct pstore_info for the backend
118 * Returns 0 on success, and non-zero on error. (Though pstore will
122 * Read next available backend record. Called after a successful
126 * pointer to record to populate. @buf should be allocated
127 * by the backend and filled. At least @type and @id should
128 * be populated, since these are used when creating pstorefs
131 * Returns record size on success, zero when no more records are
132 * available, or negative on error.
135 * A newly generated record needs to be written to backend storage.
138 * pointer to record metadata. When @type is PSTORE_TYPE_DMESG,
139 * @buf will be pointing to the preallocated @psi.buf, since
140 * memory allocation may be broken during an Oops. Regardless,
141 * @buf must be proccesed or copied before returning. The
142 * backend is also expected to write @id with something that
143 * can help identify this record to a future @erase callback.
144 * The @time field will be prepopulated with the current time,
145 * when available. The @size field will have the size of data
148 * Returns 0 on success, and non-zero on error.
151 * Perform a frontend write to a backend record, using a specified
152 * buffer that is coming directly from userspace, instead of the
155 * @record: pointer to record metadata.
156 * @buf: pointer to userspace contents to write to backend
158 * Returns 0 on success, and non-zero on error.
161 * Delete a record from backend storage. Different backends
162 * identify records differently, so entire original record is
163 * passed back to assist in identification of what the backend
164 * should remove from storage.
166 * @record: pointer to record metadata.
168 * Returns 0 on success, and non-zero on error.
172 struct module *owner;
175 struct semaphore buf_lock;
179 struct mutex read_mutex;
184 int (*open)(struct pstore_info *psi);
185 int (*close)(struct pstore_info *psi);
186 ssize_t (*read)(struct pstore_record *record);
187 int (*write)(struct pstore_record *record);
188 int (*write_user)(struct pstore_record *record,
189 const char __user *buf);
190 int (*erase)(struct pstore_record *record);
193 /* Supported frontends */
194 #define PSTORE_FLAGS_DMESG BIT(0)
195 #define PSTORE_FLAGS_CONSOLE BIT(1)
196 #define PSTORE_FLAGS_FTRACE BIT(2)
197 #define PSTORE_FLAGS_PMSG BIT(3)
199 extern int pstore_register(struct pstore_info *);
200 extern void pstore_unregister(struct pstore_info *);
202 struct pstore_ftrace_record {
204 unsigned long parent_ip;
209 * ftrace related stuff: Both backends and frontends need these so expose
213 #if NR_CPUS <= 2 && defined(CONFIG_ARM_THUMB)
214 #define PSTORE_CPU_IN_IP 0x1
215 #elif NR_CPUS <= 4 && defined(CONFIG_ARM)
216 #define PSTORE_CPU_IN_IP 0x3
219 #define TS_CPU_SHIFT 8
220 #define TS_CPU_MASK (BIT(TS_CPU_SHIFT) - 1)
223 * If CPU number can be stored in IP, store it there, otherwise store it in
224 * the time stamp. This means more timestamp resolution is available when
225 * the CPU can be stored in the IP.
227 #ifdef PSTORE_CPU_IN_IP
229 pstore_ftrace_encode_cpu(struct pstore_ftrace_record *rec, unsigned int cpu)
234 static inline unsigned int
235 pstore_ftrace_decode_cpu(struct pstore_ftrace_record *rec)
237 return rec->ip & PSTORE_CPU_IN_IP;
241 pstore_ftrace_read_timestamp(struct pstore_ftrace_record *rec)
247 pstore_ftrace_write_timestamp(struct pstore_ftrace_record *rec, u64 val)
253 pstore_ftrace_encode_cpu(struct pstore_ftrace_record *rec, unsigned int cpu)
255 rec->ts &= ~(TS_CPU_MASK);
259 static inline unsigned int
260 pstore_ftrace_decode_cpu(struct pstore_ftrace_record *rec)
262 return rec->ts & TS_CPU_MASK;
266 pstore_ftrace_read_timestamp(struct pstore_ftrace_record *rec)
268 return rec->ts >> TS_CPU_SHIFT;
272 pstore_ftrace_write_timestamp(struct pstore_ftrace_record *rec, u64 val)
274 rec->ts = (rec->ts & TS_CPU_MASK) | (val << TS_CPU_SHIFT);
278 #endif /*_LINUX_PSTORE_H*/