| Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 1 | /* | 
 | 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> | 
| Luck, Tony | 366f7e7 | 2011-03-18 15:33:43 -0700 | [diff] [blame] | 30 | #include <linux/parser.h> | 
| Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 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> | 
 | 36 |  | 
 | 37 | #include "internal.h" | 
 | 38 |  | 
 | 39 | #define	PSTORE_NAMELEN	64 | 
 | 40 |  | 
 | 41 | struct pstore_private { | 
 | 42 | 	u64	id; | 
 | 43 | 	int	(*erase)(u64); | 
| Tony Luck | fbe0aa1 | 2011-03-17 16:29:15 -0700 | [diff] [blame] | 44 | 	ssize_t	size; | 
 | 45 | 	char	data[]; | 
| Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 46 | }; | 
 | 47 |  | 
| Tony Luck | fbe0aa1 | 2011-03-17 16:29:15 -0700 | [diff] [blame] | 48 | static int pstore_file_open(struct inode *inode, struct file *file) | 
 | 49 | { | 
 | 50 | 	file->private_data = inode->i_private; | 
 | 51 | 	return 0; | 
 | 52 | } | 
 | 53 |  | 
 | 54 | static ssize_t pstore_file_read(struct file *file, char __user *userbuf, | 
 | 55 | 						size_t count, loff_t *ppos) | 
 | 56 | { | 
 | 57 | 	struct pstore_private *ps = file->private_data; | 
 | 58 |  | 
 | 59 | 	return simple_read_from_buffer(userbuf, count, ppos, ps->data, ps->size); | 
 | 60 | } | 
 | 61 |  | 
 | 62 | static const struct file_operations pstore_file_operations = { | 
 | 63 | 	.open	= pstore_file_open, | 
 | 64 | 	.read	= pstore_file_read, | 
 | 65 | 	.llseek	= default_llseek, | 
 | 66 | }; | 
| Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 67 |  | 
 | 68 | /* | 
 | 69 |  * When a file is unlinked from our file system we call the | 
 | 70 |  * platform driver to erase the record from persistent store. | 
 | 71 |  */ | 
 | 72 | static int pstore_unlink(struct inode *dir, struct dentry *dentry) | 
 | 73 | { | 
 | 74 | 	struct pstore_private *p = dentry->d_inode->i_private; | 
 | 75 |  | 
 | 76 | 	p->erase(p->id); | 
| Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 77 |  | 
 | 78 | 	return simple_unlink(dir, dentry); | 
 | 79 | } | 
 | 80 |  | 
| Tony Luck | a872d51 | 2011-03-18 11:44:48 -0700 | [diff] [blame] | 81 | static void pstore_evict_inode(struct inode *inode) | 
 | 82 | { | 
 | 83 | 	end_writeback(inode); | 
 | 84 | 	kfree(inode->i_private); | 
 | 85 | } | 
 | 86 |  | 
| Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 87 | static const struct inode_operations pstore_dir_inode_operations = { | 
 | 88 | 	.lookup		= simple_lookup, | 
 | 89 | 	.unlink		= pstore_unlink, | 
 | 90 | }; | 
 | 91 |  | 
| Tony Luck | fbe0aa1 | 2011-03-17 16:29:15 -0700 | [diff] [blame] | 92 | static struct inode *pstore_get_inode(struct super_block *sb, | 
 | 93 | 					const struct inode *dir, int mode, dev_t dev) | 
 | 94 | { | 
 | 95 | 	struct inode *inode = new_inode(sb); | 
 | 96 |  | 
 | 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; | 
 | 111 | 		} | 
 | 112 | 	} | 
 | 113 | 	return inode; | 
 | 114 | } | 
 | 115 |  | 
