kvm tools, setup: Create private directory
[linux-2.6/next.git] / fs / pstore / inode.c
blob977ed272384574e0220bdfe06439ea3cdd045ad5
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/parser.h>
31 #include <linux/sched.h>
32 #include <linux/magic.h>
33 #include <linux/pstore.h>
34 #include <linux/slab.h>
35 #include <linux/uaccess.h>
37 #include "internal.h"
39 #define PSTORE_NAMELEN 64
41 struct pstore_private {
42 u64 id;
43 int (*erase)(u64);
44 ssize_t size;
45 char data[];
48 static int pstore_file_open(struct inode *inode, struct file *file)
50 file->private_data = inode->i_private;
51 return 0;
54 static ssize_t pstore_file_read(struct file *file, char __user *userbuf,
55 size_t count, loff_t *ppos)
57 struct pstore_private *ps = file->private_data;
59 return simple_read_from_buffer(userbuf, count, ppos, ps->data, ps->size);
62 static const struct file_operations pstore_file_operations = {
63 .open = pstore_file_open,
64 .read = pstore_file_read,
65 .llseek = default_llseek,
69 * When a file is unlinked from our file system we call the
70 * platform driver to erase the record from persistent store.
72 static int pstore_unlink(struct inode *dir, struct dentry *dentry)
74 struct pstore_private *p = dentry->d_inode->i_private;
76 p->erase(p->id);
78 return simple_unlink(dir, dentry);
81 static void pstore_evict_inode(struct inode *inode)
83 end_writeback(inode);
84 kfree(inode->i_private);
87 static const struct inode_operations pstore_dir_inode_operations = {
88 .lookup = simple_lookup,
89 .unlink = pstore_unlink,
92 static struct inode *pstore_get_inode(struct super_block *sb,
93 const struct inode *dir, int mode, dev_t dev)
95 struct inode *inode = new_inode(sb);
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;
113 return inode;
116 enum {
117 Opt_kmsg_bytes, Opt_err
120 static const match_table_t tokens = {
121 {Opt_kmsg_bytes, "kmsg_bytes=%u"},
122 {Opt_err, NULL}
125 static void parse_options(char *options)
127 char *p;
128 substring_t args[MAX_OPT_ARGS];
129 int option;
131 if (!options)
132 return;
134 while ((p = strsep(&options, ",")) != NULL) {
135 int token;
137 if (!*p)
138 continue;
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;
150 static int pstore_remount(struct super_block *sb, int *flags, char *data)
152 parse_options(data);
154 return 0;
157 static const struct super_operations pstore_ops = {
158 .statfs = simple_statfs,
159 .drop_inode = generic_delete_inode,
160 .evict_inode = pstore_evict_inode,
161 .remount_fs = pstore_remount,
162 .show_options = generic_show_options,
165 static struct super_block *pstore_sb;
167 int pstore_is_mounted(void)
169 return pstore_sb != NULL;
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.
177 int pstore_mkfile(enum pstore_type_id type, char *psname, u64 id,
178 char *data, size_t size,
179 struct timespec time, int (*erase)(u64))
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;
188 rc = -ENOMEM;
189 inode = pstore_get_inode(pstore_sb, root->d_inode, S_IFREG | 0444, 0);
190 if (!inode)
191 goto fail;
192 private = kmalloc(sizeof *private + size, GFP_KERNEL);
193 if (!private)
194 goto fail_alloc;
195 private->id = id;
196 private->erase = erase;
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;
213 mutex_lock(&root->d_inode->i_mutex);
215 rc = -ENOSPC;
216 dentry = d_alloc_name(root, name);
217 if (IS_ERR(dentry))
218 goto fail_lockedalloc;
220 memcpy(private->data, data, size);
221 inode->i_size = private->size = size;
223 inode->i_private = private;
225 if (time.tv_sec)
226 inode->i_mtime = inode->i_ctime = time;
228 d_add(dentry, inode);
230 mutex_unlock(&root->d_inode->i_mutex);
232 return 0;
234 fail_lockedalloc:
235 mutex_unlock(&root->d_inode->i_mutex);
236 kfree(private);
237 fail_alloc:
238 iput(inode);
240 fail:
241 return rc;
244 int pstore_fill_super(struct super_block *sb, void *data, int silent)
246 struct inode *inode = NULL;
247 struct dentry *root;
248 int err;
250 save_mount_options(sb, data);
252 pstore_sb = sb;
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;
261 parse_options(data);
263 inode = pstore_get_inode(sb, NULL, S_IFDIR | 0755, 0);
264 if (!inode) {
265 err = -ENOMEM;
266 goto fail;
268 /* override ramfs "dir" options so we catch unlink(2) */
269 inode->i_op = &pstore_dir_inode_operations;
271 root = d_alloc_root(inode);
272 sb->s_root = root;
273 if (!root) {
274 err = -ENOMEM;
275 goto fail;
278 pstore_get_records();
280 return 0;
281 fail:
282 iput(inode);
283 return err;
286 static struct dentry *pstore_mount(struct file_system_type *fs_type,
287 int flags, const char *dev_name, void *data)
289 return mount_single(fs_type, flags, data, pstore_fill_super);
292 static void pstore_kill_sb(struct super_block *sb)
294 kill_litter_super(sb);
295 pstore_sb = NULL;
298 static struct file_system_type pstore_fs_type = {
299 .name = "pstore",
300 .mount = pstore_mount,
301 .kill_sb = pstore_kill_sb,
304 static int __init init_pstore_fs(void)
306 return register_filesystem(&pstore_fs_type);
308 module_init(init_pstore_fs)
310 MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
311 MODULE_LICENSE("GPL");