diff options
author | Tony Luck <tony.luck@intel.com> | 2010-12-28 17:25:21 -0500 |
---|---|---|
committer | Tony Luck <tony.luck@intel.com> | 2010-12-28 17:25:21 -0500 |
commit | ca01d6dd2d7a2652000307520777538740efc286 (patch) | |
tree | f419325637badb0ef7a3bfa29d520be2f5282220 /fs/pstore | |
parent | 90a8a73c06cc32b609a880d48449d7083327e11a (diff) |
pstore: new filesystem interface to platform persistent storage
Some platforms have a small amount of non-volatile storage that
can be used to store information useful to diagnose the cause of
a system crash. This is the generic part of a file system interface
that presents information from the crash as a series of files in
/dev/pstore. Once the information has been seen, the underlying
storage is freed by deleting the files.
Signed-off-by: Tony Luck <tony.luck@intel.com>
Diffstat (limited to 'fs/pstore')
-rw-r--r-- | fs/pstore/Kconfig | 13 | ||||
-rw-r--r-- | fs/pstore/Makefile | 7 | ||||
-rw-r--r-- | fs/pstore/inode.c | 280 | ||||
-rw-r--r-- | fs/pstore/internal.h | 7 | ||||
-rw-r--r-- | fs/pstore/platform.c | 202 |
5 files changed, 509 insertions, 0 deletions
diff --git a/fs/pstore/Kconfig b/fs/pstore/Kconfig new file mode 100644 index 000000000000..867d0ac026ce --- /dev/null +++ b/fs/pstore/Kconfig | |||
@@ -0,0 +1,13 @@ | |||
1 | config PSTORE | ||
2 | bool "Persistant store support" | ||
3 | default n | ||
4 | help | ||
5 | This option enables generic access to platform level | ||
6 | persistent storage via "pstore" filesystem that can | ||
7 | be mounted as /dev/pstore. Only useful if you have | ||
8 | a platform level driver that registers with pstore to | ||
9 | provide the data, so you probably should just go say "Y" | ||
10 | (or "M") to a platform specific persistent store driver | ||
11 | (e.g. ACPI_APEI on X86) which will select this for you. | ||
12 | If you don't have a platform persistent store driver, | ||
13 | say N. | ||
diff --git a/fs/pstore/Makefile b/fs/pstore/Makefile new file mode 100644 index 000000000000..760f4bce7d1d --- /dev/null +++ b/fs/pstore/Makefile | |||
@@ -0,0 +1,7 @@ | |||
1 | # | ||
2 | # Makefile for the linux pstorefs routines. | ||
3 | # | ||
4 | |||
5 | obj-y += pstore.o | ||
6 | |||
7 | pstore-objs += inode.o platform.o | ||
diff --git a/fs/pstore/inode.c b/fs/pstore/inode.c new file mode 100644 index 000000000000..0e806aafe857 --- /dev/null +++ b/fs/pstore/inode.c | |||
@@ -0,0 +1,280 @@ | |||
1 | /* | ||
2 | * Persistent Storage - ramfs 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/module.h> | ||
21 | #include <linux/fs.h> | ||
22 | #include <linux/fsnotify.h> | ||
23 | #include <linux/pagemap.h> | ||
24 | #include <linux/highmem.h> | ||
25 | #include <linux/time.h> | ||
26 | #include <linux/init.h> | ||
27 | #include <linux/string.h> | ||
28 | #include <linux/mount.h> | ||
29 | #include <linux/ramfs.h> | ||
30 | #include <linux/sched.h> | ||
31 | #include <linux/magic.h> | ||
32 | #include <linux/pstore.h> | ||
33 | #include <linux/slab.h> | ||
34 | #include <linux/uaccess.h> | ||
35 | |||
36 | #include "internal.h" | ||
37 | |||
38 | #define PSTORE_NAMELEN 64 | ||
39 | |||
40 | struct pstore_private { | ||
41 | u64 id; | ||
42 | int (*erase)(u64); | ||
43 | }; | ||
44 | |||
45 | #define pstore_get_inode ramfs_get_inode | ||
46 | |||
47 | /* | ||
48 | * When a file is unlinked from our file system we call the | ||
49 | * platform driver to erase the record from persistent store. | ||
50 | */ | ||
51 | static int pstore_unlink(struct inode *dir, struct dentry *dentry) | ||
52 | { | ||
53 | struct pstore_private *p = dentry->d_inode->i_private; | ||
54 | |||
55 | p->erase(p->id); | ||
56 | kfree(p); | ||
57 | |||
58 | return simple_unlink(dir, dentry); | ||
59 | } | ||
60 | |||
61 | static const struct inode_operations pstore_dir_inode_operations = { | ||
62 | .lookup = simple_lookup, | ||
63 | .unlink = pstore_unlink, | ||
64 | }; | ||
65 | |||
66 | static const struct super_operations pstore_ops = { | ||
67 | .statfs = simple_statfs, | ||
68 | .drop_inode = generic_delete_inode, | ||
69 | .show_options = generic_show_options, | ||
70 | }; | ||
71 | |||
72 | static struct super_block *pstore_sb; | ||
73 | static struct vfsmount *pstore_mnt; | ||
74 | |||
75 | int pstore_is_mounted(void) | ||
76 | { | ||
77 | return pstore_mnt != NULL; | ||
78 | } | ||
79 | |||
80 | /* | ||
81 | * Set up a file structure as if we had opened this file and | ||
82 | * write our data to it. | ||
83 | */ | ||
84 | static int pstore_writefile(struct inode *inode, struct dentry *dentry, | ||
85 | char *data, size_t size) | ||
86 | { | ||
87 | struct file f; | ||
88 | ssize_t n; | ||
89 | mm_segment_t old_fs = get_fs(); | ||
90 | |||
91 | memset(&f, '0', sizeof f); | ||
92 | f.f_mapping = inode->i_mapping; | ||
93 | f.f_path.dentry = dentry; | ||
94 | f.f_path.mnt = pstore_mnt; | ||
95 | f.f_pos = 0; | ||
96 | f.f_op = inode->i_fop; | ||
97 | set_fs(KERNEL_DS); | ||
98 | n = do_sync_write(&f, data, size, &f.f_pos); | ||
99 | set_fs(old_fs); | ||
100 | |||
101 | fsnotify_modify(&f); | ||
102 | |||
103 | return n == size; | ||
104 | } | ||
105 | |||
106 | /* | ||
107 | * Make a regular file in the root directory of our file system. | ||
108 | * Load it up with "size" bytes of data from "buf". | ||
109 | * Set the mtime & ctime to the date that this record was originally stored. | ||
110 | */ | ||
111 | int pstore_mkfile(enum pstore_type_id type, char *psname, u64 id, | ||
112 | char *data, size_t size, | ||
113 | struct timespec time, int (*erase)(u64)) | ||
114 | { | ||
115 | struct dentry *root = pstore_sb->s_root; | ||
116 | struct dentry *dentry; | ||
117 | struct inode *inode; | ||
118 | int rc; | ||
119 | char name[PSTORE_NAMELEN]; | ||
120 | struct pstore_private *private; | ||
121 | |||
122 | rc = -ENOMEM; | ||
123 | inode = pstore_get_inode(pstore_sb, root->d_inode, S_IFREG | 0444, 0); | ||
124 | if (!inode) | ||
125 | goto fail; | ||
126 | inode->i_uid = inode->i_gid = 0; | ||
127 | private = kmalloc(sizeof *private, GFP_KERNEL); | ||
128 | if (!private) | ||
129 | goto fail_alloc; | ||
130 | private->id = id; | ||
131 | private->erase = erase; | ||
132 | |||
133 | switch (type) { | ||
134 | case PSTORE_TYPE_DMESG: | ||
135 | sprintf(name, "dmesg-%s-%lld", psname, id); | ||
136 | break; | ||
137 | case PSTORE_TYPE_MCE: | ||
138 | sprintf(name, "mce-%s-%lld", psname, id); | ||
139 | break; | ||
140 | case PSTORE_TYPE_UNKNOWN: | ||
141 | sprintf(name, "unknown-%s-%lld", psname, id); | ||
142 | break; | ||
143 | default: | ||
144 | sprintf(name, "type%d-%s-%lld", type, psname, id); | ||
145 | break; | ||
146 | } | ||
147 | |||
148 | mutex_lock(&root->d_inode->i_mutex); | ||
149 | |||
150 | rc = -ENOSPC; | ||
151 | dentry = d_alloc_name(root, name); | ||
152 | if (IS_ERR(dentry)) | ||
153 | goto fail_lockedalloc; | ||
154 | |||
155 | d_add(dentry, inode); | ||
156 | |||
157 | mutex_unlock(&root->d_inode->i_mutex); | ||
158 | |||
159 | if (!pstore_writefile(inode, dentry, data, size)) | ||
160 | goto fail_write; | ||
161 | |||
162 | inode->i_private = private; | ||
163 | |||
164 | if (time.tv_sec) | ||
165 | inode->i_mtime = inode->i_ctime = time; | ||
166 | |||
167 | return 0; | ||
168 | |||
169 | fail_write: | ||
170 | kfree(private); | ||
171 | inode->i_nlink--; | ||
172 | mutex_lock(&root->d_inode->i_mutex); | ||
173 | d_delete(dentry); | ||
174 | dput(dentry); | ||
175 | mutex_unlock(&root->d_inode->i_mutex); | ||
176 | goto fail; | ||
177 | |||
178 | fail_lockedalloc: | ||
179 | mutex_unlock(&root->d_inode->i_mutex); | ||
180 | kfree(private); | ||
181 | fail_alloc: | ||
182 | iput(inode); | ||
183 | |||
184 | fail: | ||
185 | return rc; | ||
186 | } | ||
187 | |||
188 | int pstore_fill_super(struct super_block *sb, void *data, int silent) | ||
189 | { | ||
190 | struct inode *inode = NULL; | ||
191 | struct dentry *root; | ||
192 | int err; | ||
193 | |||
194 | save_mount_options(sb, data); | ||
195 | |||
196 | pstore_sb = sb; | ||
197 | |||
198 | sb->s_maxbytes = MAX_LFS_FILESIZE; | ||
199 | sb->s_blocksize = PAGE_CACHE_SIZE; | ||
200 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; | ||
201 | sb->s_magic = PSTOREFS_MAGIC; | ||
202 | sb->s_op = &pstore_ops; | ||
203 | sb->s_time_gran = 1; | ||
204 | |||
205 | inode = pstore_get_inode(sb, NULL, S_IFDIR | 0755, 0); | ||
206 | if (!inode) { | ||
207 | err = -ENOMEM; | ||
208 | goto fail; | ||
209 | } | ||
210 | /* override ramfs "dir" options so we catch unlink(2) */ | ||
211 | inode->i_op = &pstore_dir_inode_operations; | ||
212 | |||
213 | root = d_alloc_root(inode); | ||
214 | sb->s_root = root; | ||
215 | if (!root) { | ||
216 | err = -ENOMEM; | ||
217 | goto fail; | ||
218 | } | ||
219 | |||
220 | pstore_get_records(); | ||
221 | |||
222 | return 0; | ||
223 | fail: | ||
224 | iput(inode); | ||
225 | return err; | ||
226 | } | ||
227 | |||
228 | static int pstore_get_sb(struct file_system_type *fs_type, | ||
229 | int flags, const char *dev_name, void *data, struct vfsmount *mnt) | ||
230 | { | ||
231 | struct dentry *root; | ||
232 | |||
233 | root = mount_nodev(fs_type, flags, data, pstore_fill_super); | ||
234 | if (IS_ERR(root)) | ||
235 | return -ENOMEM; | ||
236 | |||
237 | mnt->mnt_root = root; | ||
238 | mnt->mnt_sb = root->d_sb; | ||
239 | pstore_mnt = mnt; | ||
240 | |||
241 | return 0; | ||
242 | } | ||
243 | |||
244 | static void pstore_kill_sb(struct super_block *sb) | ||
245 | { | ||
246 | kill_litter_super(sb); | ||
247 | pstore_sb = NULL; | ||
248 | pstore_mnt = NULL; | ||
249 | } | ||
250 | |||
251 | static struct file_system_type pstore_fs_type = { | ||
252 | .name = "pstore", | ||
253 | .get_sb = pstore_get_sb, | ||
254 | .kill_sb = pstore_kill_sb, | ||
255 | }; | ||
256 | |||
257 | static int __init init_pstore_fs(void) | ||
258 | { | ||
259 | int ret = 0; | ||
260 | struct kobject *pstorefs_kobj; | ||
261 | |||
262 | pstorefs_kobj = kobject_create_and_add("pstore", fs_kobj); | ||
263 | if (!pstorefs_kobj) | ||
264 | return -ENOMEM; | ||
265 | |||
266 | sysfs_create_file(pstorefs_kobj, &pstore_kmsg_bytes_attr.attr); | ||
267 | |||
268 | ret = register_filesystem(&pstore_fs_type); | ||
269 | |||
270 | if (ret) { | ||
271 | sysfs_remove_file(pstorefs_kobj, &pstore_kmsg_bytes_attr.attr); | ||
272 | kobject_put(pstorefs_kobj); | ||
273 | } | ||
274 | |||
275 | return ret; | ||
276 | } | ||
277 | module_init(init_pstore_fs) | ||
278 | |||
279 | MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>"); | ||
280 | MODULE_LICENSE("GPL"); | ||
diff --git a/fs/pstore/internal.h b/fs/pstore/internal.h new file mode 100644 index 000000000000..76c26d2fab29 --- /dev/null +++ b/fs/pstore/internal.h | |||
@@ -0,0 +1,7 @@ | |||
1 | extern void pstore_get_records(void); | ||
2 | extern int pstore_mkfile(enum pstore_type_id, char *psname, u64 id, | ||
3 | char *data, size_t size, | ||
4 | struct timespec time, int (*erase)(u64)); | ||
5 | extern int pstore_is_mounted(void); | ||
6 | |||
7 | extern struct kobj_attribute pstore_kmsg_bytes_attr; | ||
diff --git a/fs/pstore/platform.c b/fs/pstore/platform.c new file mode 100644 index 000000000000..705fdf8abf6e --- /dev/null +++ b/fs/pstore/platform.c | |||
@@ -0,0 +1,202 @@ | |||
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> | ||
28 | #include <linux/slab.h> | ||
29 | #include <linux/uaccess.h> | ||
30 | |||
31 | #include "internal.h" | ||
32 | |||
33 | /* | ||
34 | * pstore_lock just protects "psinfo" during | ||
35 | * calls to pstore_register() | ||
36 | */ | ||
37 | static DEFINE_SPINLOCK(pstore_lock); | ||
38 | static struct pstore_info *psinfo; | ||
39 | |||
40 | /* How much of the console log to snapshot. /sys/fs/pstore/kmsg_bytes */ | ||
41 | static unsigned long kmsg_bytes = 10240; | ||
42 | |||
43 | static ssize_t b_show(struct kobject *kobj, | ||
44 | struct kobj_attribute *attr, char *buf) | ||
45 | { | ||
46 | return snprintf(buf, PAGE_SIZE, "%lu\n", kmsg_bytes); | ||
47 | } | ||
48 | |||
49 | static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr, | ||
50 | const char *buf, size_t count) | ||
51 | { | ||
52 | return (sscanf(buf, "%lu", &kmsg_bytes) > 0) ? count : 0; | ||
53 | } | ||
54 | |||
55 | struct kobj_attribute pstore_kmsg_bytes_attr = | ||
56 | __ATTR(kmsg_bytes, S_IRUGO | S_IWUSR, b_show, b_store); | ||
57 | |||
58 | /* Tag each group of saved records with a sequence number */ | ||
59 | static int oopscount; | ||
60 | |||
61 | /* | ||
62 | * callback from kmsg_dump. (s2,l2) has the most recently | ||
63 | * written bytes, older bytes are in (s1,l1). Save as much | ||
64 | * as we can from the end of the buffer. | ||
65 | */ | ||
66 | static void pstore_dump(struct kmsg_dumper *dumper, | ||
67 | enum kmsg_dump_reason reason, | ||
68 | const char *s1, unsigned long l1, | ||
69 | const char *s2, unsigned long l2) | ||
70 | { | ||
71 | unsigned long s1_start, s2_start; | ||
72 | unsigned long l1_cpy, l2_cpy; | ||
73 | unsigned long size, total = 0; | ||
74 | char *dst; | ||
75 | u64 id; | ||
76 | int hsize, part = 1; | ||
77 | |||
78 | mutex_lock(&psinfo->buf_mutex); | ||
79 | oopscount++; | ||
80 | while (total < kmsg_bytes) { | ||
81 | dst = psinfo->buf; | ||
82 | hsize = sprintf(dst, "Oops#%d Part%d\n", oopscount, part++); | ||
83 | size = psinfo->bufsize - hsize; | ||
84 | dst += hsize; | ||
85 | |||
86 | l2_cpy = min(l2, size); | ||
87 | l1_cpy = min(l1, size - l2_cpy); | ||
88 | |||
89 | if (l1_cpy + l2_cpy == 0) | ||
90 | break; | ||
91 | |||
92 | s2_start = l2 - l2_cpy; | ||
93 | s1_start = l1 - l1_cpy; | ||
94 | |||
95 | memcpy(dst, s1 + s1_start, l1_cpy); | ||
96 | memcpy(dst + l1_cpy, s2 + s2_start, l2_cpy); | ||
97 | |||
98 | id = psinfo->write(PSTORE_TYPE_DMESG, hsize + l1_cpy + l2_cpy); | ||
99 | if (pstore_is_mounted()) | ||
100 | pstore_mkfile(PSTORE_TYPE_DMESG, psinfo->name, id, | ||
101 | psinfo->buf, hsize + l1_cpy + l2_cpy, | ||
102 | CURRENT_TIME, psinfo->erase); | ||
103 | l1 -= l1_cpy; | ||
104 | l2 -= l2_cpy; | ||
105 | total += l1_cpy + l2_cpy; | ||
106 | } | ||
107 | mutex_unlock(&psinfo->buf_mutex); | ||
108 | } | ||
109 | |||
110 | static struct kmsg_dumper pstore_dumper = { | ||
111 | .dump = pstore_dump, | ||
112 | }; | ||
113 | |||
114 | /* | ||
115 | * platform specific persistent storage driver registers with | ||
116 | * us here. If pstore is already mounted, call the platform | ||
117 | * read function right away to populate the file system. If not | ||
118 | * then the pstore mount code will call us later to fill out | ||
119 | * the file system. | ||
120 | * | ||
121 | * Register with kmsg_dump to save last part of console log on panic. | ||
122 | */ | ||
123 | int pstore_register(struct pstore_info *psi) | ||
124 | { | ||
125 | struct module *owner = psi->owner; | ||
126 | |||
127 | spin_lock(&pstore_lock); | ||
128 | if (psinfo) { | ||
129 | spin_unlock(&pstore_lock); | ||
130 | return -EBUSY; | ||
131 | } | ||
132 | psinfo = psi; | ||
133 | spin_unlock(&pstore_lock); | ||
134 | |||
135 | if (owner && !try_module_get(owner)) { | ||
136 | psinfo = NULL; | ||
137 | return -EINVAL; | ||
138 | } | ||
139 | |||
140 | if (pstore_is_mounted()) | ||
141 | pstore_get_records(); | ||
142 | |||
143 | kmsg_dump_register(&pstore_dumper); | ||
144 | |||
145 | return 0; | ||
146 | } | ||
147 | EXPORT_SYMBOL_GPL(pstore_register); | ||
148 | |||
149 | /* | ||
150 | * Read all the records from the persistent store. Create and | ||
151 | * file files in our filesystem. | ||
152 | */ | ||
153 | void pstore_get_records(void) | ||
154 | { | ||
155 | struct pstore_info *psi = psinfo; | ||
156 | size_t size; | ||
157 | u64 id; | ||
158 | enum pstore_type_id type; | ||
159 | struct timespec time; | ||
160 | int failed = 0; | ||
161 | |||
162 | if (!psi) | ||
163 | return; | ||
164 | |||
165 | mutex_lock(&psinfo->buf_mutex); | ||
166 | while ((size = psi->read(&id, &type, &time)) > 0) { | ||
167 | if (pstore_mkfile(type, psi->name, id, psi->buf, size, | ||
168 | time, psi->erase)) | ||
169 | failed++; | ||
170 | } | ||
171 | mutex_unlock(&psinfo->buf_mutex); | ||
172 | |||
173 | if (failed) | ||
174 | printk(KERN_WARNING "pstore: failed to load %d record(s) from '%s'\n", | ||
175 | failed, psi->name); | ||
176 | } | ||
177 | |||
178 | /* | ||
179 | * Call platform driver to write a record to the | ||
180 | * persistent store. | ||
181 | */ | ||
182 | int pstore_write(enum pstore_type_id type, char *buf, size_t size) | ||
183 | { | ||
184 | u64 id; | ||
185 | |||
186 | if (!psinfo) | ||
187 | return -ENODEV; | ||
188 | |||
189 | if (size > psinfo->bufsize) | ||
190 | return -EFBIG; | ||
191 | |||
192 | mutex_lock(&psinfo->buf_mutex); | ||
193 | memcpy(psinfo->buf, buf, size); | ||
194 | id = psinfo->write(type, size); | ||
195 | if (pstore_is_mounted()) | ||
196 | pstore_mkfile(PSTORE_TYPE_DMESG, psinfo->name, id, psinfo->buf, | ||
197 | size, CURRENT_TIME, psinfo->erase); | ||
198 | mutex_unlock(&psinfo->buf_mutex); | ||
199 | |||
200 | return 0; | ||
201 | } | ||
202 | EXPORT_SYMBOL_GPL(pstore_write); | ||