2 * Persistent Storage - platform driver interface parts.
4 * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
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.
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.
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
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>
28 #include <linux/timer.h>
29 #include <linux/slab.h>
30 #include <linux/uaccess.h>
31 #include <linux/workqueue.h>
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
40 #define PSTORE_INTERVAL (60 * HZ)
42 static int pstore_new_entry;
44 static void pstore_timefunc(unsigned long);
45 static DEFINE_TIMER(pstore_timer, pstore_timefunc, 0, 0);
47 static void pstore_dowork(struct work_struct *);
48 static DECLARE_WORK(pstore_work, pstore_dowork);
51 * pstore_lock just protects "psinfo" during
52 * calls to pstore_register()
54 static DEFINE_SPINLOCK(pstore_lock);
55 static struct pstore_info *psinfo;
59 /* How much of the console log to snapshot */
60 static unsigned long kmsg_bytes = 10240;
62 void pstore_set_kmsg_bytes(int bytes)
67 /* Tag each group of saved records with a sequence number */
70 static char *reason_str[] = {
71 "Oops", "Panic", "Kexec", "Restart", "Halt", "Poweroff", "Emergency"
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.
79 static 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)
84 unsigned long s1_start, s2_start;
85 unsigned long l1_cpy, l2_cpy;
86 unsigned long size, total = 0;
90 unsigned int part = 1;
92 if (reason < ARRAY_SIZE(reason_str))
93 why = reason_str[reason];
97 mutex_lock(&psinfo->buf_mutex);
99 while (total < kmsg_bytes) {
101 hsize = sprintf(dst, "%s#%d Part%d\n", why, oopscount, part);
102 size = psinfo->bufsize - hsize;
105 l2_cpy = min(l2, size);
106 l1_cpy = min(l1, size - l2_cpy);
108 if (l1_cpy + l2_cpy == 0)
111 s2_start = l2 - l2_cpy;
112 s1_start = l1 - l1_cpy;
114 memcpy(dst, s1 + s1_start, l1_cpy);
115 memcpy(dst + l1_cpy, s2 + s2_start, l2_cpy);
117 id = psinfo->write(PSTORE_TYPE_DMESG, part,
118 hsize + l1_cpy + l2_cpy, psinfo);
119 if (reason == KMSG_DUMP_OOPS && pstore_is_mounted())
120 pstore_new_entry = 1;
123 total += l1_cpy + l2_cpy;
126 mutex_unlock(&psinfo->buf_mutex);
129 static struct kmsg_dumper pstore_dumper = {
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
140 * Register with kmsg_dump to save last part of console log on panic.
142 int pstore_register(struct pstore_info *psi)
144 struct module *owner = psi->owner;
146 spin_lock(&pstore_lock);
148 spin_unlock(&pstore_lock);
152 if (backend && strcmp(backend, psi->name)) {
153 spin_unlock(&pstore_lock);
158 spin_unlock(&pstore_lock);
160 if (owner && !try_module_get(owner)) {
165 if (pstore_is_mounted())
166 pstore_get_records(0);
168 kmsg_dump_register(&pstore_dumper);
170 pstore_timer.expires = jiffies + PSTORE_INTERVAL;
171 add_timer(&pstore_timer);
175 EXPORT_SYMBOL_GPL(pstore_register);
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
183 void pstore_get_records(int quiet)
185 struct pstore_info *psi = psinfo;
188 enum pstore_type_id type;
189 struct timespec time;
195 mutex_lock(&psinfo->buf_mutex);
200 while ((size = psi->read(&id, &type, &time, psi)) > 0) {
201 rc = pstore_mkfile(type, psi->name, id, psi->buf, (size_t)size,
203 if (rc && (rc != -EEXIST || !quiet))
208 mutex_unlock(&psinfo->buf_mutex);
211 printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n",
215 static void pstore_dowork(struct work_struct *work)
217 pstore_get_records(1);
220 static void pstore_timefunc(unsigned long dummy)
222 if (pstore_new_entry) {
223 pstore_new_entry = 0;
224 schedule_work(&pstore_work);
227 mod_timer(&pstore_timer, jiffies + PSTORE_INTERVAL);
231 * Call platform driver to write a record to the
234 int pstore_write(enum pstore_type_id type, char *buf, size_t size)
241 if (size > psinfo->bufsize)
244 mutex_lock(&psinfo->buf_mutex);
245 memcpy(psinfo->buf, buf, size);
246 id = psinfo->write(type, 0, size, psinfo);
247 if (pstore_is_mounted())
248 pstore_mkfile(PSTORE_TYPE_DMESG, psinfo->name, id, psinfo->buf,
249 size, CURRENT_TIME, psinfo);
250 mutex_unlock(&psinfo->buf_mutex);
254 EXPORT_SYMBOL_GPL(pstore_write);
256 module_param(backend, charp, 0444);
257 MODULE_PARM_DESC(backend, "Pstore backend to use");