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); |
| 43 | }; |
| 44 | |
| 45 | #define pstore_get_inode ramfs_get_inode |
| 46 | |
| 47 | /* |
| 48 | * When a file is unlinked from our file system we call the |
| 49 | * platform driver to erase the record from persistent store. |
| 50 | */ |
| 51 | static int pstore_unlink(struct inode *dir, struct dentry *dentry) |
| 52 | { |
| 53 | struct pstore_private *p = dentry->d_inode->i_private; |
| 54 | |
| 55 | p->erase(p->id); |
| 56 | kfree(p); |
| 57 | |
| 58 | return simple_unlink(dir, dentry); |
| 59 | } |
| 60 | |
| 61 | static const struct inode_operations pstore_dir_inode_operations = { |
| 62 | .lookup = simple_lookup, |
| 63 | .unlink = pstore_unlink, |
| 64 | }; |
| 65 | |
| 66 | static const struct super_operations pstore_ops = { |
| 67 | .statfs = simple_statfs, |
| 68 | .drop_inode = generic_delete_inode, |
| 69 | .show_options = generic_show_options, |
| 70 | }; |
| 71 | |
| 72 | static struct super_block *pstore_sb; |
| 73 | static struct vfsmount *pstore_mnt; |
| 74 | |
| 75 | int pstore_is_mounted(void) |
| 76 | { |
| 77 | return pstore_mnt != NULL; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Set up a file structure as if we had opened this file and |
| 82 | * write our data to it. |
| 83 | */ |
| 84 | static int pstore_writefile(struct inode *inode, struct dentry *dentry, |
| 85 | char *data, size_t size) |
| 86 | { |
| 87 | struct file f; |
| 88 | ssize_t n; |
| 89 | mm_segment_t old_fs = get_fs(); |
| 90 | |
| 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; |
| 95 | f.f_pos = 0; |
| 96 | f.f_op = inode->i_fop; |
| 97 | set_fs(KERNEL_DS); |
| 98 | n = do_sync_write(&f, data, size, &f.f_pos); |
| 99 | set_fs(old_fs); |
| 100 | |
| 101 | fsnotify_modify(&f); |
| 102 | |
| 103 | return n == size; |
| 104 | } |
| 105 | |
| 106 | /* |
| 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. |
| 110 | */ |
| 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)) |
| 114 | { |
| 115 | struct dentry *root = pstore_sb->s_root; |
| 116 | struct dentry *dentry; |
| 117 | struct inode *inode; |
| 118 | int rc; |
| 119 | char name[PSTORE_NAMELEN]; |
| 120 | struct pstore_private *private; |
| 121 | |
| 122 | rc = -ENOMEM; |
| 123 | inode = pstore_get_inode(pstore_sb, root->d_inode, S_IFREG | 0444, 0); |
| 124 | if (!inode) |
| 125 | goto fail; |
| 126 | inode->i_uid = inode->i_gid = 0; |
| 127 | private = kmalloc(sizeof *private, GFP_KERNEL); |
| 128 | if (!private) |
| 129 | goto fail_alloc; |
| 130 | private->id = id; |
| 131 | private->erase = erase; |
| 132 | |
| 133 | switch (type) { |
| 134 | case PSTORE_TYPE_DMESG: |
| 135 | sprintf(name, "dmesg-%s-%lld", psname, id); |
| 136 | break; |
| 137 | case PSTORE_TYPE_MCE: |
| 138 | sprintf(name, "mce-%s-%lld", psname, id); |
| 139 | break; |
| 140 | case PSTORE_TYPE_UNKNOWN: |
| 141 | sprintf(name, "unknown-%s-%lld", psname, id); |
| 142 | break; |
| 143 | default: |
| 144 | sprintf(name, "type%d-%s-%lld", type, psname, id); |
| 145 | break; |
| 146 | } |
| 147 | |
| 148 | mutex_lock(&root->d_inode->i_mutex); |
| 149 | |
| 150 | rc = -ENOSPC; |
| 151 | dentry = d_alloc_name(root, name); |
| 152 | if (IS_ERR(dentry)) |
| 153 | goto fail_lockedalloc; |
| 154 | |
| 155 | d_add(dentry, inode); |
| 156 | |
| 157 | mutex_unlock(&root->d_inode->i_mutex); |
| 158 | |
| 159 | if (!pstore_writefile(inode, dentry, data, size)) |
| 160 | goto fail_write; |
| 161 | |
| 162 | inode->i_private = private; |
| 163 | |
| 164 | if (time.tv_sec) |
| 165 | inode->i_mtime = inode->i_ctime = time; |
| 166 | |
| 167 | return 0; |
| 168 | |
| 169 | fail_write: |
| 170 | kfree(private); |
| 171 | inode->i_nlink--; |
| 172 | mutex_lock(&root->d_inode->i_mutex); |
| 173 | d_delete(dentry); |
| 174 | dput(dentry); |
| 175 | mutex_unlock(&root->d_inode->i_mutex); |
| 176 | goto fail; |
| 177 | |
| 178 | fail_lockedalloc: |
| 179 | mutex_unlock(&root->d_inode->i_mutex); |
| 180 | kfree(private); |
| 181 | fail_alloc: |
| 182 | iput(inode); |
| 183 | |
| 184 | fail: |
| 185 | return rc; |
| 186 | } |
| 187 | |
| 188 | int pstore_fill_super(struct super_block *sb, void *data, int silent) |
| 189 | { |
| 190 | struct inode *inode = NULL; |
| 191 | struct dentry *root; |
| 192 | int err; |
| 193 | |
| 194 | save_mount_options(sb, data); |
| 195 | |
| 196 | pstore_sb = sb; |
| 197 | |
| 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; |
| 203 | sb->s_time_gran = 1; |
| 204 | |
| 205 | inode = pstore_get_inode(sb, NULL, S_IFDIR | 0755, 0); |
| 206 | if (!inode) { |
| 207 | err = -ENOMEM; |
| 208 | goto fail; |
| 209 | } |
| 210 | /* override ramfs "dir" options so we catch unlink(2) */ |
| 211 | inode->i_op = &pstore_dir_inode_operations; |
| 212 | |
| 213 | root = d_alloc_root(inode); |
| 214 | sb->s_root = root; |
| 215 | if (!root) { |
| 216 | err = -ENOMEM; |
| 217 | goto fail; |
| 218 | } |
| 219 | |
| 220 | pstore_get_records(); |
| 221 | |
| 222 | return 0; |
| 223 | fail: |
| 224 | iput(inode); |
| 225 | return err; |
| 226 | } |
| 227 | |
| 228 | static int pstore_get_sb(struct file_system_type *fs_type, |
| 229 | int flags, const char *dev_name, void *data, struct vfsmount *mnt) |
| 230 | { |
| 231 | struct dentry *root; |
| 232 | |
| 233 | root = mount_nodev(fs_type, flags, data, pstore_fill_super); |
| 234 | if (IS_ERR(root)) |
| 235 | return -ENOMEM; |
| 236 | |
| 237 | mnt->mnt_root = root; |
| 238 | mnt->mnt_sb = root->d_sb; |
| 239 | pstore_mnt = mnt; |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | static void pstore_kill_sb(struct super_block *sb) |
| 245 | { |
| 246 | kill_litter_super(sb); |
| 247 | pstore_sb = NULL; |
| 248 | pstore_mnt = NULL; |
| 249 | } |
| 250 | |
| 251 | static struct file_system_type pstore_fs_type = { |
| 252 | .name = "pstore", |
| 253 | .get_sb = pstore_get_sb, |
| 254 | .kill_sb = pstore_kill_sb, |
| 255 | }; |
| 256 | |
| 257 | static int __init init_pstore_fs(void) |
| 258 | { |
| 259 | int ret = 0; |
| 260 | struct kobject *pstorefs_kobj; |
| 261 | |
| 262 | pstorefs_kobj = kobject_create_and_add("pstore", fs_kobj); |
| 263 | if (!pstorefs_kobj) |
| 264 | return -ENOMEM; |
| 265 | |
| 266 | sysfs_create_file(pstorefs_kobj, &pstore_kmsg_bytes_attr.attr); |
| 267 | |
| 268 | ret = register_filesystem(&pstore_fs_type); |
| 269 | |
| 270 | if (ret) { |
| 271 | sysfs_remove_file(pstorefs_kobj, &pstore_kmsg_bytes_attr.attr); |
| 272 | kobject_put(pstorefs_kobj); |
| 273 | } |
| 274 | |
| 275 | return ret; |
| 276 | } |
| 277 | module_init(init_pstore_fs) |
| 278 | |
| 279 | MODULE_AUTHOR("Tony Luck <tony.luck@intel.com>"); |
| 280 | MODULE_LICENSE("GPL"); |