]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/pstore/platform.c
pstore: defer inserting OOPS entries into pstore
[mirror_ubuntu-bionic-kernel.git] / fs / pstore / platform.c
CommitLineData
ca01d6dd
TL
1/*
2 * Persistent Storage - platform driver interface parts.
3 *
4 * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/atomic.h>
21#include <linux/types.h>
22#include <linux/errno.h>
23#include <linux/init.h>
24#include <linux/kmsg_dump.h>
25#include <linux/module.h>
26#include <linux/pstore.h>
27#include <linux/string.h>
6dda9266 28#include <linux/timer.h>
ca01d6dd
TL
29#include <linux/slab.h>
30#include <linux/uaccess.h>
6dda9266 31#include <linux/workqueue.h>
ca01d6dd
TL
32
33#include "internal.h"
34
6dda9266
TL
35/*
36 * We defer making "oops" entries appear in pstore - see
37 * whether the system is actually still running well enough
38 * to let someone see the entry
39 */
40#define PSTORE_INTERVAL (60 * HZ)
41
42static int pstore_new_entry;
43
44static void pstore_timefunc(unsigned long);
45static DEFINE_TIMER(pstore_timer, pstore_timefunc, 0, 0);
46
47static void pstore_dowork(struct work_struct *);
48static DECLARE_WORK(pstore_work, pstore_dowork);
49
ca01d6dd
TL
50/*
51 * pstore_lock just protects "psinfo" during
52 * calls to pstore_register()
53 */
54static DEFINE_SPINLOCK(pstore_lock);
55static struct pstore_info *psinfo;
56
dee28e72
MG
57static char *backend;
58
366f7e7a 59/* How much of the console log to snapshot */
ca01d6dd
TL
60static unsigned long kmsg_bytes = 10240;
61
366f7e7a 62void pstore_set_kmsg_bytes(int bytes)
ca01d6dd 63{
366f7e7a 64 kmsg_bytes = bytes;
ca01d6dd
TL
65}
66
ca01d6dd
TL
67/* Tag each group of saved records with a sequence number */
68static int oopscount;
69
9f6af27f
TL
70static char *reason_str[] = {
71 "Oops", "Panic", "Kexec", "Restart", "Halt", "Poweroff", "Emergency"
72};
73
ca01d6dd
TL
74/*
75 * callback from kmsg_dump. (s2,l2) has the most recently
76 * written bytes, older bytes are in (s1,l1). Save as much
77 * as we can from the end of the buffer.
78 */
79static void pstore_dump(struct kmsg_dumper *dumper,
80 enum kmsg_dump_reason reason,
81 const char *s1, unsigned long l1,
82 const char *s2, unsigned long l2)
83{
84 unsigned long s1_start, s2_start;
85 unsigned long l1_cpy, l2_cpy;
86 unsigned long size, total = 0;
9f6af27f 87 char *dst, *why;
ca01d6dd 88 u64 id;
b94fdd07
MG
89 int hsize;
90 unsigned int part = 1;
ca01d6dd 91
9f6af27f
TL
92 if (reason < ARRAY_SIZE(reason_str))
93 why = reason_str[reason];
94 else
95 why = "Unknown";
96
ca01d6dd
TL
97 mutex_lock(&psinfo->buf_mutex);
98 oopscount++;
99 while (total < kmsg_bytes) {
100 dst = psinfo->buf;
56280682 101 hsize = sprintf(dst, "%s#%d Part%d\n", why, oopscount, part);
ca01d6dd
TL
102 size = psinfo->bufsize - hsize;
103 dst += hsize;
104
105 l2_cpy = min(l2, size);
106 l1_cpy = min(l1, size - l2_cpy);
107
108 if (l1_cpy + l2_cpy == 0)
109 break;
110
111 s2_start = l2 - l2_cpy;
112 s1_start = l1 - l1_cpy;
113
114 memcpy(dst, s1 + s1_start, l1_cpy);
115 memcpy(dst + l1_cpy, s2 + s2_start, l2_cpy);
116
56280682
MG
117 id = psinfo->write(PSTORE_TYPE_DMESG, part,
118 hsize + l1_cpy + l2_cpy, psinfo);
9f6af27f 119 if (reason == KMSG_DUMP_OOPS && pstore_is_mounted())
6dda9266 120 pstore_new_entry = 1;
ca01d6dd
TL
121 l1 -= l1_cpy;
122 l2 -= l2_cpy;
123 total += l1_cpy + l2_cpy;
56280682 124 part++;
ca01d6dd
TL
125 }
126 mutex_unlock(&psinfo->buf_mutex);
127}
128
129static struct kmsg_dumper pstore_dumper = {
130 .dump = pstore_dump,
131};
132
133/*
134 * platform specific persistent storage driver registers with
135 * us here. If pstore is already mounted, call the platform
136 * read function right away to populate the file system. If not
137 * then the pstore mount code will call us later to fill out
138 * the file system.
139 *
140 * Register with kmsg_dump to save last part of console log on panic.
141 */
142int pstore_register(struct pstore_info *psi)
143{
144 struct module *owner = psi->owner;
145
146 spin_lock(&pstore_lock);
147 if (psinfo) {
148 spin_unlock(&pstore_lock);
149 return -EBUSY;
150 }
dee28e72
MG
151
152 if (backend && strcmp(backend, psi->name)) {
153 spin_unlock(&pstore_lock);
154 return -EINVAL;
155 }
156
ca01d6dd
TL
157 psinfo = psi;
158 spin_unlock(&pstore_lock);
159
160 if (owner && !try_module_get(owner)) {
161 psinfo = NULL;
162 return -EINVAL;
163 }
164
165 if (pstore_is_mounted())
6dda9266 166 pstore_get_records(0);
ca01d6dd
TL
167
168 kmsg_dump_register(&pstore_dumper);
169
6dda9266
TL
170 pstore_timer.expires = jiffies + PSTORE_INTERVAL;
171 add_timer(&pstore_timer);
172
ca01d6dd
TL
173 return 0;
174}
175EXPORT_SYMBOL_GPL(pstore_register);
176
177/*
6dda9266
TL
178 * Read all the records from the persistent store. Create
179 * files in our filesystem. Don't warn about -EEXIST errors
180 * when we are re-scanning the backing store looking to add new
181 * error records.
ca01d6dd 182 */
6dda9266 183void pstore_get_records(int quiet)
ca01d6dd
TL
184{
185 struct pstore_info *psi = psinfo;
8d38d74b 186 ssize_t size;
ca01d6dd
TL
187 u64 id;
188 enum pstore_type_id type;
189 struct timespec time;
06cf91b4 190 int failed = 0, rc;
ca01d6dd
TL
191
192 if (!psi)
193 return;
194
195 mutex_lock(&psinfo->buf_mutex);
06cf91b4
CG
196 rc = psi->open(psi);
197 if (rc)
198 goto out;
199
638c1fd3 200 while ((size = psi->read(&id, &type, &time, psi)) > 0) {
6dda9266
TL
201 rc = pstore_mkfile(type, psi->name, id, psi->buf, (size_t)size,
202 time, psi);
203 if (rc && (rc != -EEXIST || !quiet))
ca01d6dd
TL
204 failed++;
205 }
06cf91b4
CG
206 psi->close(psi);
207out:
ca01d6dd
TL
208 mutex_unlock(&psinfo->buf_mutex);
209
210 if (failed)
211 printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n",
212 failed, psi->name);
213}
214
6dda9266
TL
215static void pstore_dowork(struct work_struct *work)
216{
217 pstore_get_records(1);
218}
219
220static void pstore_timefunc(unsigned long dummy)
221{
222 if (pstore_new_entry) {
223 pstore_new_entry = 0;
224 schedule_work(&pstore_work);
225 }
226
227 mod_timer(&pstore_timer, jiffies + PSTORE_INTERVAL);
228}
229
ca01d6dd
TL
230/*
231 * Call platform driver to write a record to the
232 * persistent store.
233 */
234int pstore_write(enum pstore_type_id type, char *buf, size_t size)
235{
236 u64 id;
237
238 if (!psinfo)
239 return -ENODEV;
240
241 if (size > psinfo->bufsize)
242 return -EFBIG;
243
244 mutex_lock(&psinfo->buf_mutex);
245 memcpy(psinfo->buf, buf, size);
56280682 246 id = psinfo->write(type, 0, size, psinfo);
ca01d6dd
TL
247 if (pstore_is_mounted())
248 pstore_mkfile(PSTORE_TYPE_DMESG, psinfo->name, id, psinfo->buf,
638c1fd3 249 size, CURRENT_TIME, psinfo);
ca01d6dd
TL
250 mutex_unlock(&psinfo->buf_mutex);
251
252 return 0;
253}
254EXPORT_SYMBOL_GPL(pstore_write);
dee28e72
MG
255
256module_param(backend, charp, 0444);
257MODULE_PARM_DESC(backend, "Pstore backend to use");