blob: 377485668e0f9e4f76ad0958e2f961601c98dc18
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/list.h> |
28 | #include <linux/string.h> |
29 | #include <linux/mount.h> |
30 | #include <linux/seq_file.h> |
31 | #include <linux/ramfs.h> |
32 | #include <linux/parser.h> |
33 | #include <linux/sched.h> |
34 | #include <linux/magic.h> |
35 | #include <linux/pstore.h> |
36 | #include <linux/slab.h> |
37 | #include <linux/spinlock.h> |
38 | #include <linux/uaccess.h> |
39 | #include <linux/syslog.h> |
40 | |
41 | #include "internal.h" |
42 | |
43 | #define PSTORE_NAMELEN 64 |
44 | |
45 | static DEFINE_SPINLOCK(allpstore_lock); |
46 | static LIST_HEAD(allpstore); |
47 | |
48 | struct pstore_private { |
49 | struct list_head list; |
50 | struct pstore_info *psi; |
51 | enum pstore_type_id type; |
52 | u64 id; |
53 | int count; |
54 | ssize_t size; |
55 | char data[]; |
56 | }; |
57 | |
58 | struct pstore_ftrace_seq_data { |
59 | const void *ptr; |
60 | size_t off; |
61 | size_t size; |
62 | }; |
63 | |
64 | #define REC_SIZE sizeof(struct pstore_ftrace_record) |
65 | |
66 | static void *pstore_ftrace_seq_start(struct seq_file *s, loff_t *pos) |
67 | { |
68 | struct pstore_private *ps = s->private; |
69 | struct pstore_ftrace_seq_data *data; |
70 | |
71 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
72 | if (!data) |
73 | return NULL; |
74 | |
75 | data->off = ps->size % REC_SIZE; |
76 | data->off += *pos * REC_SIZE; |
77 | if (data->off + REC_SIZE > ps->size) { |
78 | kfree(data); |
79 | return NULL; |
80 | } |
81 | |
82 | return data; |
83 | |
84 | } |
85 | |
86 | static void pstore_ftrace_seq_stop(struct seq_file *s, void *v) |
87 | { |
88 | kfree(v); |
89 | } |
90 | |
91 | static void *pstore_ftrace_seq_next(struct seq_file *s, void *v, loff_t *pos) |
92 | { |
93 | struct pstore_private *ps = s->private; |
94 | struct pstore_ftrace_seq_data *data = v; |
95 | |
96 | data->off += REC_SIZE; |
97 | if (data->off + REC_SIZE > ps->size) |
98 | return NULL; |
99 | |
100 | (*pos)++; |
101 | return data; |
102 | } |
103 | |
104 | static int pstore_ftrace_seq_show(struct seq_file *s, void *v) |
105 | { |
106 | struct pstore_private *ps = s->private; |
107 | struct pstore_ftrace_seq_data *data = v; |
108 | struct pstore_ftrace_record *rec = (void *)(ps->data + data->off); |
109 | |
110 | seq_printf(s, "%d %08lx %08lx %pf <- %pF\n", |
111 | pstore_ftrace_decode_cpu(rec), rec->ip, rec->parent_ip, |
112 | (void *)rec->ip, (void *)rec->parent_ip); |
113 | |
114 | return 0; |
115 | } |
116 | |
117 | static const struct seq_operations pstore_ftrace_seq_ops = { |
118 | .start = pstore_ftrace_seq_start, |
119 | .next = pstore_ftrace_seq_next, |
120 | .stop = pstore_ftrace_seq_stop, |
121 | .show = pstore_ftrace_seq_show, |
122 | }; |
123 | |
124 | static ssize_t pstore_file_read(struct file *file, char __user *userbuf, |
125 | size_t count, loff_t *ppos) |
126 | { |
127 | struct seq_file *sf = file->private_data; |
128 | struct pstore_private *ps = sf->private; |
129 | |
130 | if (ps->type == PSTORE_TYPE_FTRACE) |
131 | return seq_read(file, userbuf, count, ppos); |
132 | return simple_read_from_buffer(userbuf, count, ppos, ps->data, ps->size); |
133 | } |
134 | |
135 | static int pstore_file_open(struct inode *inode, struct file *file) |
136 | { |
137 | struct pstore_private *ps = inode->i_private; |
138 | struct seq_file *sf; |
139 | int err; |
140 | const struct seq_operations *sops = NULL; |
141 | |
142 | if (ps->type == PSTORE_TYPE_FTRACE) |
143 | sops = &pstore_ftrace_seq_ops; |
144 | |
145 | err = seq_open(file, sops); |
146 | if (err < 0) |
147 | return err; |
148 | |
149 | sf = file->private_data; |
150 | sf->private = ps; |
151 | |
152 | return 0; |
153 | } |
154 | |
155 | static loff_t pstore_file_llseek(struct file *file, loff_t off, int whence) |
156 | { |
157 | struct seq_file *sf = file->private_data; |
158 | |
159 | if (sf->op) |
160 | return seq_lseek(file, off, whence); |
161 | return default_llseek(file, off, whence); |
162 | } |
163 | |
164 | static const struct file_operations pstore_file_operations = { |
165 | .open = pstore_file_open, |
166 | .read = pstore_file_read, |
167 | .llseek = pstore_file_llseek, |
168 | .release = seq_release, |
169 | }; |
170 | |
171 | /* |
172 | * When a file is unlinked from our file system we call the |
173 | * platform driver to erase the record from persistent store. |
174 | */ |
175 | static int pstore_unlink(struct inode *dir, struct dentry *dentry) |
176 | { |
177 | struct pstore_private *p = d_inode(dentry)->i_private; |
178 | |
179 | if (p->psi->erase) |
180 | p->psi->erase(p->type, p->id, p->count, |
181 | d_inode(dentry)->i_ctime, p->psi); |
182 | else |
183 | return -EPERM; |
184 | |
185 | return simple_unlink(dir, dentry); |
186 | } |
187 | |
188 | static void pstore_evict_inode(struct inode *inode) |
189 | { |
190 | struct pstore_private *p = inode->i_private; |
191 | unsigned long flags; |
192 | |
193 | clear_inode(inode); |
194 | if (p) { |
195 | spin_lock_irqsave(&allpstore_lock, flags); |
196 | list_del(&p->list); |
197 | spin_unlock_irqrestore(&allpstore_lock, flags); |
198 | kfree(p); |
199 | } |
200 | } |
201 | |
202 | static const struct inode_operations pstore_dir_inode_operations = { |
203 | .lookup = simple_lookup, |
204 | .unlink = pstore_unlink, |
205 | }; |
206 | |
207 | static struct inode *pstore_get_inode(struct super_block *sb) |
208 | { |
209 | struct inode *inode = new_inode(sb); |
210 | if (inode) { |
211 | inode->i_ino = get_next_ino(); |
212 | inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); |
213 | } |
214 | return inode; |
215 | } |
216 | |
217 | enum { |
218 | Opt_kmsg_bytes, Opt_err |
219 | }; |
220 | |
221 | static const match_table_t tokens = { |
222 | {Opt_kmsg_bytes, "kmsg_bytes=%u"}, |
223 | {Opt_err, NULL} |
224 | }; |
225 | |
226 | static void parse_options(char *options) |
227 | { |
228 | char *p; |
229 | substring_t args[MAX_OPT_ARGS]; |
230 | int option; |
231 | |
232 | if (!options) |
233 | return; |
234 | |
235 | while ((p = strsep(&options, ",")) != NULL) { |
236 | int token; |
237 | |
238 | if (!*p) |
239 | continue; |
240 | |
241 | token = match_token(p, tokens, args); |
242 | switch (token) { |
243 | case Opt_kmsg_bytes: |
244 | if (!match_int(&args[0], &option)) |
245 | pstore_set_kmsg_bytes(option); |
246 | break; |
247 | } |
248 | } |
249 | } |
250 | |
251 | static int pstore_remount(struct super_block *sb, int *flags, char *data) |
252 | { |
253 | sync_filesystem(sb); |
254 | parse_options(data); |
255 | |
256 | return 0; |
257 | } |
258 | |
259 | static const struct super_operations pstore_ops = { |
260 | .statfs = simple_statfs, |
261 | .drop_inode = generic_delete_inode, |
262 | .evict_inode = pstore_evict_inode, |
263 | .remount_fs = pstore_remount, |
264 | .show_options = generic_show_options, |
265 | }; |
266 | |
267 | static struct super_block *pstore_sb; |
268 | |
269 | bool pstore_is_mounted(void) |
270 | { |
271 | return pstore_sb != NULL; |
272 | } |
273 | |
274 | /* |
275 | * Make a regular file in the root directory of our file system. |
276 | * Load it up with "size" bytes of data from "buf". |
277 | * Set the mtime & ctime to the date that this record was originally stored. |
278 | */ |
279 | int pstore_mkfile(enum pstore_type_id type, char *psname, u64 id, int count, |
280 | char *data, bool compressed, size_t size, |
281 | struct timespec time, struct pstore_info *psi) |
282 | { |
283 | struct dentry *root = pstore_sb->s_root; |
284 | struct dentry *dentry; |
285 | struct inode *inode; |
286 | int rc = 0; |
287 | char name[PSTORE_NAMELEN]; |
288 | struct pstore_private *private, *pos; |
289 | unsigned long flags; |
290 | |
291 | spin_lock_irqsave(&allpstore_lock, flags); |
292 | list_for_each_entry(pos, &allpstore, list) { |
293 | if (pos->type == type && |
294 | pos->id == id && |
295 | pos->psi == psi) { |
296 | rc = -EEXIST; |
297 | break; |
298 | } |
299 | } |
300 | spin_unlock_irqrestore(&allpstore_lock, flags); |
301 | if (rc) |
302 | return rc; |
303 | |
304 | rc = -ENOMEM; |
305 | inode = pstore_get_inode(pstore_sb); |
306 | if (!inode) |
307 | goto fail; |
308 | inode->i_mode = S_IFREG | 0444; |
309 | inode->i_fop = &pstore_file_operations; |
310 | private = kmalloc(sizeof *private + size, GFP_KERNEL); |
311 | if (!private) |
312 | goto fail_alloc; |
313 | private->type = type; |
314 | private->id = id; |
315 | private->count = count; |
316 | private->psi = psi; |
317 | |
318 | switch (type) { |
319 | case PSTORE_TYPE_DMESG: |
320 | scnprintf(name, sizeof(name), "dmesg-%s-%lld%s", |
321 | psname, id, compressed ? ".enc.z" : ""); |
322 | break; |
323 | case PSTORE_TYPE_CONSOLE: |
324 | scnprintf(name, sizeof(name), "console-%s-%lld", psname, id); |
325 | break; |
326 | case PSTORE_TYPE_FTRACE: |
327 | scnprintf(name, sizeof(name), "ftrace-%s-%lld", psname, id); |
328 | break; |
329 | case PSTORE_TYPE_MCE: |
330 | scnprintf(name, sizeof(name), "mce-%s-%lld", psname, id); |
331 | break; |
332 | case PSTORE_TYPE_PPC_RTAS: |
333 | scnprintf(name, sizeof(name), "rtas-%s-%lld", psname, id); |
334 | break; |
335 | case PSTORE_TYPE_PPC_OF: |
336 | scnprintf(name, sizeof(name), "powerpc-ofw-%s-%lld", |
337 | psname, id); |
338 | break; |
339 | case PSTORE_TYPE_PPC_COMMON: |
340 | scnprintf(name, sizeof(name), "powerpc-common-%s-%lld", |
341 | psname, id); |
342 | break; |
343 | case PSTORE_TYPE_PMSG: |
344 | scnprintf(name, sizeof(name), "pmsg-%s-%lld", psname, id); |
345 | break; |
346 | case PSTORE_TYPE_PPC_OPAL: |
347 | sprintf(name, "powerpc-opal-%s-%lld", psname, id); |
348 | break; |
349 | case PSTORE_TYPE_UNKNOWN: |
350 | scnprintf(name, sizeof(name), "unknown-%s-%lld", psname, id); |
351 | break; |
352 | default: |
353 | scnprintf(name, sizeof(name), "type%d-%s-%lld", |
354 | type, psname, id); |
355 | break; |
356 | } |
357 | |
358 | inode_lock(d_inode(root)); |
359 | |
360 | dentry = d_alloc_name(root, name); |
361 | if (!dentry) |
362 | goto fail_lockedalloc; |
363 | |
364 | memcpy(private->data, data, size); |
365 | inode->i_size = private->size = size; |
366 | |
367 | inode->i_private = private; |
368 | |
369 | if (time.tv_sec) |
370 | inode->i_mtime = inode->i_ctime = time; |
371 | |
372 | d_add(dentry, inode); |
373 | |
374 | spin_lock_irqsave(&allpstore_lock, flags); |
375 | list_add(&private->list, &allpstore); |
376 | spin_unlock_irqrestore(&allpstore_lock, flags); |
377 | |
378 | inode_unlock(d_inode(root)); |
379 | |
380 | return 0; |
381 | |
382 | fail_lockedalloc: |
383 | inode_unlock(d_inode(root)); |
384 | kfree(private); |
385 | fail_alloc: |
386 | iput(inode); |
387 | |
388 | fail: |
389 | return rc; |
390 | } |
391 | |
392 | static int pstore_fill_super(struct super_block *sb, void *data, int silent) |
393 | { |
394 | struct inode *inode; |
395 | |
396 | save_mount_options(sb, data); |
397 | |
398 | pstore_sb = sb; |
399 | |
400 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
401 | sb->s_blocksize = PAGE_SIZE; |
402 | sb->s_blocksize_bits = PAGE_SHIFT; |
403 | sb->s_magic = PSTOREFS_MAGIC; |
404 | sb->s_op = &pstore_ops; |
405 | sb->s_time_gran = 1; |
406 | |
407 | parse_options(data); |
408 | |
409 | inode = pstore_get_inode(sb); |
410 | if (inode) { |
411 | inode->i_mode = S_IFDIR | 0755; |
412 | inode->i_op = &pstore_dir_inode_operations; |
413 | inode->i_fop = &simple_dir_operations; |
414 | inc_nlink(inode); |
415 | } |
416 | sb->s_root = d_make_root(inode); |
417 | if (!sb->s_root) |
418 | return -ENOMEM; |
419 | |
420 | pstore_get_records(0); |
421 | |
422 | return 0; |
423 | } |
424 | |
425 | static struct dentry *pstore_mount(struct file_system_type *fs_type, |
426 | int flags, const char *dev_name, void *data) |
427 | { |
428 | return mount_single(fs_type, flags, data, pstore_fill_super); |
429 | } |
430 | |
431 | static void pstore_kill_sb(struct super_block *sb) |
432 | { |
433 | kill_litter_super(sb); |
434 | pstore_sb = NULL; |
435 | } |
436 | |
437 | static struct file_system_type pstore_fs_type = { |
438 | .owner = THIS_MODULE, |
439 | .name = "pstore", |
440 | .mount = pstore_mount, |
441 | .kill_sb = pstore_kill_sb, |
442 | }; |
443 | |
444 | static int __init init_pstore_fs(void) |
445 | { |
446 | int err; |
447 | |
448 | /* Create a convenient mount point for people to access pstore */ |
449 | err = sysfs_create_mount_point(fs_kobj, "pstore"); |
450 | if (err) |
451 | goto out; |
452 | |
453 | err = register_filesystem(&pstore_fs_type); |
454 | if (err < 0) |
455 | sysfs_remove_mount_point(fs_kobj, "pstore"); |
456 | |
457 | out: |
458 | return err; |
459 | } |
460 | module_init(init_pstore_fs) |
461 | |
462 | static void __exit exit_pstore_fs(void) |
463 | { |
464 | unregister_filesystem(&pstore_fs_type); |
465 | sysfs_remove_mount_point(fs_kobj, "pstore"); |
466 | } |
467 | module_exit(exit_pstore_fs) |
468 | |
469 | MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>"); |
470 | MODULE_LICENSE("GPL"); |
471 |