blob: 08342232cb1c4f82ddb07db1fc4c8fcb9908d72b [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>
30#include <linux/sched.h>
31#include <linux/magic.h>
32#include <linux/pstore.h>
33#include <linux/slab.h>
34#include <linux/uaccess.h>
35
36#include "internal.h"
37
38#define PSTORE_NAMELEN 64
39
40struct pstore_private {
41 u64 id;
42 int (*erase)(u64);
Tony Luckfbe0aa12011-03-17 16:29:15 -070043 ssize_t size;
44 char data[];
Tony Luckca01d6d2010-12-28 14:25:21 -080045};
46
Tony Luckfbe0aa12011-03-17 16:29:15 -070047static int pstore_file_open(struct inode *inode, struct file *file)
48{
49 file->private_data = inode->i_private;
50 return 0;
51}
52
53static ssize_t pstore_file_read(struct file *file, char __user *userbuf,
54 size_t count, loff_t *ppos)
55{
56 struct pstore_private *ps = file->private_data;
57
58 return simple_read_from_buffer(userbuf, count, ppos, ps->data, ps->size);
59}
60
61static const struct file_operations pstore_file_operations = {
62 .open = pstore_file_open,
63 .read = pstore_file_read,
64 .llseek = default_llseek,
65};
Tony Luckca01d6d2010-12-28 14:25:21 -080066
67/*
68 * When a file is unlinked from our file system we call the
69 * platform driver to erase the record from persistent store.
70 */
71static int pstore_unlink(struct inode *dir, struct dentry *dentry)
72{
73 struct pstore_private *p = dentry->d_inode->i_private;
74
75 p->erase(p->id);
76 kfree(p);
77
78 return simple_unlink(dir, dentry);
79}
80
81static const struct inode_operations pstore_dir_inode_operations = {
82 .lookup = simple_lookup,
83 .unlink = pstore_unlink,
84};
85
Tony Luckfbe0aa12011-03-17 16:29:15 -070086static struct inode *pstore_get_inode(struct super_block *sb,
87 const struct inode *dir, int mode, dev_t dev)
88{
89 struct inode *inode = new_inode(sb);
90
91 if (inode) {
92 inode->i_ino = get_next_ino();
93 inode->i_uid = inode->i_gid = 0;
94 inode->i_mode = mode;
95 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
96 switch (mode & S_IFMT) {
97 case S_IFREG:
98 inode->i_fop = &pstore_file_operations;
99 break;
100 case S_IFDIR:
101 inode->i_op = &pstore_dir_inode_operations;
102 inode->i_fop = &simple_dir_operations;
103 inc_nlink(inode);
104 break;
105 }
106 }
107 return inode;
108}
109
Tony Luckca01d6d2010-12-28 14:25:21 -0800110static const struct super_operations pstore_ops = {
111 .statfs = simple_statfs,
112 .drop_inode = generic_delete_inode,
113 .show_options = generic_show_options,
114};
115
116static struct super_block *pstore_sb;
Tony Luckca01d6d2010-12-28 14:25:21 -0800117
118int pstore_is_mounted(void)
119{
Tony Luckfbe0aa12011-03-17 16:29:15 -0700120 return pstore_sb != NULL;
Tony Luckca01d6d2010-12-28 14:25:21 -0800121}
122
123/*
124 * Make a regular file in the root directory of our file system.
125 * Load it up with "size" bytes of data from "buf".
126 * Set the mtime & ctime to the date that this record was originally stored.
127 */
128int pstore_mkfile(enum pstore_type_id type, char *psname, u64 id,
129 char *data, size_t size,
130 struct timespec time, int (*erase)(u64))
131{
132 struct dentry *root = pstore_sb->s_root;
133 struct dentry *dentry;
134 struct inode *inode;
135 int rc;
136 char name[PSTORE_NAMELEN];
137 struct pstore_private *private;
138
139 rc = -ENOMEM;
140 inode = pstore_get_inode(pstore_sb, root->d_inode, S_IFREG | 0444, 0);
141 if (!inode)
142 goto fail;
Tony Luckfbe0aa12011-03-17 16:29:15 -0700143 private = kmalloc(sizeof *private + size, GFP_KERNEL);
Tony Luckca01d6d2010-12-28 14:25:21 -0800144 if (!private)
145 goto fail_alloc;
146 private->id = id;
147 private->erase = erase;
148
149 switch (type) {
150 case PSTORE_TYPE_DMESG:
151 sprintf(name, "dmesg-%s-%lld", psname, id);
152 break;
153 case PSTORE_TYPE_MCE:
154 sprintf(name, "mce-%s-%lld", psname, id);
155 break;
156 case PSTORE_TYPE_UNKNOWN:
157 sprintf(name, "unknown-%s-%lld", psname, id);
158 break;
159 default:
160 sprintf(name, "type%d-%s-%lld", type, psname, id);
161 break;
162 }
163
164 mutex_lock(&root->d_inode->i_mutex);
165
166 rc = -ENOSPC;
167 dentry = d_alloc_name(root, name);
168 if (IS_ERR(dentry))
169 goto fail_lockedalloc;
170
Tony Luckfbe0aa12011-03-17 16:29:15 -0700171 memcpy(private->data, data, size);
172 inode->i_size = private->size = size;
Tony Luckca01d6d2010-12-28 14:25:21 -0800173
174 inode->i_private = private;
175
176 if (time.tv_sec)
177 inode->i_mtime = inode->i_ctime = time;
178
Tony Luckfbe0aa12011-03-17 16:29:15 -0700179 d_add(dentry, inode);
Tony Luckca01d6d2010-12-28 14:25:21 -0800180
Tony Luckca01d6d2010-12-28 14:25:21 -0800181 mutex_unlock(&root->d_inode->i_mutex);
Tony Luckfbe0aa12011-03-17 16:29:15 -0700182
183 return 0;
Tony Luckca01d6d2010-12-28 14:25:21 -0800184
185fail_lockedalloc:
186 mutex_unlock(&root->d_inode->i_mutex);
187 kfree(private);
188fail_alloc:
189 iput(inode);
190
191fail:
192 return rc;
193}
194
195int pstore_fill_super(struct super_block *sb, void *data, int silent)
196{
197 struct inode *inode = NULL;
198 struct dentry *root;
199 int err;
200
201 save_mount_options(sb, data);
202
203 pstore_sb = sb;
204
205 sb->s_maxbytes = MAX_LFS_FILESIZE;
206 sb->s_blocksize = PAGE_CACHE_SIZE;
207 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
208 sb->s_magic = PSTOREFS_MAGIC;
209 sb->s_op = &pstore_ops;
210 sb->s_time_gran = 1;
211
212 inode = pstore_get_inode(sb, NULL, S_IFDIR | 0755, 0);
213 if (!inode) {
214 err = -ENOMEM;
215 goto fail;
216 }
217 /* override ramfs "dir" options so we catch unlink(2) */
218 inode->i_op = &pstore_dir_inode_operations;
219
220 root = d_alloc_root(inode);
221 sb->s_root = root;
222 if (!root) {
223 err = -ENOMEM;
224 goto fail;
225 }
226
227 pstore_get_records();
228
229 return 0;
230fail:
231 iput(inode);
232 return err;
233}
234
Tony Luckfbe0aa12011-03-17 16:29:15 -0700235static struct dentry *pstore_mount(struct file_system_type *fs_type,
236 int flags, const char *dev_name, void *data)
Tony Luckca01d6d2010-12-28 14:25:21 -0800237{
Tony Luckfbe0aa12011-03-17 16:29:15 -0700238 return mount_single(fs_type, flags, data, pstore_fill_super);
Tony Luckca01d6d2010-12-28 14:25:21 -0800239}
240
241static void pstore_kill_sb(struct super_block *sb)
242{
243 kill_litter_super(sb);
244 pstore_sb = NULL;
Tony Luckca01d6d2010-12-28 14:25:21 -0800245}
246
247static struct file_system_type pstore_fs_type = {
248 .name = "pstore",
Tony Luckfbe0aa12011-03-17 16:29:15 -0700249 .mount = pstore_mount,
Tony Luckca01d6d2010-12-28 14:25:21 -0800250 .kill_sb = pstore_kill_sb,
251};
252
253static int __init init_pstore_fs(void)
254{
Tony Luck168f2e12011-01-06 16:58:58 -0800255 int rc = 0;
Tony Luckca01d6d2010-12-28 14:25:21 -0800256 struct kobject *pstorefs_kobj;
257
258 pstorefs_kobj = kobject_create_and_add("pstore", fs_kobj);
Tony Luck168f2e12011-01-06 16:58:58 -0800259 if (!pstorefs_kobj) {
260 rc = -ENOMEM;
261 goto done;
Tony Luckca01d6d2010-12-28 14:25:21 -0800262 }
263
Tony Luck168f2e12011-01-06 16:58:58 -0800264 rc = sysfs_create_file(pstorefs_kobj, &pstore_kmsg_bytes_attr.attr);
265 if (rc)
266 goto done1;
267
268 rc = register_filesystem(&pstore_fs_type);
269 if (rc == 0)
270 goto done;
271
272 sysfs_remove_file(pstorefs_kobj, &pstore_kmsg_bytes_attr.attr);
273done1:
274 kobject_put(pstorefs_kobj);
275done:
276 return rc;
Tony Luckca01d6d2010-12-28 14:25:21 -0800277}
278module_init(init_pstore_fs)
279
280MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
281MODULE_LICENSE("GPL");