| Luck, Tony | 366f7e7 | 2011-03-18 15:33:43 -0700 | [diff] [blame] | 116 | enum { | 
 | 117 | 	Opt_kmsg_bytes, Opt_err | 
 | 118 | }; | 
 | 119 |  | 
 | 120 | static const match_table_t tokens = { | 
 | 121 | 	{Opt_kmsg_bytes, "kmsg_bytes=%u"}, | 
 | 122 | 	{Opt_err, NULL} | 
 | 123 | }; | 
 | 124 |  | 
 | 125 | static void parse_options(char *options) | 
 | 126 | { | 
 | 127 | 	char		*p; | 
 | 128 | 	substring_t	args[MAX_OPT_ARGS]; | 
 | 129 | 	int		option; | 
 | 130 |  | 
 | 131 | 	if (!options) | 
 | 132 | 		return; | 
 | 133 |  | 
 | 134 | 	while ((p = strsep(&options, ",")) != NULL) { | 
 | 135 | 		int token; | 
 | 136 |  | 
 | 137 | 		if (!*p) | 
 | 138 | 			continue; | 
 | 139 |  | 
 | 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; | 
 | 146 | 		} | 
 | 147 | 	} | 
 | 148 | } | 
 | 149 |  | 
 | 150 | static int pstore_remount(struct super_block *sb, int *flags, char *data) | 
 | 151 | { | 
 | 152 | 	parse_options(data); | 
 | 153 |  | 
 | 154 | 	return 0; | 
 | 155 | } | 
 | 156 |  | 
| Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 157 | static const struct super_operations pstore_ops = { | 
 | 158 | 	.statfs		= simple_statfs, | 
 | 159 | 	.drop_inode	= generic_delete_inode, | 
| Tony Luck | a872d51 | 2011-03-18 11:44:48 -0700 | [diff] [blame] | 160 | 	.evict_inode	= pstore_evict_inode, | 
| Luck, Tony | 366f7e7 | 2011-03-18 15:33:43 -0700 | [diff] [blame] | 161 | 	.remount_fs	= pstore_remount, | 
| Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 162 | 	.show_options	= generic_show_options, | 
 | 163 | }; | 
 | 164 |  | 
 | 165 | static struct super_block *pstore_sb; | 
| Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 166 |  | 
 | 167 | int pstore_is_mounted(void) | 
 | 168 | { | 
| Tony Luck | fbe0aa1 | 2011-03-17 16:29:15 -0700 | [diff] [blame] | 169 | 	return pstore_sb != NULL; | 
| Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 170 | } | 
 | 171 |  | 
 | 172 | /* | 
 | 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. | 
 | 176 |  */ | 
 | 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)) | 
 | 180 | { | 
 | 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; | 
 | 187 |  | 
 | 188 | 	rc = -ENOMEM; | 
 | 189 | 	inode = pstore_get_inode(pstore_sb, root->d_inode, S_IFREG | 0444, 0); | 
 | 190 | 	if (!inode) | 
 | 191 | 		goto fail; | 
| Tony Luck | fbe0aa1 | 2011-03-17 16:29:15 -0700 | [diff] [blame] | 192 | 	private = kmalloc(sizeof *private + size, GFP_KERNEL); | 
| Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 193 | 	if (!private) | 
 | 194 | 		goto fail_alloc; | 
 | 195 | 	private->id = id; | 
 | 196 | 	private->erase = erase; | 
 | 197 |  | 
 | 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; | 
 | 211 | 	} | 
 | 212 |  | 
 | 213 | 	mutex_lock(&root->d_inode->i_mutex); | 
 | 214 |  | 
 | 215 | 	rc = -ENOSPC; | 
 | 216 | 	dentry = d_alloc_name(root, name); | 
 | 217 | 	if (IS_ERR(dentry)) | 
 | 218 | 		goto fail_lockedalloc; | 
 | 219 |  | 
| Tony Luck | fbe0aa1 | 2011-03-17 16:29:15 -0700 | [diff] [blame] | 220 | 	memcpy(private->data, data, size); | 
 | 221 | 	inode->i_size = private->size = size; | 
| Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 222 |  | 
 | 223 | 	inode->i_private = private; | 
 | 224 |  | 
 | 225 | 	if (time.tv_sec) | 
 | 226 | 		inode->i_mtime = inode->i_ctime = time; | 
 | 227 |  | 
