pstore: new filesystem interface to platform persistent storage
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / pstore / inode.c
blob0e806aafe8577d9c6b5492773d48d71572990b97
1 /*
2 * Persistent Storage - ramfs parts.
4 * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
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.
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.
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
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>
36 #include "internal.h"
38 #define PSTORE_NAMELEN 64
40 struct pstore_private {
41 u64 id;
42 int (*erase)(u64);
45 #define pstore_get_inode ramfs_get_inode
48 * When a file is unlinked from our file system we call the
49 * platform driver to erase the record from persistent store.
51 static int pstore_unlink(struct inode *dir, struct dentry *dentry)
53 struct pstore_private *p = dentry->d_inode->i_private;
55 p->erase(p->id);
56 kfree(p);
58 return simple_unlink(dir, dentry);
61 static const struct inode_operations pstore_dir_inode_operations = {
62 .lookup = simple_lookup,
63 .unlink = pstore_unlink,
66 static const struct super_operations pstore_ops = {
67 .statfs = simple_statfs,
68 .drop_inode = generic_delete_inode,
69 .show_options = generic_show_options,
72 static struct super_block *pstore_sb;
73 static struct vfsmount *pstore_mnt;
75 int pstore_is_mounted(void)
77 return pstore_mnt != NULL;
81 * Set up a file structure as if we had opened this file and
82 * write our data to it.
84 static int pstore_writefile(struct inode *inode, struct dentry *dentry,
85 char *data, size_t size)
87 struct file f;
88 ssize_t n;
89 mm_segment_t old_fs = get_fs();
91 memset(&f, '0', sizeof f);
92 f.f_mapping = inode->i_mapping;
93 f.f_path.dentry = dentry;
94 f.f_path.mnt = pstore_mnt;
95 f.f_pos = 0;
96 f.f_op = inode->i_fop;
97 set_fs(KERNEL_DS);
98 n = do_sync_write(&f, data, size, &f.f_pos);
99 set_fs(old_fs);
101 fsnotify_modify(&f);
103 return n == size;
107 * Make a regular file in the root directory of our file system.
108 * Load it up with "size" bytes of data from "buf".
109 * Set the mtime & ctime to the date that this record was originally stored.
111 int pstore_mkfile(enum pstore_type_id type, char *psname, u64 id,
112 char *data, size_t size,
113 struct timespec time, int (*erase)(u64))
115 struct dentry *root = pstore_sb->s_root;
116 struct dentry *dentry;
117 struct inode *inode;
118 int rc;
119 char name[PSTORE_NAMELEN];
120 struct pstore_private *private;
122 rc = -ENOMEM;
123 inode = pstore_get_inode(pstore_sb, root->d_inode, S_IFREG | 0444, 0);
124 if (!inode)
125 goto fail;
126 inode->i_uid = inode->i_gid = 0;
127 private = kmalloc(sizeof *private, GFP_KERNEL);
128 if (!private)
129 goto fail_alloc;
130 private->id = id;
131 private->erase = erase;
133 switch (type) {
134 case PSTORE_TYPE_DMESG:
135 sprintf(name, "dmesg-%s-%lld", psname, id);
136 break;
137 case PSTORE_TYPE_MCE:
138 sprintf(name, "mce-%s-%lld", psname, id);
139 break;
140 case PSTORE_TYPE_UNKNOWN:
141 sprintf(name, "unknown-%s-%lld", psname, id);
142 break;
143 default:
144 sprintf(name, "type%d-%s-%lld", type, psname, id);
145 break;
148 mutex_lock(&root->d_inode->i_mutex);
150 rc = -ENOSPC;
151 dentry = d_alloc_name(root, name);
152 if (IS_ERR(dentry))
153 goto fail_lockedalloc;
155 d_add(dentry, inode);
157 mutex_unlock(&root->d_inode->i_mutex);
159 if (!pstore_writefile(inode, dentry, data, size))
160 goto fail_write;
162 inode->i_private = private;
164 if (time.tv_sec)
165 inode->i_mtime = inode->i_ctime = time;
167 return 0;
169 fail_write:
170 kfree(private);
171 inode->i_nlink--;
172 mutex_lock(&root->d_inode->i_mutex);
173 d_delete(dentry);
174 dput(dentry);
175 mutex_unlock(&root->d_inode->i_mutex);
176 goto fail;
178 fail_lockedalloc:
179 mutex_unlock(&root->d_inode->i_mutex);
180 kfree(private);
181 fail_alloc:
182 iput(inode);
184 fail:
185 return rc;
188 int pstore_fill_super(struct super_block *sb, void *data, int silent)
190 struct inode *inode = NULL;
191 struct dentry *root;
192 int err;
194 save_mount_options(sb, data);
196 pstore_sb = sb;
198 sb->s_maxbytes = MAX_LFS_FILESIZE;
199 sb->s_blocksize = PAGE_CACHE_SIZE;
200 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
201 sb->s_magic = PSTOREFS_MAGIC;
202 sb->s_op = &pstore_ops;
203 sb->s_time_gran = 1;
205 inode = pstore_get_inode(sb, NULL, S_IFDIR | 0755, 0);
206 if (!inode) {
207 err = -ENOMEM;
208 goto fail;
210 /* override ramfs "dir" options so we catch unlink(2) */
211 inode->i_op = &pstore_dir_inode_operations;
213 root = d_alloc_root(inode);
214 sb->s_root = root;
215 if (!root) {
216 err = -ENOMEM;
217 goto fail;
220 pstore_get_records();
222 return 0;
223 fail:
224 iput(inode);
225 return err;
228 static int pstore_get_sb(struct file_system_type *fs_type,
229 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
231 struct dentry *root;
233 root = mount_nodev(fs_type, flags, data, pstore_fill_super);
234 if (IS_ERR(root))
235 return -ENOMEM;
237 mnt->mnt_root = root;
238 mnt->mnt_sb = root->d_sb;
239 pstore_mnt = mnt;
241 return 0;
244 static void pstore_kill_sb(struct super_block *sb)
246 kill_litter_super(sb);
247 pstore_sb = NULL;
248 pstore_mnt = NULL;
251 static struct file_system_type pstore_fs_type = {
252 .name = "pstore",
253 .get_sb = pstore_get_sb,
254 .kill_sb = pstore_kill_sb,
257 static int __init init_pstore_fs(void)
259 int ret = 0;
260 struct kobject *pstorefs_kobj;
262 pstorefs_kobj = kobject_create_and_add("pstore", fs_kobj);
263 if (!pstorefs_kobj)
264 return -ENOMEM;
266 sysfs_create_file(pstorefs_kobj, &pstore_kmsg_bytes_attr.attr);
268 ret = register_filesystem(&pstore_fs_type);
270 if (ret) {
271 sysfs_remove_file(pstorefs_kobj, &pstore_kmsg_bytes_attr.attr);
272 kobject_put(pstorefs_kobj);
275 return ret;
277 module_init(init_pstore_fs)
279 MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
280 MODULE_LICENSE("GPL");