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