blob: 3d1f047e4f413285fcece3b2a35ab64c89723092 [file] [log] [blame]
Tony Luckca01d6d2010-12-28 14:25:21 -08001/*
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>
Luck, Tony6dda9262011-08-11 15:14:39 -070027#include <linux/list.h>
Tony Luckca01d6d2010-12-28 14:25:21 -080028#include <linux/string.h>
29#include <linux/mount.h>
Anton Vorontsov060287b2012-07-09 17:10:41 -070030#include <linux/seq_file.h>
Tony Luckca01d6d2010-12-28 14:25:21 -080031#include <linux/ramfs.h>
Luck, Tony366f7e72011-03-18 15:33:43 -070032#include <linux/parser.h>
Tony Luckca01d6d2010-12-28 14:25:21 -080033#include <linux/sched.h>
34#include <linux/magic.h>
35#include <linux/pstore.h>
36#include <linux/slab.h>
Luck, Tony6dda9262011-08-11 15:14:39 -070037#include <linux/spinlock.h>
Tony Luckca01d6d2010-12-28 14:25:21 -080038#include <linux/uaccess.h>
Sebastian Schmidt68c4a4f2014-10-19 20:05:15 +020039#include <linux/syslog.h>
Tony Luckca01d6d2010-12-28 14:25:21 -080040
41#include "internal.h"
42
43#define PSTORE_NAMELEN 64
44
Luck, Tony6dda9262011-08-11 15:14:39 -070045static DEFINE_SPINLOCK(allpstore_lock);
46static LIST_HEAD(allpstore);
47
Tony Luckca01d6d2010-12-28 14:25:21 -080048struct pstore_private {
Luck, Tony6dda9262011-08-11 15:14:39 -070049 struct list_head list;
Matthew Garrett638c1fd2011-07-21 16:57:52 -040050 struct pstore_info *psi;
Matthew Garrett56280682011-07-21 16:57:53 -040051 enum pstore_type_id type;
52 u64 id;
Seiji Aguchi755d4fe2012-11-26 16:07:44 -080053 int count;
Tony Luckfbe0aa12011-03-17 16:29:15 -070054 ssize_t size;
Kees Cook1dfff7d2017-03-04 22:46:41 -080055 char *buf;
Tony Luckca01d6d2010-12-28 14:25:21 -080056};
57
Anton Vorontsov060287b2012-07-09 17:10:41 -070058struct 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
Kees Cook1dfff7d2017-03-04 22:46:41 -080066static void free_pstore_private(struct pstore_private *private)
67{
68 if (!private)
69 return;
70 kfree(private->buf);
71 kfree(private);
72}
73
Anton Vorontsov060287b2012-07-09 17:10:41 -070074static 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;
Kees Cook1dfff7d2017-03-04 22:46:41 -0800116 struct pstore_ftrace_record *rec = (void *)(ps->buf + data->off);
Anton Vorontsov060287b2012-07-09 17:10:41 -0700117
Joel Fernandesfbccdeb2016-10-20 00:34:05 -0700118 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);
Anton Vorontsov060287b2012-07-09 17:10:41 -0700123
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
Sebastian Schmidt68c4a4f2014-10-19 20:05:15 +0200134static 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
Tony Luckfbe0aa12011-03-17 16:29:15 -0700146static ssize_t pstore_file_read(struct file *file, char __user *userbuf,
147 size_t count, loff_t *ppos)
148{
Anton Vorontsov060287b2012-07-09 17:10:41 -0700149 struct seq_file *sf = file->private_data;
150 struct pstore_private *ps = sf->private;
Tony Luckfbe0aa12011-03-17 16:29:15 -0700151
Anton Vorontsov060287b2012-07-09 17:10:41 -0700152 if (ps->type == PSTORE_TYPE_FTRACE)
153 return seq_read(file, userbuf, count, ppos);
Kees Cook1dfff7d2017-03-04 22:46:41 -0800154 return simple_read_from_buffer(userbuf, count, ppos, ps->buf, ps->size);
Tony Luckfbe0aa12011-03-17 16:29:15 -0700155}
156
Anton Vorontsov060287b2012-07-09 17:10:41 -0700157static 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
Sebastian Schmidt68c4a4f2014-10-19 20:05:15 +0200164 err = pstore_check_syslog_permissions(ps);
165 if (err)
166 return err;
167
Anton Vorontsov060287b2012-07-09 17:10:41 -0700168 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
Andrew Morton965c8e52012-12-17 15:59:39 -0800181static loff_t pstore_file_llseek(struct file *file, loff_t off, int whence)
Anton Vorontsov060287b2012-07-09 17:10:41 -0700182{
183 struct seq_file *sf = file->private_data;
184
185 if (sf->op)
Andrew Morton965c8e52012-12-17 15:59:39 -0800186 return seq_lseek(file, off, whence);
187 return default_llseek(file, off, whence);
Anton Vorontsov060287b2012-07-09 17:10:41 -0700188}
189
Tony Luckfbe0aa12011-03-17 16:29:15 -0700190static const struct file_operations pstore_file_operations = {
Anton Vorontsov060287b2012-07-09 17:10:41 -0700191 .open = pstore_file_open,
192 .read = pstore_file_read,
193 .llseek = pstore_file_llseek,
194 .release = seq_release,
Tony Luckfbe0aa12011-03-17 16:29:15 -0700195};
Tony Luckca01d6d2010-12-28 14:25:21 -0800196
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{
David Howells2b0143b2015-03-17 22:25:59 +0000203 struct pstore_private *p = d_inode(dentry)->i_private;
Sebastian Schmidt68c4a4f2014-10-19 20:05:15 +0200204 int err;
205
206 err = pstore_check_syslog_permissions(p);
207 if (err)
208 return err;
Tony Luckca01d6d2010-12-28 14:25:21 -0800209
Namhyung Kime9e360b2016-10-19 10:23:40 +0900210 if (p->psi->erase) {
211 mutex_lock(&p->psi->read_mutex);
Seiji Aguchi755d4fe2012-11-26 16:07:44 -0800212 p->psi->erase(p->type, p->id, p->count,
David Howells2b0143b2015-03-17 22:25:59 +0000213 d_inode(dentry)->i_ctime, p->psi);
Namhyung Kime9e360b2016-10-19 10:23:40 +0900214 mutex_unlock(&p->psi->read_mutex);
215 } else {
Aruna Balakrishnaiahbf288332013-06-25 14:33:56 +0530216 return -EPERM;
Namhyung Kime9e360b2016-10-19 10:23:40 +0900217 }
Tony Luckca01d6d2010-12-28 14:25:21 -0800218
219 return simple_unlink(dir, dentry);
220}
221
Tony Lucka872d512011-03-18 11:44:48 -0700222static void pstore_evict_inode(struct inode *inode)
223{
Luck, Tony6dda9262011-08-11 15:14:39 -0700224 struct pstore_private *p = inode->i_private;
225 unsigned long flags;
226
Jan Karadbd57682012-05-03 14:48:02 +0200227 clear_inode(inode);
Luck, Tony6dda9262011-08-11 15:14:39 -0700228 if (p) {
229 spin_lock_irqsave(&allpstore_lock, flags);
230 list_del(&p->list);
231 spin_unlock_irqrestore(&allpstore_lock, flags);
Kees Cook1dfff7d2017-03-04 22:46:41 -0800232 free_pstore_private(p);
Luck, Tony6dda9262011-08-11 15:14:39 -0700233 }
Tony Lucka872d512011-03-18 11:44:48 -0700234}
235
Tony Luckca01d6d2010-12-28 14:25:21 -0800236static const struct inode_operations pstore_dir_inode_operations = {
237 .lookup = simple_lookup,
238 .unlink = pstore_unlink,
239};
240
Al Viro22a71c32012-03-22 12:26:35 -0400241static struct inode *pstore_get_inode(struct super_block *sb)
Tony Luckfbe0aa12011-03-17 16:29:15 -0700242{
243 struct inode *inode = new_inode(sb);
Tony Luckfbe0aa12011-03-17 16:29:15 -0700244 if (inode) {
245 inode->i_ino = get_next_ino();
Deepa Dinamani078cd822016-09-14 07:48:04 -0700246 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Tony Luckfbe0aa12011-03-17 16:29:15 -0700247 }
248 return inode;
249}
250
Luck, Tony366f7e72011-03-18 15:33:43 -0700251enum {
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{
Theodore Ts'o02b99842014-03-13 10:14:33 -0400287 sync_filesystem(sb);
Luck, Tony366f7e72011-03-18 15:33:43 -0700288 parse_options(data);
289
290 return 0;
291}
292
Tony Luckca01d6d2010-12-28 14:25:21 -0800293static const struct super_operations pstore_ops = {
294 .statfs = simple_statfs,
295 .drop_inode = generic_delete_inode,
Tony Lucka872d512011-03-18 11:44:48 -0700296 .evict_inode = pstore_evict_inode,
Luck, Tony366f7e72011-03-18 15:33:43 -0700297 .remount_fs = pstore_remount,
Tony Luckca01d6d2010-12-28 14:25:21 -0800298 .show_options = generic_show_options,
299};
300
301static struct super_block *pstore_sb;
Tony Luckca01d6d2010-12-28 14:25:21 -0800302
Geliang Tang7e26e9f2015-10-22 01:02:33 -0700303bool pstore_is_mounted(void)
Tony Luckca01d6d2010-12-28 14:25:21 -0800304{
Tony Luckfbe0aa12011-03-17 16:29:15 -0700305 return pstore_sb != NULL;
Tony Luckca01d6d2010-12-28 14:25:21 -0800306}
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 */
Kees Cook1edd1aa2017-03-03 18:16:32 -0800313int pstore_mkfile(struct pstore_record *record)
Tony Luckca01d6d2010-12-28 14:25:21 -0800314{
315 struct dentry *root = pstore_sb->s_root;
316 struct dentry *dentry;
317 struct inode *inode;
Luck, Tony6dda9262011-08-11 15:14:39 -0700318 int rc = 0;
Tony Luckca01d6d2010-12-28 14:25:21 -0800319 char name[PSTORE_NAMELEN];
Luck, Tony6dda9262011-08-11 15:14:39 -0700320 struct pstore_private *private, *pos;
321 unsigned long flags;
Kees Cook1edd1aa2017-03-03 18:16:32 -0800322 size_t size = record->size + record->ecc_notice_size;
Luck, Tony6dda9262011-08-11 15:14:39 -0700323
324 spin_lock_irqsave(&allpstore_lock, flags);
325 list_for_each_entry(pos, &allpstore, list) {
Kees Cook1edd1aa2017-03-03 18:16:32 -0800326 if (pos->type == record->type &&
327 pos->id == record->id &&
328 pos->psi == record->psi) {
Luck, Tony6dda9262011-08-11 15:14:39 -0700329 rc = -EEXIST;
330 break;
331 }
332 }
333 spin_unlock_irqrestore(&allpstore_lock, flags);
334 if (rc)
335 return rc;
Tony Luckca01d6d2010-12-28 14:25:21 -0800336
337 rc = -ENOMEM;
Al Viro22a71c32012-03-22 12:26:35 -0400338 inode = pstore_get_inode(pstore_sb);
Tony Luckca01d6d2010-12-28 14:25:21 -0800339 if (!inode)
340 goto fail;
Al Viro22a71c32012-03-22 12:26:35 -0400341 inode->i_mode = S_IFREG | 0444;
342 inode->i_fop = &pstore_file_operations;
Kees Cook1dfff7d2017-03-04 22:46:41 -0800343 private = kzalloc(sizeof(*private), GFP_KERNEL);
Tony Luckca01d6d2010-12-28 14:25:21 -0800344 if (!private)
345 goto fail_alloc;
Kees Cook1edd1aa2017-03-03 18:16:32 -0800346 private->type = record->type;
347 private->id = record->id;
348 private->count = record->count;
349 private->psi = record->psi;
Tony Luckca01d6d2010-12-28 14:25:21 -0800350
Kees Cook1edd1aa2017-03-03 18:16:32 -0800351 switch (record->type) {
Tony Luckca01d6d2010-12-28 14:25:21 -0800352 case PSTORE_TYPE_DMESG:
Mark Salyzyndbaffde2015-01-06 11:18:24 -0800353 scnprintf(name, sizeof(name), "dmesg-%s-%lld%s",
Kees Cook1edd1aa2017-03-03 18:16:32 -0800354 record->psi->name, record->id,
355 record->compressed ? ".enc.z" : "");
Tony Luckca01d6d2010-12-28 14:25:21 -0800356 break;
Anton Vorontsovf29e5952012-05-26 06:20:19 -0700357 case PSTORE_TYPE_CONSOLE:
Kees Cook1edd1aa2017-03-03 18:16:32 -0800358 scnprintf(name, sizeof(name), "console-%s-%lld",
359 record->psi->name, record->id);
Anton Vorontsovf29e5952012-05-26 06:20:19 -0700360 break;
Anton Vorontsov060287b2012-07-09 17:10:41 -0700361 case PSTORE_TYPE_FTRACE:
Kees Cook1edd1aa2017-03-03 18:16:32 -0800362 scnprintf(name, sizeof(name), "ftrace-%s-%lld",
363 record->psi->name, record->id);
Anton Vorontsov060287b2012-07-09 17:10:41 -0700364 break;
Tony Luckca01d6d2010-12-28 14:25:21 -0800365 case PSTORE_TYPE_MCE:
Kees Cook1edd1aa2017-03-03 18:16:32 -0800366 scnprintf(name, sizeof(name), "mce-%s-%lld",
367 record->psi->name, record->id);
Tony Luckca01d6d2010-12-28 14:25:21 -0800368 break;
Aruna Balakrishnaiah69020ee2013-06-06 00:21:44 +0530369 case PSTORE_TYPE_PPC_RTAS:
Kees Cook1edd1aa2017-03-03 18:16:32 -0800370 scnprintf(name, sizeof(name), "rtas-%s-%lld",
371 record->psi->name, record->id);
Aruna Balakrishnaiah69020ee2013-06-06 00:21:44 +0530372 break;
Aruna Balakrishnaiahf33f7482013-06-06 00:22:10 +0530373 case PSTORE_TYPE_PPC_OF:
Mark Salyzyndbaffde2015-01-06 11:18:24 -0800374 scnprintf(name, sizeof(name), "powerpc-ofw-%s-%lld",
Kees Cook1edd1aa2017-03-03 18:16:32 -0800375 record->psi->name, record->id);
Aruna Balakrishnaiahf33f7482013-06-06 00:22:10 +0530376 break;
Aruna Balakrishnaiaha5e47972013-06-06 00:22:20 +0530377 case PSTORE_TYPE_PPC_COMMON:
Mark Salyzyndbaffde2015-01-06 11:18:24 -0800378 scnprintf(name, sizeof(name), "powerpc-common-%s-%lld",
Kees Cook1edd1aa2017-03-03 18:16:32 -0800379 record->psi->name, record->id);
Aruna Balakrishnaiaha5e47972013-06-06 00:22:20 +0530380 break;
Mark Salyzyn9d5438f2015-01-16 16:01:10 -0800381 case PSTORE_TYPE_PMSG:
Kees Cook1edd1aa2017-03-03 18:16:32 -0800382 scnprintf(name, sizeof(name), "pmsg-%s-%lld",
383 record->psi->name, record->id);
Mark Salyzyn9d5438f2015-01-16 16:01:10 -0800384 break;
Hari Bathiniae011d22015-02-06 01:06:28 +0530385 case PSTORE_TYPE_PPC_OPAL:
Kees Cook1edd1aa2017-03-03 18:16:32 -0800386 scnprintf(name, sizeof(name), "powerpc-opal-%s-%lld",
387 record->psi->name, record->id);
Hari Bathiniae011d22015-02-06 01:06:28 +0530388 break;
Tony Luckca01d6d2010-12-28 14:25:21 -0800389 case PSTORE_TYPE_UNKNOWN:
Kees Cook1edd1aa2017-03-03 18:16:32 -0800390 scnprintf(name, sizeof(name), "unknown-%s-%lld",
391 record->psi->name, record->id);
Tony Luckca01d6d2010-12-28 14:25:21 -0800392 break;
393 default:
Mark Salyzyndbaffde2015-01-06 11:18:24 -0800394 scnprintf(name, sizeof(name), "type%d-%s-%lld",
Kees Cook1edd1aa2017-03-03 18:16:32 -0800395 record->type, record->psi->name, record->id);
Tony Luckca01d6d2010-12-28 14:25:21 -0800396 break;
397 }
398
Al Viro59551022016-01-22 15:40:57 -0500399 inode_lock(d_inode(root));
Tony Luckca01d6d2010-12-28 14:25:21 -0800400
Tony Luckca01d6d2010-12-28 14:25:21 -0800401 dentry = d_alloc_name(root, name);
Dan Carpenterc39524e2013-08-14 10:55:49 -0700402 if (!dentry)
Tony Luckca01d6d2010-12-28 14:25:21 -0800403 goto fail_lockedalloc;
404
Kees Cook1dfff7d2017-03-04 22:46:41 -0800405 private->buf = record->buf;
Tony Luckfbe0aa12011-03-17 16:29:15 -0700406 inode->i_size = private->size = size;
Tony Luckca01d6d2010-12-28 14:25:21 -0800407
408 inode->i_private = private;
409
Kees Cook1edd1aa2017-03-03 18:16:32 -0800410 if (record->time.tv_sec)
411 inode->i_mtime = inode->i_ctime = record->time;
Tony Luckca01d6d2010-12-28 14:25:21 -0800412
Tony Luckfbe0aa12011-03-17 16:29:15 -0700413 d_add(dentry, inode);
Tony Luckca01d6d2010-12-28 14:25:21 -0800414
Luck, Tony6dda9262011-08-11 15:14:39 -0700415 spin_lock_irqsave(&allpstore_lock, flags);
416 list_add(&private->list, &allpstore);
417 spin_unlock_irqrestore(&allpstore_lock, flags);
418
Al Viro59551022016-01-22 15:40:57 -0500419 inode_unlock(d_inode(root));
Tony Luckfbe0aa12011-03-17 16:29:15 -0700420
421 return 0;
Tony Luckca01d6d2010-12-28 14:25:21 -0800422
423fail_lockedalloc:
Al Viro59551022016-01-22 15:40:57 -0500424 inode_unlock(d_inode(root));
Kees Cook1dfff7d2017-03-04 22:46:41 -0800425 free_pstore_private(private);
Tony Luckca01d6d2010-12-28 14:25:21 -0800426fail_alloc:
427 iput(inode);
428
429fail:
430 return rc;
431}
432
Anton Vorontsov364ed2f2012-05-26 06:07:53 -0700433static int pstore_fill_super(struct super_block *sb, void *data, int silent)
Tony Luckca01d6d2010-12-28 14:25:21 -0800434{
Al Viro318ceed2012-02-12 22:08:01 -0500435 struct inode *inode;
Tony Luckca01d6d2010-12-28 14:25:21 -0800436
437 save_mount_options(sb, data);
438
439 pstore_sb = sb;
440
441 sb->s_maxbytes = MAX_LFS_FILESIZE;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300442 sb->s_blocksize = PAGE_SIZE;
443 sb->s_blocksize_bits = PAGE_SHIFT;
Tony Luckca01d6d2010-12-28 14:25:21 -0800444 sb->s_magic = PSTOREFS_MAGIC;
445 sb->s_op = &pstore_ops;
446 sb->s_time_gran = 1;
447
Luck, Tony366f7e72011-03-18 15:33:43 -0700448 parse_options(data);
449
Al Viro22a71c32012-03-22 12:26:35 -0400450 inode = pstore_get_inode(sb);
Al Viro318ceed2012-02-12 22:08:01 -0500451 if (inode) {
Al Viro22a71c32012-03-22 12:26:35 -0400452 inode->i_mode = S_IFDIR | 0755;
Al Viro318ceed2012-02-12 22:08:01 -0500453 inode->i_op = &pstore_dir_inode_operations;
Al Viro22a71c32012-03-22 12:26:35 -0400454 inode->i_fop = &simple_dir_operations;
455 inc_nlink(inode);
Tony Luckca01d6d2010-12-28 14:25:21 -0800456 }
Al Viro318ceed2012-02-12 22:08:01 -0500457 sb->s_root = d_make_root(inode);
458 if (!sb->s_root)
459 return -ENOMEM;
Tony Luckca01d6d2010-12-28 14:25:21 -0800460
Luck, Tony6dda9262011-08-11 15:14:39 -0700461 pstore_get_records(0);
Tony Luckca01d6d2010-12-28 14:25:21 -0800462
463 return 0;
Tony Luckca01d6d2010-12-28 14:25:21 -0800464}
465
Tony Luckfbe0aa12011-03-17 16:29:15 -0700466static struct dentry *pstore_mount(struct file_system_type *fs_type,
467 int flags, const char *dev_name, void *data)
Tony Luckca01d6d2010-12-28 14:25:21 -0800468{
Tony Luckfbe0aa12011-03-17 16:29:15 -0700469 return mount_single(fs_type, flags, data, pstore_fill_super);
Tony Luckca01d6d2010-12-28 14:25:21 -0800470}
471
472static void pstore_kill_sb(struct super_block *sb)
473{
474 kill_litter_super(sb);
475 pstore_sb = NULL;
Tony Luckca01d6d2010-12-28 14:25:21 -0800476}
477
478static struct file_system_type pstore_fs_type = {
Geliang Tangee1d2672015-10-20 00:39:03 -0700479 .owner = THIS_MODULE,
Tony Luckca01d6d2010-12-28 14:25:21 -0800480 .name = "pstore",
Tony Luckfbe0aa12011-03-17 16:29:15 -0700481 .mount = pstore_mount,
Tony Luckca01d6d2010-12-28 14:25:21 -0800482 .kill_sb = pstore_kill_sb,
483};
484
485static int __init init_pstore_fs(void)
486{
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -0500487 int err;
Josh Boyerfb0af3f2013-02-12 13:07:22 -0800488
489 /* Create a convenient mount point for people to access pstore */
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -0500490 err = sysfs_create_mount_point(fs_kobj, "pstore");
491 if (err)
Josh Boyerfb0af3f2013-02-12 13:07:22 -0800492 goto out;
Josh Boyerfb0af3f2013-02-12 13:07:22 -0800493
494 err = register_filesystem(&pstore_fs_type);
495 if (err < 0)
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -0500496 sysfs_remove_mount_point(fs_kobj, "pstore");
Josh Boyerfb0af3f2013-02-12 13:07:22 -0800497
498out:
499 return err;
Tony Luckca01d6d2010-12-28 14:25:21 -0800500}
501module_init(init_pstore_fs)
502
Geliang Tangee1d2672015-10-20 00:39:03 -0700503static 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
Tony Luckca01d6d2010-12-28 14:25:21 -0800510MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
511MODULE_LICENSE("GPL");