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