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/sched.h>
31 #include <linux/magic.h>
32 #include <linux/pstore.h>
33 #include <linux/slab.h>
34 #include <linux/uaccess.h>
38 #define PSTORE_NAMELEN 64
40 struct pstore_private
{
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
;
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
)
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
;
96 f
.f_op
= inode
->i_fop
;
98 n
= do_sync_write(&f
, data
, size
, &f
.f_pos
);
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
;
119 char name
[PSTORE_NAMELEN
];
120 struct pstore_private
*private;
123 inode
= pstore_get_inode(pstore_sb
, root
->d_inode
, S_IFREG
| 0444, 0);
126 inode
->i_uid
= inode
->i_gid
= 0;
127 private = kmalloc(sizeof *private, GFP_KERNEL
);
131 private->erase
= erase
;
134 case PSTORE_TYPE_DMESG
:
135 sprintf(name
, "dmesg-%s-%lld", psname
, id
);
137 case PSTORE_TYPE_MCE
:
138 sprintf(name
, "mce-%s-%lld", psname
, id
);
140 case PSTORE_TYPE_UNKNOWN
:
141 sprintf(name
, "unknown-%s-%lld", psname
, id
);
144 sprintf(name
, "type%d-%s-%lld", type
, psname
, id
);
148 mutex_lock(&root
->d_inode
->i_mutex
);
151 dentry
= d_alloc_name(root
, name
);
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
))
162 inode
->i_private
= private;
165 inode
->i_mtime
= inode
->i_ctime
= time
;
172 mutex_lock(&root
->d_inode
->i_mutex
);
175 mutex_unlock(&root
->d_inode
->i_mutex
);
179 mutex_unlock(&root
->d_inode
->i_mutex
);
188 int pstore_fill_super(struct super_block
*sb
, void *data
, int silent
)
190 struct inode
*inode
= NULL
;
194 save_mount_options(sb
, data
);
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
;
205 inode
= pstore_get_inode(sb
, NULL
, S_IFDIR
| 0755, 0);
210 /* override ramfs "dir" options so we catch unlink(2) */
211 inode
->i_op
= &pstore_dir_inode_operations
;
213 root
= d_alloc_root(inode
);
220 pstore_get_records();
228 static int pstore_get_sb(struct file_system_type
*fs_type
,
229 int flags
, const char *dev_name
, void *data
, struct vfsmount
*mnt
)
233 root
= mount_nodev(fs_type
, flags
, data
, pstore_fill_super
);
237 mnt
->mnt_root
= root
;
238 mnt
->mnt_sb
= root
->d_sb
;
244 static void pstore_kill_sb(struct super_block
*sb
)
246 kill_litter_super(sb
);
251 static struct file_system_type pstore_fs_type
= {
253 .get_sb
= pstore_get_sb
,
254 .kill_sb
= pstore_kill_sb
,
257 static int __init
init_pstore_fs(void)
260 struct kobject
*pstorefs_kobj
;
262 pstorefs_kobj
= kobject_create_and_add("pstore", fs_kobj
);
266 sysfs_create_file(pstorefs_kobj
, &pstore_kmsg_bytes_attr
.attr
);
268 ret
= register_filesystem(&pstore_fs_type
);
271 sysfs_remove_file(pstorefs_kobj
, &pstore_kmsg_bytes_attr
.attr
);
272 kobject_put(pstorefs_kobj
);
277 module_init(init_pstore_fs
)
279 MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>");
280 MODULE_LICENSE("GPL");