Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Persistent Storage - ramfs parts.
0004  *
0005  * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/fs.h>
0010 #include <linux/fsnotify.h>
0011 #include <linux/pagemap.h>
0012 #include <linux/highmem.h>
0013 #include <linux/time.h>
0014 #include <linux/init.h>
0015 #include <linux/list.h>
0016 #include <linux/string.h>
0017 #include <linux/mount.h>
0018 #include <linux/seq_file.h>
0019 #include <linux/ramfs.h>
0020 #include <linux/parser.h>
0021 #include <linux/sched.h>
0022 #include <linux/magic.h>
0023 #include <linux/pstore.h>
0024 #include <linux/slab.h>
0025 #include <linux/uaccess.h>
0026 
0027 #include "internal.h"
0028 
0029 #define PSTORE_NAMELEN  64
0030 
0031 static DEFINE_MUTEX(records_list_lock);
0032 static LIST_HEAD(records_list);
0033 
0034 static DEFINE_MUTEX(pstore_sb_lock);
0035 static struct super_block *pstore_sb;
0036 
0037 struct pstore_private {
0038     struct list_head list;
0039     struct dentry *dentry;
0040     struct pstore_record *record;
0041     size_t total_size;
0042 };
0043 
0044 struct pstore_ftrace_seq_data {
0045     const void *ptr;
0046     size_t off;
0047     size_t size;
0048 };
0049 
0050 #define REC_SIZE sizeof(struct pstore_ftrace_record)
0051 
0052 static void free_pstore_private(struct pstore_private *private)
0053 {
0054     if (!private)
0055         return;
0056     if (private->record) {
0057         kfree(private->record->buf);
0058         kfree(private->record->priv);
0059         kfree(private->record);
0060     }
0061     kfree(private);
0062 }
0063 
0064 static void *pstore_ftrace_seq_start(struct seq_file *s, loff_t *pos)
0065 {
0066     struct pstore_private *ps = s->private;
0067     struct pstore_ftrace_seq_data *data;
0068 
0069     data = kzalloc(sizeof(*data), GFP_KERNEL);
0070     if (!data)
0071         return NULL;
0072 
0073     data->off = ps->total_size % REC_SIZE;
0074     data->off += *pos * REC_SIZE;
0075     if (data->off + REC_SIZE > ps->total_size) {
0076         kfree(data);
0077         return NULL;
0078     }
0079 
0080     return data;
0081 
0082 }
0083 
0084 static void pstore_ftrace_seq_stop(struct seq_file *s, void *v)
0085 {
0086     kfree(v);
0087 }
0088 
0089 static void *pstore_ftrace_seq_next(struct seq_file *s, void *v, loff_t *pos)
0090 {
0091     struct pstore_private *ps = s->private;
0092     struct pstore_ftrace_seq_data *data = v;
0093 
0094     (*pos)++;
0095     data->off += REC_SIZE;
0096     if (data->off + REC_SIZE > ps->total_size)
0097         return NULL;
0098 
0099     return data;
0100 }
0101 
0102 static int pstore_ftrace_seq_show(struct seq_file *s, void *v)
0103 {
0104     struct pstore_private *ps = s->private;
0105     struct pstore_ftrace_seq_data *data = v;
0106     struct pstore_ftrace_record *rec;
0107 
0108     if (!data)
0109         return 0;
0110 
0111     rec = (struct pstore_ftrace_record *)(ps->record->buf + data->off);
0112 
0113     seq_printf(s, "CPU:%d ts:%llu %08lx  %08lx  %ps <- %pS\n",
0114            pstore_ftrace_decode_cpu(rec),
0115            pstore_ftrace_read_timestamp(rec),
0116            rec->ip, rec->parent_ip, (void *)rec->ip,
0117            (void *)rec->parent_ip);
0118 
0119     return 0;
0120 }
0121 
0122 static const struct seq_operations pstore_ftrace_seq_ops = {
0123     .start  = pstore_ftrace_seq_start,
0124     .next   = pstore_ftrace_seq_next,
0125     .stop   = pstore_ftrace_seq_stop,
0126     .show   = pstore_ftrace_seq_show,
0127 };
0128 
0129 static ssize_t pstore_file_read(struct file *file, char __user *userbuf,
0130                         size_t count, loff_t *ppos)
0131 {
0132     struct seq_file *sf = file->private_data;
0133     struct pstore_private *ps = sf->private;
0134 
0135     if (ps->record->type == PSTORE_TYPE_FTRACE)
0136         return seq_read(file, userbuf, count, ppos);
0137     return simple_read_from_buffer(userbuf, count, ppos,
0138                        ps->record->buf, ps->total_size);
0139 }
0140 
0141 static int pstore_file_open(struct inode *inode, struct file *file)
0142 {
0143     struct pstore_private *ps = inode->i_private;
0144     struct seq_file *sf;
0145     int err;
0146     const struct seq_operations *sops = NULL;
0147 
0148     if (ps->record->type == PSTORE_TYPE_FTRACE)
0149         sops = &pstore_ftrace_seq_ops;
0150 
0151     err = seq_open(file, sops);
0152     if (err < 0)
0153         return err;
0154 
0155     sf = file->private_data;
0156     sf->private = ps;
0157 
0158     return 0;
0159 }
0160 
0161 static loff_t pstore_file_llseek(struct file *file, loff_t off, int whence)
0162 {
0163     struct seq_file *sf = file->private_data;
0164 
0165     if (sf->op)
0166         return seq_lseek(file, off, whence);
0167     return default_llseek(file, off, whence);
0168 }
0169 
0170 static const struct file_operations pstore_file_operations = {
0171     .open       = pstore_file_open,
0172     .read       = pstore_file_read,
0173     .llseek     = pstore_file_llseek,
0174     .release    = seq_release,
0175 };
0176 
0177 /*
0178  * When a file is unlinked from our file system we call the
0179  * platform driver to erase the record from persistent store.
0180  */
0181 static int pstore_unlink(struct inode *dir, struct dentry *dentry)
0182 {
0183     struct pstore_private *p = d_inode(dentry)->i_private;
0184     struct pstore_record *record = p->record;
0185     int rc = 0;
0186 
0187     if (!record->psi->erase)
0188         return -EPERM;
0189 
0190     /* Make sure we can't race while removing this file. */
0191     mutex_lock(&records_list_lock);
0192     if (!list_empty(&p->list))
0193         list_del_init(&p->list);
0194     else
0195         rc = -ENOENT;
0196     p->dentry = NULL;
0197     mutex_unlock(&records_list_lock);
0198     if (rc)
0199         return rc;
0200 
0201     mutex_lock(&record->psi->read_mutex);
0202     record->psi->erase(record);
0203     mutex_unlock(&record->psi->read_mutex);
0204 
0205     return simple_unlink(dir, dentry);
0206 }
0207 
0208 static void pstore_evict_inode(struct inode *inode)
0209 {
0210     struct pstore_private   *p = inode->i_private;
0211 
0212     clear_inode(inode);
0213     free_pstore_private(p);
0214 }
0215 
0216 static const struct inode_operations pstore_dir_inode_operations = {
0217     .lookup     = simple_lookup,
0218     .unlink     = pstore_unlink,
0219 };
0220 
0221 static struct inode *pstore_get_inode(struct super_block *sb)
0222 {
0223     struct inode *inode = new_inode(sb);
0224     if (inode) {
0225         inode->i_ino = get_next_ino();
0226         inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
0227     }
0228     return inode;
0229 }
0230 
0231 enum {
0232     Opt_kmsg_bytes, Opt_err
0233 };
0234 
0235 static const match_table_t tokens = {
0236     {Opt_kmsg_bytes, "kmsg_bytes=%u"},
0237     {Opt_err, NULL}
0238 };
0239 
0240 static void parse_options(char *options)
0241 {
0242     char        *p;
0243     substring_t args[MAX_OPT_ARGS];
0244     int     option;
0245 
0246     if (!options)
0247         return;
0248 
0249     while ((p = strsep(&options, ",")) != NULL) {
0250         int token;
0251 
0252         if (!*p)
0253             continue;
0254 
0255         token = match_token(p, tokens, args);
0256         switch (token) {
0257         case Opt_kmsg_bytes:
0258             if (!match_int(&args[0], &option))
0259                 pstore_set_kmsg_bytes(option);
0260             break;
0261         }
0262     }
0263 }
0264 
0265 /*
0266  * Display the mount options in /proc/mounts.
0267  */
0268 static int pstore_show_options(struct seq_file *m, struct dentry *root)
0269 {
0270     if (kmsg_bytes != CONFIG_PSTORE_DEFAULT_KMSG_BYTES)
0271         seq_printf(m, ",kmsg_bytes=%lu", kmsg_bytes);
0272     return 0;
0273 }
0274 
0275 static int pstore_remount(struct super_block *sb, int *flags, char *data)
0276 {
0277     sync_filesystem(sb);
0278     parse_options(data);
0279 
0280     return 0;
0281 }
0282 
0283 static const struct super_operations pstore_ops = {
0284     .statfs     = simple_statfs,
0285     .drop_inode = generic_delete_inode,
0286     .evict_inode    = pstore_evict_inode,
0287     .remount_fs = pstore_remount,
0288     .show_options   = pstore_show_options,
0289 };
0290 
0291 static struct dentry *psinfo_lock_root(void)
0292 {
0293     struct dentry *root;
0294 
0295     mutex_lock(&pstore_sb_lock);
0296     /*
0297      * Having no backend is fine -- no records appear.
0298      * Not being mounted is fine -- nothing to do.
0299      */
0300     if (!psinfo || !pstore_sb) {
0301         mutex_unlock(&pstore_sb_lock);
0302         return NULL;
0303     }
0304 
0305     root = pstore_sb->s_root;
0306     inode_lock(d_inode(root));
0307     mutex_unlock(&pstore_sb_lock);
0308 
0309     return root;
0310 }
0311 
0312 int pstore_put_backend_records(struct pstore_info *psi)
0313 {
0314     struct pstore_private *pos, *tmp;
0315     struct dentry *root;
0316     int rc = 0;
0317 
0318     root = psinfo_lock_root();
0319     if (!root)
0320         return 0;
0321 
0322     mutex_lock(&records_list_lock);
0323     list_for_each_entry_safe(pos, tmp, &records_list, list) {
0324         if (pos->record->psi == psi) {
0325             list_del_init(&pos->list);
0326             rc = simple_unlink(d_inode(root), pos->dentry);
0327             if (WARN_ON(rc))
0328                 break;
0329             d_drop(pos->dentry);
0330             dput(pos->dentry);
0331             pos->dentry = NULL;
0332         }
0333     }
0334     mutex_unlock(&records_list_lock);
0335 
0336     inode_unlock(d_inode(root));
0337 
0338     return rc;
0339 }
0340 
0341 /*
0342  * Make a regular file in the root directory of our file system.
0343  * Load it up with "size" bytes of data from "buf".
0344  * Set the mtime & ctime to the date that this record was originally stored.
0345  */
0346 int pstore_mkfile(struct dentry *root, struct pstore_record *record)
0347 {
0348     struct dentry       *dentry;
0349     struct inode        *inode;
0350     int         rc = 0;
0351     char            name[PSTORE_NAMELEN];
0352     struct pstore_private   *private, *pos;
0353     size_t          size = record->size + record->ecc_notice_size;
0354 
0355     if (WARN_ON(!inode_is_locked(d_inode(root))))
0356         return -EINVAL;
0357 
0358     rc = -EEXIST;
0359     /* Skip records that are already present in the filesystem. */
0360     mutex_lock(&records_list_lock);
0361     list_for_each_entry(pos, &records_list, list) {
0362         if (pos->record->type == record->type &&
0363             pos->record->id == record->id &&
0364             pos->record->psi == record->psi)
0365             goto fail;
0366     }
0367 
0368     rc = -ENOMEM;
0369     inode = pstore_get_inode(root->d_sb);
0370     if (!inode)
0371         goto fail;
0372     inode->i_mode = S_IFREG | 0444;
0373     inode->i_fop = &pstore_file_operations;
0374     scnprintf(name, sizeof(name), "%s-%s-%llu%s",
0375             pstore_type_to_name(record->type),
0376             record->psi->name, record->id,
0377             record->compressed ? ".enc.z" : "");
0378 
0379     private = kzalloc(sizeof(*private), GFP_KERNEL);
0380     if (!private)
0381         goto fail_inode;
0382 
0383     dentry = d_alloc_name(root, name);
0384     if (!dentry)
0385         goto fail_private;
0386 
0387     private->dentry = dentry;
0388     private->record = record;
0389     inode->i_size = private->total_size = size;
0390     inode->i_private = private;
0391 
0392     if (record->time.tv_sec)
0393         inode->i_mtime = inode->i_ctime = record->time;
0394 
0395     d_add(dentry, inode);
0396 
0397     list_add(&private->list, &records_list);
0398     mutex_unlock(&records_list_lock);
0399 
0400     return 0;
0401 
0402 fail_private:
0403     free_pstore_private(private);
0404 fail_inode:
0405     iput(inode);
0406 fail:
0407     mutex_unlock(&records_list_lock);
0408     return rc;
0409 }
0410 
0411 /*
0412  * Read all the records from the persistent store. Create
0413  * files in our filesystem.  Don't warn about -EEXIST errors
0414  * when we are re-scanning the backing store looking to add new
0415  * error records.
0416  */
0417 void pstore_get_records(int quiet)
0418 {
0419     struct dentry *root;
0420 
0421     root = psinfo_lock_root();
0422     if (!root)
0423         return;
0424 
0425     pstore_get_backend_records(psinfo, root, quiet);
0426     inode_unlock(d_inode(root));
0427 }
0428 
0429 static int pstore_fill_super(struct super_block *sb, void *data, int silent)
0430 {
0431     struct inode *inode;
0432 
0433     sb->s_maxbytes      = MAX_LFS_FILESIZE;
0434     sb->s_blocksize     = PAGE_SIZE;
0435     sb->s_blocksize_bits    = PAGE_SHIFT;
0436     sb->s_magic     = PSTOREFS_MAGIC;
0437     sb->s_op        = &pstore_ops;
0438     sb->s_time_gran     = 1;
0439 
0440     parse_options(data);
0441 
0442     inode = pstore_get_inode(sb);
0443     if (inode) {
0444         inode->i_mode = S_IFDIR | 0750;
0445         inode->i_op = &pstore_dir_inode_operations;
0446         inode->i_fop = &simple_dir_operations;
0447         inc_nlink(inode);
0448     }
0449     sb->s_root = d_make_root(inode);
0450     if (!sb->s_root)
0451         return -ENOMEM;
0452 
0453     mutex_lock(&pstore_sb_lock);
0454     pstore_sb = sb;
0455     mutex_unlock(&pstore_sb_lock);
0456 
0457     pstore_get_records(0);
0458 
0459     return 0;
0460 }
0461 
0462 static struct dentry *pstore_mount(struct file_system_type *fs_type,
0463     int flags, const char *dev_name, void *data)
0464 {
0465     return mount_single(fs_type, flags, data, pstore_fill_super);
0466 }
0467 
0468 static void pstore_kill_sb(struct super_block *sb)
0469 {
0470     mutex_lock(&pstore_sb_lock);
0471     WARN_ON(pstore_sb && pstore_sb != sb);
0472 
0473     kill_litter_super(sb);
0474     pstore_sb = NULL;
0475 
0476     mutex_lock(&records_list_lock);
0477     INIT_LIST_HEAD(&records_list);
0478     mutex_unlock(&records_list_lock);
0479 
0480     mutex_unlock(&pstore_sb_lock);
0481 }
0482 
0483 static struct file_system_type pstore_fs_type = {
0484     .owner          = THIS_MODULE,
0485     .name       = "pstore",
0486     .mount      = pstore_mount,
0487     .kill_sb    = pstore_kill_sb,
0488 };
0489 
0490 int __init pstore_init_fs(void)
0491 {
0492     int err;
0493 
0494     /* Create a convenient mount point for people to access pstore */
0495     err = sysfs_create_mount_point(fs_kobj, "pstore");
0496     if (err)
0497         goto out;
0498 
0499     err = register_filesystem(&pstore_fs_type);
0500     if (err < 0)
0501         sysfs_remove_mount_point(fs_kobj, "pstore");
0502 
0503 out:
0504     return err;
0505 }
0506 
0507 void __exit pstore_exit_fs(void)
0508 {
0509     unregister_filesystem(&pstore_fs_type);
0510     sysfs_remove_mount_point(fs_kobj, "pstore");
0511 }