| Tony Luck | fbe0aa1 | 2011-03-17 16:29:15 -0700 | [diff] [blame] | 228 | 	d_add(dentry, inode); | 
| Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 229 |  | 
| Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 230 | 	mutex_unlock(&root->d_inode->i_mutex); | 
| Tony Luck | fbe0aa1 | 2011-03-17 16:29:15 -0700 | [diff] [blame] | 231 |  | 
 | 232 | 	return 0; | 
| Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 233 |  | 
 | 234 | fail_lockedalloc: | 
 | 235 | 	mutex_unlock(&root->d_inode->i_mutex); | 
 | 236 | 	kfree(private); | 
 | 237 | fail_alloc: | 
 | 238 | 	iput(inode); | 
 | 239 |  | 
 | 240 | fail: | 
 | 241 | 	return rc; | 
 | 242 | } | 
 | 243 |  | 
 | 244 | int pstore_fill_super(struct super_block *sb, void *data, int silent) | 
 | 245 | { | 
 | 246 | 	struct inode *inode = NULL; | 
 | 247 | 	struct dentry *root; | 
 | 248 | 	int err; | 
 | 249 |  | 
 | 250 | 	save_mount_options(sb, data); | 
 | 251 |  | 
 | 252 | 	pstore_sb = sb; | 
 | 253 |  | 
 | 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; | 
 | 260 |  | 
| Luck, Tony | 366f7e7 | 2011-03-18 15:33:43 -0700 | [diff] [blame] | 261 | 	parse_options(data); | 
 | 262 |  | 
| Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 263 | 	inode = pstore_get_inode(sb, NULL, S_IFDIR | 0755, 0); | 
 | 264 | 	if (!inode) { | 
 | 265 | 		err = -ENOMEM; | 
 | 266 | 		goto fail; | 
 | 267 | 	} | 
 | 268 | 	/* override ramfs "dir" options so we catch unlink(2) */ | 
 | 269 | 	inode->i_op = &pstore_dir_inode_operations; | 
 | 270 |  | 
 | 271 | 	root = d_alloc_root(inode); | 
 | 272 | 	sb->s_root = root; | 
 | 273 | 	if (!root) { | 
 | 274 | 		err = -ENOMEM; | 
 | 275 | 		goto fail; | 
 | 276 | 	} | 
 | 277 |  | 
 | 278 | 	pstore_get_records(); | 
 | 279 |  | 
 | 280 | 	return 0; | 
 | 281 | fail: | 
 | 282 | 	iput(inode); | 
 | 283 | 	return err; | 
 | 284 | } | 
 | 285 |  | 
| Tony Luck | fbe0aa1 | 2011-03-17 16:29:15 -0700 | [diff] [blame] | 286 | static struct dentry *pstore_mount(struct file_system_type *fs_type, | 
 | 287 | 	int flags, const char *dev_name, void *data) | 
| Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 288 | { | 
| Tony Luck | fbe0aa1 | 2011-03-17 16:29:15 -0700 | [diff] [blame] | 289 | 	return mount_single(fs_type, flags, data, pstore_fill_super); | 
| Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 290 | } | 
 | 291 |  | 
 | 292 | static void pstore_kill_sb(struct super_block *sb) | 
 | 293 | { | 
 | 294 | 	kill_litter_super(sb); | 
 | 295 | 	pstore_sb = NULL; | 
| Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 296 | } | 
 | 297 |  | 
 | 298 | static struct file_system_type pstore_fs_type = { | 
 | 299 | 	.name		= "pstore", | 
| Tony Luck | fbe0aa1 | 2011-03-17 16:29:15 -0700 | [diff] [blame] | 300 | 	.mount		= pstore_mount, | 
| Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 301 | 	.kill_sb	= pstore_kill_sb, | 
 | 302 | }; | 
 | 303 |  | 
 | 304 | static int __init init_pstore_fs(void) | 
 | 305 | { | 
| Luck, Tony | 366f7e7 | 2011-03-18 15:33:43 -0700 | [diff] [blame] | 306 | 	return register_filesystem(&pstore_fs_type); | 
| Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 307 | } | 
 | 308 | module_init(init_pstore_fs) | 
 | 309 |  | 
 | 310 | MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>"); | 
 | 311 | MODULE_LICENSE("GPL"); |