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> |
| 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> |
| 35 | |
| 36 | #include "internal.h" |
| 37 | |
| 38 | #define PSTORE_NAMELEN 64 |
| 39 | |
| 40 | struct pstore_private { |
| 41 | u64 id; |
| 42 | int (*erase)(u64); |
Tony Luck | fbe0aa1 | 2011-03-17 16:29:15 -0700 | [diff] [blame] | 43 | ssize_t size; |
| 44 | char data[]; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 45 | }; |
| 46 | |
Tony Luck | fbe0aa1 | 2011-03-17 16:29:15 -0700 | [diff] [blame] | 47 | static int pstore_file_open(struct inode *inode, struct file *file) |
| 48 | { |
| 49 | file->private_data = inode->i_private; |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | static ssize_t pstore_file_read(struct file *file, char __user *userbuf, |
| 54 | size_t count, loff_t *ppos) |
| 55 | { |
| 56 | struct pstore_private *ps = file->private_data; |
| 57 | |
| 58 | return simple_read_from_buffer(userbuf, count, ppos, ps->data, ps->size); |
| 59 | } |
| 60 | |
| 61 | static const struct file_operations pstore_file_operations = { |
| 62 | .open = pstore_file_open, |
| 63 | .read = pstore_file_read, |
| 64 | .llseek = default_llseek, |
| 65 | }; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 66 | |
| 67 | /* |
| 68 | * When a file is unlinked from our file system we call the |
| 69 | * platform driver to erase the record from persistent store. |
| 70 | */ |
| 71 | static int pstore_unlink(struct inode *dir, struct dentry *dentry) |
| 72 | { |
| 73 | struct pstore_private *p = dentry->d_inode->i_private; |
| 74 | |
| 75 | p->erase(p->id); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 76 | |
| 77 | return simple_unlink(dir, dentry); |
| 78 | } |
| 79 | |
Tony Luck | a872d51 | 2011-03-18 11:44:48 -0700 | [diff] [blame^] | 80 | static void pstore_evict_inode(struct inode *inode) |
| 81 | { |
| 82 | end_writeback(inode); |
| 83 | kfree(inode->i_private); |
| 84 | } |
| 85 | |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 86 | static const struct inode_operations pstore_dir_inode_operations = { |
| 87 | .lookup = simple_lookup, |
| 88 | .unlink = pstore_unlink, |
| 89 | }; |
| 90 | |
Tony Luck | fbe0aa1 | 2011-03-17 16:29:15 -0700 | [diff] [blame] | 91 | static struct inode *pstore_get_inode(struct super_block *sb, |
| 92 | const struct inode *dir, int mode, dev_t dev) |
| 93 | { |
| 94 | struct inode *inode = new_inode(sb); |
| 95 | |
| 96 | if (inode) { |
| 97 | inode->i_ino = get_next_ino(); |
| 98 | inode->i_uid = inode->i_gid = 0; |
| 99 | inode->i_mode = mode; |
| 100 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 101 | switch (mode & S_IFMT) { |
| 102 | case S_IFREG: |
| 103 | inode->i_fop = &pstore_file_operations; |
| 104 | break; |
| 105 | case S_IFDIR: |
| 106 | inode->i_op = &pstore_dir_inode_operations; |
| 107 | inode->i_fop = &simple_dir_operations; |
| 108 | inc_nlink(inode); |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | return inode; |
| 113 | } |
| 114 | |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 115 | static const struct super_operations pstore_ops = { |
| 116 | .statfs = simple_statfs, |
| 117 | .drop_inode = generic_delete_inode, |
Tony Luck | a872d51 | 2011-03-18 11:44:48 -0700 | [diff] [blame^] | 118 | .evict_inode = pstore_evict_inode, |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 119 | .show_options = generic_show_options, |
| 120 | }; |
| 121 | |
| 122 | static struct super_block *pstore_sb; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 123 | |
| 124 | int pstore_is_mounted(void) |
| 125 | { |
Tony Luck | fbe0aa1 | 2011-03-17 16:29:15 -0700 | [diff] [blame] | 126 | return pstore_sb != NULL; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Make a regular file in the root directory of our file system. |
| 131 | * Load it up with "size" bytes of data from "buf". |
| 132 | * Set the mtime & ctime to the date that this record was originally stored. |
| 133 | */ |
| 134 | int pstore_mkfile(enum pstore_type_id type, char *psname, u64 id, |
| 135 | char *data, size_t size, |
| 136 | struct timespec time, int (*erase)(u64)) |
| 137 | { |
| 138 | struct dentry *root = pstore_sb->s_root; |
| 139 | struct dentry *dentry; |
| 140 | struct inode *inode; |
| 141 | int rc; |
| 142 | char name[PSTORE_NAMELEN]; |
| 143 | struct pstore_private *private; |
| 144 | |
| 145 | rc = -ENOMEM; |
| 146 | inode = pstore_get_inode(pstore_sb, root->d_inode, S_IFREG | 0444, 0); |
| 147 | if (!inode) |
| 148 | goto fail; |
Tony Luck | fbe0aa1 | 2011-03-17 16:29:15 -0700 | [diff] [blame] | 149 | private = kmalloc(sizeof *private + size, GFP_KERNEL); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 150 | if (!private) |
| 151 | goto fail_alloc; |
| 152 | private->id = id; |
| 153 | private->erase = erase; |
| 154 | |
| 155 | switch (type) { |
| 156 | case PSTORE_TYPE_DMESG: |
| 157 | sprintf(name, "dmesg-%s-%lld", psname, id); |
| 158 | break; |
| 159 | case PSTORE_TYPE_MCE: |
| 160 | sprintf(name, "mce-%s-%lld", psname, id); |
| 161 | break; |
| 162 | case PSTORE_TYPE_UNKNOWN: |
| 163 | sprintf(name, "unknown-%s-%lld", psname, id); |
| 164 | break; |
| 165 | default: |
| 166 | sprintf(name, "type%d-%s-%lld", type, psname, id); |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | mutex_lock(&root->d_inode->i_mutex); |
| 171 | |
| 172 | rc = -ENOSPC; |
| 173 | dentry = d_alloc_name(root, name); |
| 174 | if (IS_ERR(dentry)) |
| 175 | goto fail_lockedalloc; |
| 176 | |
Tony Luck | fbe0aa1 | 2011-03-17 16:29:15 -0700 | [diff] [blame] | 177 | memcpy(private->data, data, size); |
| 178 | inode->i_size = private->size = size; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 179 | |
| 180 | inode->i_private = private; |
| 181 | |
| 182 | if (time.tv_sec) |
| 183 | inode->i_mtime = inode->i_ctime = time; |
| 184 | |
Tony Luck | fbe0aa1 | 2011-03-17 16:29:15 -0700 | [diff] [blame] | 185 | d_add(dentry, inode); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 186 | |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 187 | mutex_unlock(&root->d_inode->i_mutex); |
Tony Luck | fbe0aa1 | 2011-03-17 16:29:15 -0700 | [diff] [blame] | 188 | |
| 189 | return 0; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 190 | |
| 191 | fail_lockedalloc: |
| 192 | mutex_unlock(&root->d_inode->i_mutex); |
| 193 | kfree(private); |
| 194 | fail_alloc: |
| 195 | iput(inode); |
| 196 | |
| 197 | fail: |
| 198 | return rc; |
| 199 | } |
| 200 | |
| 201 | int pstore_fill_super(struct super_block *sb, void *data, int silent) |
| 202 | { |
| 203 | struct inode *inode = NULL; |
| 204 | struct dentry *root; |
| 205 | int err; |
| 206 | |
| 207 | save_mount_options(sb, data); |
| 208 | |
| 209 | pstore_sb = sb; |
| 210 | |
| 211 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
| 212 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 213 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 214 | sb->s_magic = PSTOREFS_MAGIC; |
| 215 | sb->s_op = &pstore_ops; |
| 216 | sb->s_time_gran = 1; |
| 217 | |
| 218 | inode = pstore_get_inode(sb, NULL, S_IFDIR | 0755, 0); |
| 219 | if (!inode) { |
| 220 | err = -ENOMEM; |
| 221 | goto fail; |
| 222 | } |
| 223 | /* override ramfs "dir" options so we catch unlink(2) */ |
| 224 | inode->i_op = &pstore_dir_inode_operations; |
| 225 | |
| 226 | root = d_alloc_root(inode); |
| 227 | sb->s_root = root; |
| 228 | if (!root) { |
| 229 | err = -ENOMEM; |
| 230 | goto fail; |
| 231 | } |
| 232 | |
| 233 | pstore_get_records(); |
| 234 | |
| 235 | return 0; |
| 236 | fail: |
| 237 | iput(inode); |
| 238 | return err; |
| 239 | } |
| 240 | |
Tony Luck | fbe0aa1 | 2011-03-17 16:29:15 -0700 | [diff] [blame] | 241 | static struct dentry *pstore_mount(struct file_system_type *fs_type, |
| 242 | int flags, const char *dev_name, void *data) |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 243 | { |
Tony Luck | fbe0aa1 | 2011-03-17 16:29:15 -0700 | [diff] [blame] | 244 | return mount_single(fs_type, flags, data, pstore_fill_super); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | static void pstore_kill_sb(struct super_block *sb) |
| 248 | { |
| 249 | kill_litter_super(sb); |
| 250 | pstore_sb = NULL; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | static struct file_system_type pstore_fs_type = { |
| 254 | .name = "pstore", |
Tony Luck | fbe0aa1 | 2011-03-17 16:29:15 -0700 | [diff] [blame] | 255 | .mount = pstore_mount, |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 256 | .kill_sb = pstore_kill_sb, |
| 257 | }; |
| 258 | |
| 259 | static int __init init_pstore_fs(void) |
| 260 | { |
Tony Luck | 168f2e1 | 2011-01-06 16:58:58 -0800 | [diff] [blame] | 261 | int rc = 0; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 262 | struct kobject *pstorefs_kobj; |
| 263 | |
| 264 | pstorefs_kobj = kobject_create_and_add("pstore", fs_kobj); |
Tony Luck | 168f2e1 | 2011-01-06 16:58:58 -0800 | [diff] [blame] | 265 | if (!pstorefs_kobj) { |
| 266 | rc = -ENOMEM; |
| 267 | goto done; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Tony Luck | 168f2e1 | 2011-01-06 16:58:58 -0800 | [diff] [blame] | 270 | rc = sysfs_create_file(pstorefs_kobj, &pstore_kmsg_bytes_attr.attr); |
| 271 | if (rc) |
| 272 | goto done1; |
| 273 | |
| 274 | rc = register_filesystem(&pstore_fs_type); |
| 275 | if (rc == 0) |
| 276 | goto done; |
| 277 | |
| 278 | sysfs_remove_file(pstorefs_kobj, &pstore_kmsg_bytes_attr.attr); |
| 279 | done1: |
| 280 | kobject_put(pstorefs_kobj); |
| 281 | done: |
| 282 | return rc; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 283 | } |
| 284 | module_init(init_pstore_fs) |
| 285 | |
| 286 | MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>"); |
| 287 | MODULE_LICENSE("GPL"); |