blob: b19884a1ba7737f90b051372821ad268af09d317 [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>
27#include <linux/string.h>
28#include <linux/mount.h>
29#include <linux/ramfs.h>
Luck, Tony366f7e72011-03-18 15:33:43 -070030#include <linux/parser.h>
Tony Luckca01d6d2010-12-28 14:25:21 -080031#include <linux/sched.h>
32#include <linux/magic.h>
33#include <linux/pstore.h>
34#include <linux/slab.h>
35#include <linux/uaccess.h>
36
37#include "internal.h"
38
39#define PSTORE_NAMELEN 64
40
41struct pstore_private {
42 u64 id;
Matthew Garrett638c1fd2011-07-21 16:57:52 -040043 struct pstore_info *psi;
Tony Luckfbe0aa12011-03-17 16:29:15 -070044 ssize_t size;
45 char data[];
Tony Luckca01d6d2010-12-28 14:25:21 -080046};
47
Tony Luckfbe0aa12011-03-17 16:29:15 -070048static int pstore_file_open(struct inode *inode, struct file *file)
49{
50 file->private_data = inode->i_private;
51 return 0;
52}
53
54static ssize_t pstore_file_read(struct file *file, char __user *userbuf,
55 size_t count, loff_t *ppos)
56{
57 struct pstore_private *ps = file->private_data;
58
59 return simple_read_from_buffer(userbuf, count, ppos, ps->data, ps->size);
60}
61
62static const struct file_operations pstore_file_operations = {
63 .open = pstore_file_open,
64 .read = pstore_file_read,
65 .llseek = default_llseek,
66};
Tony Luckca01d6d2010-12-28 14:25:21 -080067
68/*
69 * When a file is unlinked from our file system we call the
70 * platform driver to erase the record from persistent store.
71 */
72static int pstore_unlink(struct inode *dir, struct dentry *dentry)
73{
74 struct pstore_private *p = dentry->d_inode->i_private;
75
Matthew Garrett638c1fd2011-07-21 16:57:52 -040076 p->psi->erase(p->id, p->psi);
Tony Luckca01d6d2010-12-28 14:25:21 -080077
78 return simple_unlink(dir, dentry);
79}
80
Tony Lucka872d512011-03-18 11:44:48 -070081static void pstore_evict_inode(struct inode *inode)
82{
83 end_writeback(inode);
84 kfree(inode->i_private);
85}
86
Tony Luckca01d6d2010-12-28 14:25:21 -080087static const struct inode_operations pstore_dir_inode_operations = {
88 .lookup = simple_lookup,
89 .unlink = pstore_unlink,
90};
91
Tony Luckfbe0aa12011-03-17 16:29:15 -070092static struct inode *pstore_get_inode(struct super_block *sb,
93 const struct inode *dir, int mode, dev_t dev)
94{
95 struct inode *inode = new_inode(sb);
96
97 if (inode) {
98 inode->i_ino = get_next_ino();
99 inode->i_uid = inode->i_gid = 0;
100 inode->i_mode = mode;
101 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
102 switch (mode & S_IFMT) {
103 case S_IFREG:
104 inode->i_fop = &pstore_file_operations;
105 break;
106 case S_IFDIR:
107 inode->i_op = &pstore_dir_inode_operations;
108 inode->i_fop = &simple_dir_operations;
109 inc_nlink(inode);
110 break;
111 }
112 }
113 return inode;
114}
115
Luck, Tony366f7e72011-03-18 15:33:43 -0700116enum {
117 Opt_kmsg_bytes, Opt_err
118};
119
120static const match_table_t tokens = {
121 {Opt_kmsg_bytes, "kmsg_bytes=%u"},
122 {Opt_err, NULL}
123};
124
125static void parse_options(char *options)
126{
127 char *p;
128 substring_t args[MAX_OPT_ARGS];
129 int option;
130
131 if (!options)
132 return;
133
134 while ((p = strsep(&options, ",")) != NULL) {
135 int token;
136
137 if (!*p)
138 continue;
139
140 token = match_token(p, tokens, args);
141 switch (token) {
142 case Opt_kmsg_bytes:
143 if (!match_int(&args[0], &option))
144 pstore_set_kmsg_bytes(option);
145 break;
146 }
147 }
148}
149
150static int pstore_remount(struct super_block *sb, int *flags, char *data)
151{
152 parse_options(data);
153
154 return 0;
155}
156
Tony Luckca01d6d2010-12-28 14:25:21 -0800157static const struct super_operations pstore_ops = {
158 .statfs = simple_statfs,
159 .drop_inode = generic_delete_inode,
Tony Lucka872d512011-03-18 11:44:48 -0700160 .evict_inode = pstore_evict_inode,
Luck, Tony366f7e72011-03-18 15:33:43 -0700161 .remount_fs = pstore_remount,
Tony Luckca01d6d2010-12-28 14:25:21 -0800162 .show_options = generic_show_options,
163};
164
165static struct super_block *pstore_sb;
Tony Luckca01d6d2010-12-28 14:25:21 -0800166
167int pstore_is_mounted(void)
168{
Tony Luckfbe0aa12011-03-17 16:29:15 -0700169 return pstore_sb != NULL;
Tony Luckca01d6d2010-12-28 14:25:21 -0800170}
171
172/*
173 * Make a regular file in the root directory of our file system.
174 * Load it up with "size" bytes of data from "buf".
175 * Set the mtime & ctime to the date that this record was originally stored.
176 */
177int pstore_mkfile(enum pstore_type_id type, char *psname, u64 id,
Matthew Garrett638c1fd2011-07-21 16:57:52 -0400178 char *data, size_t size, struct timespec time,
179 struct pstore_info *psi)
Tony Luckca01d6d2010-12-28 14:25:21 -0800180{
181 struct dentry *root = pstore_sb->s_root;
182 struct dentry *dentry;
183 struct inode *inode;
184 int rc;
185 char name[PSTORE_NAMELEN];
186 struct pstore_private *private;
187
188 rc = -ENOMEM;
189 inode = pstore_get_inode(pstore_sb, root->d_inode, S_IFREG | 0444, 0);
190 if (!inode)
191 goto fail;
Tony Luckfbe0aa12011-03-17 16:29:15 -0700192 private = kmalloc(sizeof *private + size, GFP_KERNEL);
Tony Luckca01d6d2010-12-28 14:25:21 -0800193 if (!private)
194 goto fail_alloc;
195 private->id = id;
Matthew Garrett638c1fd2011-07-21 16:57:52 -0400196 private->psi = psi;
Tony Luckca01d6d2010-12-28 14:25:21 -0800197
198 switch (type) {
199 case PSTORE_TYPE_DMESG:
200 sprintf(name, "dmesg-%s-%lld", psname, id);
201 break;
202 case PSTORE_TYPE_MCE:
203 sprintf(name, "mce-%s-%lld", psname, id);
204 break;
205 case PSTORE_TYPE_UNKNOWN:
206 sprintf(name, "unknown-%s-%lld", psname, id);
207 break;
208 default:
209 sprintf(name, "type%d-%s-%lld", type, psname, id);
210 break;
211 }
212
213 mutex_lock(&root->d_inode->i_mutex);
214
215 rc = -ENOSPC;
216 dentry = d_alloc_name(root, name);
217 if (IS_ERR(dentry))
218 goto fail_lockedalloc;
219
Tony Luckfbe0aa12011-03-17 16:29:15 -0700220 memcpy(private->data, data, size);
221 inode->i_size = private->size = size;
Tony Luckca01d6d2010-12-28 14:25:21 -0800222
223 inode->i_private = private;
224
225 if (time.tv_sec)
226 inode->i_mtime = inode->i_ctime = time;
227
Tony Luckfbe0aa12011-03-17 16:29:15 -0700228 d_add(dentry, inode);
Tony Luckca01d6d2010-12-28 14:25:21 -0800229
Tony Luckca01d6d2010-12-28 14:25:21 -0800230 mutex_unlock(&root->d_inode->i_mutex);
Tony Luckfbe0aa12011-03-17 16:29:15 -0700231
232 return 0;
Tony Luckca01d6d2010-12-28 14:25:21 -0800233
234fail_lockedalloc:
235 mutex_unlock(&root->d_inode->i_mutex);
236 kfree(private);
237fail_alloc:
238 iput(inode);
239
240fail:
241 return rc;
242}
243
244int pstore_fill_super(struct super_block *sb, void *data, int silent)
245{
246 struct inode *inode = NULL;
247 struct dentry *root;
248 int err;
249
250 save_mount_options(sb, data);
251
252 pstore_sb = sb;
253
254 sb->s_maxbytes = MAX_LFS_FILESIZE;
255 sb->s_blocksize = PAGE_CACHE_SIZE;
256 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
257 sb->s_magic = PSTOREFS_MAGIC;
258 sb->s_op = &pstore_ops;
259 sb->s_time_gran = 1;
260
Luck, Tony366f7e72011-03-18 15:33:43 -0700261 parse_options(data);
262
Tony Luckca01d6d2010-12-28 14:25:21 -0800263 inode = pstore_get_inode(sb, NULL, S_IFDIR | 0755, 0);
264 if (!inode) {
265 err = -ENOMEM;
266 goto fail;
267 }
268 /* override ramfs "dir" options so we catch unlink(2) */
269 inode->i_op = &pstore_dir_inode_operations;
270
271 root = d_alloc_root(inode);
272 sb->s_root = root;
273 if (!root) {
274 err = -ENOMEM;
275 goto fail;
276 }
277
278 pstore_get_records();
279
280 return 0;
281fail:
282 iput(inode);
283 return err;
284}
285
Tony Luckfbe0aa12011-03-17 16:29:15 -0700286static struct dentry *pstore_mount(struct file_system_type *fs_type,
287 int flags, const char *dev_name, void *data)
Tony Luckca01d6d2010-12-28 14:25:21 -0800288{
Tony Luckfbe0aa12011-03-17 16:29:15 -0700289 return mount_single(fs_type, flags, data, pstore_fill_super);
Tony Luckca01d6d2010-12-28 14:25:21 -0800290}
291
292static void pstore_kill_sb(struct super_block *sb)
293{
294 kill_litter_super(sb);
295 pstore_sb = NULL;
Tony Luckca01d6d2010-12-28 14:25:21 -0800296}
297
298static struct file_system_type pstore_fs_type = {
299 .name = "pstore",
Tony Luckfbe0aa12011-03-17 16:29:15 -0700300 .mount = pstore_mount,
Tony Luckca01d6d2010-12-28 14:25:21 -0800301 .kill_sb = pstore_kill_sb,
302};
303
304static int __init init_pstore_fs(void)
305{
Luck, Tony366f7e72011-03-18 15:33:43 -0700306 return register_filesystem(&pstore_fs_type);
Tony Luckca01d6d2010-12-28 14:25:21 -0800307}
308module_init(init_pstore_fs)
309
310MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
311MODULE_LICENSE("GPL");