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>
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>
39 #define PSTORE_NAMELEN 64
41 struct pstore_private
{
48 static int pstore_file_open(struct inode
*inode
, struct file
*file
)
50 file
->private_data
= inode
->i_private
;
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
;
78 return simple_unlink(dir
, dentry
);
81 static void pstore_evict_inode(struct inode
*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
);
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
) {
104 inode
->i_fop
= &pstore_file_operations
;
107 inode
->i_op
= &pstore_dir_inode_operations
;
108 inode
->i_fop
= &simple_dir_operations
;
117 Opt_kmsg_bytes
, Opt_err
120 static const match_table_t tokens
= {
121 {Opt_kmsg_bytes
, "kmsg_bytes=%u"},
125 static void parse_options(char *options
)
128 substring_t args
[MAX_OPT_ARGS
];
134 while ((p
= strsep(&options
, ",")) != NULL
) {
140 token
= match_token(p
, tokens
, args
);
143 if (!match_int(&args
[0], &option
))
144 pstore_set_kmsg_bytes(option
);
150 static int pstore_remount(struct super_block
*sb
, int *flags
, char *data
)
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
;
185 char name
[PSTORE_NAMELEN
];
186 struct pstore_private
*private;
189 inode
= pstore_get_inode(pstore_sb
, root
->d_inode
, S_IFREG
| 0444, 0);
192 private = kmalloc(sizeof *private + size
, GFP_KERNEL
);
196 private->erase
= erase
;
199 case PSTORE_TYPE_DMESG
:
200 sprintf(name
, "dmesg-%s-%lld", psname
, id
);
202 case PSTORE_TYPE_MCE
:
203 sprintf(name
, "mce-%s-%lld", psname
, id
);
205 case PSTORE_TYPE_UNKNOWN
:
206 sprintf(name
, "unknown-%s-%lld", psname
, id
);
209 sprintf(name
, "type%d-%s-%lld", type
, psname
, id
);
213 mutex_lock(&root
->d_inode
->i_mutex
);
216 dentry
= d_alloc_name(root
, name
);
218 goto fail_lockedalloc
;
220 memcpy(private->data
, data
, size
);
221 inode
->i_size
= private->size
= size
;
223 inode
->i_private
= private;
226 inode
->i_mtime
= inode
->i_ctime
= time
;
228 d_add(dentry
, inode
);
230 mutex_unlock(&root
->d_inode
->i_mutex
);
235 mutex_unlock(&root
->d_inode
->i_mutex
);
244 int pstore_fill_super(struct super_block
*sb
, void *data
, int silent
)
246 struct inode
*inode
= NULL
;
250 save_mount_options(sb
, data
);
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
;
263 inode
= pstore_get_inode(sb
, NULL
, S_IFDIR
| 0755, 0);
268 /* override ramfs "dir" options so we catch unlink(2) */
269 inode
->i_op
= &pstore_dir_inode_operations
;
271 root
= d_alloc_root(inode
);
278 pstore_get_records();
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
);
298 static struct file_system_type pstore_fs_type
= {
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");