Merge tag 'pm-6.16-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[linux-block.git] / fs / pstore / inode.c
CommitLineData
45051539 1// SPDX-License-Identifier: GPL-2.0-only
ca01d6dd
TL
2/*
3 * Persistent Storage - ramfs parts.
4 *
5 * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
ca01d6dd
TL
6 */
7
8#include <linux/module.h>
9#include <linux/fs.h>
10#include <linux/fsnotify.h>
11#include <linux/pagemap.h>
12#include <linux/highmem.h>
13#include <linux/time.h>
14#include <linux/init.h>
6dda9266 15#include <linux/list.h>
ca01d6dd 16#include <linux/string.h>
060287b8 17#include <linux/seq_file.h>
ca01d6dd 18#include <linux/ramfs.h>
f584714c
ES
19#include <linux/fs_parser.h>
20#include <linux/fs_context.h>
ca01d6dd
TL
21#include <linux/sched.h>
22#include <linux/magic.h>
23#include <linux/pstore.h>
24#include <linux/slab.h>
25#include <linux/uaccess.h>
6ba6ee8a 26#include <linux/cleanup.h>
ca01d6dd
TL
27
28#include "internal.h"
29
30#define PSTORE_NAMELEN 64
31
db23491c 32static DEFINE_MUTEX(records_list_lock);
47af61ff 33static LIST_HEAD(records_list);
6dda9266 34
27e5041a
KC
35static DEFINE_MUTEX(pstore_sb_lock);
36static struct super_block *pstore_sb;
37
b775a054
KC
38DEFINE_FREE(pstore_iput, struct inode *, if (_T) iput(_T))
39
ca01d6dd 40struct pstore_private {
6dda9266 41 struct list_head list;
609e28bb 42 struct dentry *dentry;
83f70f07
KC
43 struct pstore_record *record;
44 size_t total_size;
ca01d6dd
TL
45};
46
060287b8
AV
47struct pstore_ftrace_seq_data {
48 const void *ptr;
49 size_t off;
50 size_t size;
51};
52
53#define REC_SIZE sizeof(struct pstore_ftrace_record)
54
1dfff7dd
KC
55static void free_pstore_private(struct pstore_private *private)
56{
57 if (!private)
58 return;
83f70f07 59 if (private->record) {
104fd0b5 60 kvfree(private->record->buf);
8ca869b2 61 kfree(private->record->priv);
83f70f07
KC
62 kfree(private->record);
63 }
1dfff7dd
KC
64 kfree(private);
65}
24a0b5e1 66DEFINE_FREE(pstore_private, struct pstore_private *, free_pstore_private(_T));
1dfff7dd 67
060287b8
AV
68static void *pstore_ftrace_seq_start(struct seq_file *s, loff_t *pos)
69{
70 struct pstore_private *ps = s->private;
6ba6ee8a 71 struct pstore_ftrace_seq_data *data __free(kfree) = NULL;
060287b8
AV
72
73 data = kzalloc(sizeof(*data), GFP_KERNEL);
74 if (!data)
75 return NULL;
76
83f70f07 77 data->off = ps->total_size % REC_SIZE;
060287b8 78 data->off += *pos * REC_SIZE;
6ba6ee8a 79 if (data->off + REC_SIZE > ps->total_size)
060287b8 80 return NULL;
060287b8 81
6ba6ee8a 82 return_ptr(data);
060287b8
AV
83}
84
85static void pstore_ftrace_seq_stop(struct seq_file *s, void *v)
86{
87 kfree(v);
88}
89
90static void *pstore_ftrace_seq_next(struct seq_file *s, void *v, loff_t *pos)
91{
92 struct pstore_private *ps = s->private;
93 struct pstore_ftrace_seq_data *data = v;
94
6c871b73 95 (*pos)++;
060287b8 96 data->off += REC_SIZE;
83f70f07 97 if (data->off + REC_SIZE > ps->total_size)
060287b8
AV
98 return NULL;
99
060287b8
AV
100 return data;
101}
102
103static int pstore_ftrace_seq_show(struct seq_file *s, void *v)
104{
105 struct pstore_private *ps = s->private;
106 struct pstore_ftrace_seq_data *data = v;
83f70f07
KC
107 struct pstore_ftrace_record *rec;
108
6c871b73
VA
109 if (!data)
110 return 0;
111
83f70f07 112 rec = (struct pstore_ftrace_record *)(ps->record->buf + data->off);
060287b8 113
d75f773c 114 seq_printf(s, "CPU:%d ts:%llu %08lx %08lx %ps <- %pS\n",
fbccdeb8
JF
115 pstore_ftrace_decode_cpu(rec),
116 pstore_ftrace_read_timestamp(rec),
117 rec->ip, rec->parent_ip, (void *)rec->ip,
118 (void *)rec->parent_ip);
060287b8
AV
119
120 return 0;
121}
122
123static const struct seq_operations pstore_ftrace_seq_ops = {
124 .start = pstore_ftrace_seq_start,
125 .next = pstore_ftrace_seq_next,
126 .stop = pstore_ftrace_seq_stop,
127 .show = pstore_ftrace_seq_show,
128};
129
fbe0aa1f
TL
130static ssize_t pstore_file_read(struct file *file, char __user *userbuf,
131 size_t count, loff_t *ppos)
132{
060287b8
AV
133 struct seq_file *sf = file->private_data;
134 struct pstore_private *ps = sf->private;
fbe0aa1f 135
83f70f07 136 if (ps->record->type == PSTORE_TYPE_FTRACE)
060287b8 137 return seq_read(file, userbuf, count, ppos);
83f70f07
KC
138 return simple_read_from_buffer(userbuf, count, ppos,
139 ps->record->buf, ps->total_size);
fbe0aa1f
TL
140}
141
060287b8
AV
142static int pstore_file_open(struct inode *inode, struct file *file)
143{
144 struct pstore_private *ps = inode->i_private;
145 struct seq_file *sf;
146 int err;
147 const struct seq_operations *sops = NULL;
148
83f70f07 149 if (ps->record->type == PSTORE_TYPE_FTRACE)
060287b8
AV
150 sops = &pstore_ftrace_seq_ops;
151
152 err = seq_open(file, sops);
153 if (err < 0)
154 return err;
155
156 sf = file->private_data;
157 sf->private = ps;
158
159 return 0;
160}
161
965c8e59 162static loff_t pstore_file_llseek(struct file *file, loff_t off, int whence)
060287b8
AV
163{
164 struct seq_file *sf = file->private_data;
165
166 if (sf->op)
965c8e59
AM
167 return seq_lseek(file, off, whence);
168 return default_llseek(file, off, whence);
060287b8
AV
169}
170
fbe0aa1f 171static const struct file_operations pstore_file_operations = {
060287b8
AV
172 .open = pstore_file_open,
173 .read = pstore_file_read,
174 .llseek = pstore_file_llseek,
175 .release = seq_release,
fbe0aa1f 176};
ca01d6dd
TL
177
178/*
179 * When a file is unlinked from our file system we call the
180 * platform driver to erase the record from persistent store.
181 */
182static int pstore_unlink(struct inode *dir, struct dentry *dentry)
183{
2b0143b5 184 struct pstore_private *p = d_inode(dentry)->i_private;
83f70f07 185 struct pstore_record *record = p->record;
ca01d6dd 186
a61072aa 187 if (!record->psi->erase)
bf288333 188 return -EPERM;
a61072aa 189
7a0ad546 190 /* Make sure we can't race while removing this file. */
e2eeddef
KC
191 scoped_guard(mutex, &records_list_lock) {
192 if (!list_empty(&p->list))
193 list_del_init(&p->list);
194 else
195 return -ENOENT;
196 p->dentry = NULL;
197 }
198
199 scoped_guard(mutex, &record->psi->read_mutex)
200 record->psi->erase(record);
ca01d6dd
TL
201
202 return simple_unlink(dir, dentry);
203}
204
a872d510
TL
205static void pstore_evict_inode(struct inode *inode)
206{
6dda9266 207 struct pstore_private *p = inode->i_private;
6dda9266 208
dbd5768f 209 clear_inode(inode);
7a0ad546 210 free_pstore_private(p);
a872d510
TL
211}
212
ca01d6dd
TL
213static const struct inode_operations pstore_dir_inode_operations = {
214 .lookup = simple_lookup,
215 .unlink = pstore_unlink,
216};
217
22a71c30 218static struct inode *pstore_get_inode(struct super_block *sb)
fbe0aa1f
TL
219{
220 struct inode *inode = new_inode(sb);
fbe0aa1f
TL
221 if (inode) {
222 inode->i_ino = get_next_ino();
1b3c527f 223 simple_inode_init_ts(inode);
fbe0aa1f
TL
224 }
225 return inode;
226}
227
366f7e7a 228enum {
f584714c 229 Opt_kmsg_bytes
366f7e7a
LT
230};
231
f584714c
ES
232static const struct fs_parameter_spec pstore_param_spec[] = {
233 fsparam_u32 ("kmsg_bytes", Opt_kmsg_bytes),
234 {}
366f7e7a
LT
235};
236
f584714c
ES
237struct pstore_context {
238 unsigned int kmsg_bytes;
239};
366f7e7a 240
f584714c
ES
241static int pstore_parse_param(struct fs_context *fc, struct fs_parameter *param)
242{
243 struct pstore_context *ctx = fc->fs_private;
244 struct fs_parse_result result;
245 int opt;
366f7e7a 246
f584714c
ES
247 opt = fs_parse(fc, pstore_param_spec, param, &result);
248 /* pstore has historically ignored invalid kmsg_bytes param */
249 if (opt < 0)
250 return 0;
366f7e7a 251
f584714c
ES
252 switch (opt) {
253 case Opt_kmsg_bytes:
254 ctx->kmsg_bytes = result.uint_32;
255 break;
256 default:
257 return -EINVAL;
366f7e7a 258 }
f584714c
ES
259
260 return 0;
366f7e7a
LT
261}
262
349d7438
DH
263/*
264 * Display the mount options in /proc/mounts.
265 */
266static int pstore_show_options(struct seq_file *m, struct dentry *root)
267{
26fecbf7 268 if (kmsg_bytes != CONFIG_PSTORE_DEFAULT_KMSG_BYTES)
56746095 269 seq_printf(m, ",kmsg_bytes=%u", kmsg_bytes);
349d7438
DH
270 return 0;
271}
272
f584714c 273static int pstore_reconfigure(struct fs_context *fc)
366f7e7a 274{
f584714c
ES
275 struct pstore_context *ctx = fc->fs_private;
276
277 sync_filesystem(fc->root->d_sb);
278 pstore_set_kmsg_bytes(ctx->kmsg_bytes);
366f7e7a
LT
279
280 return 0;
281}
282
ca01d6dd
TL
283static const struct super_operations pstore_ops = {
284 .statfs = simple_statfs,
285 .drop_inode = generic_delete_inode,
a872d510 286 .evict_inode = pstore_evict_inode,
349d7438 287 .show_options = pstore_show_options,
ca01d6dd
TL
288};
289
27e5041a 290static struct dentry *psinfo_lock_root(void)
ca01d6dd 291{
27e5041a
KC
292 struct dentry *root;
293
e2eeddef 294 guard(mutex)(&pstore_sb_lock);
27e5041a
KC
295 /*
296 * Having no backend is fine -- no records appear.
297 * Not being mounted is fine -- nothing to do.
298 */
e2eeddef 299 if (!psinfo || !pstore_sb)
27e5041a 300 return NULL;
27e5041a
KC
301
302 root = pstore_sb->s_root;
303 inode_lock(d_inode(root));
27e5041a
KC
304
305 return root;
ca01d6dd
TL
306}
307
609e28bb
KC
308int pstore_put_backend_records(struct pstore_info *psi)
309{
310 struct pstore_private *pos, *tmp;
311 struct dentry *root;
609e28bb
KC
312
313 root = psinfo_lock_root();
314 if (!root)
315 return 0;
316
e2eeddef
KC
317 scoped_guard(mutex, &records_list_lock) {
318 list_for_each_entry_safe(pos, tmp, &records_list, list) {
319 if (pos->record->psi == psi) {
320 list_del_init(&pos->list);
a43e0fc5
KC
321 d_invalidate(pos->dentry);
322 simple_unlink(d_inode(root), pos->dentry);
e2eeddef
KC
323 pos->dentry = NULL;
324 }
609e28bb
KC
325 }
326 }
609e28bb
KC
327
328 inode_unlock(d_inode(root));
329
a43e0fc5 330 return 0;
609e28bb
KC
331}
332
ca01d6dd
TL
333/*
334 * Make a regular file in the root directory of our file system.
335 * Load it up with "size" bytes of data from "buf".
336 * Set the mtime & ctime to the date that this record was originally stored.
337 */
3a7d2fd1 338int pstore_mkfile(struct dentry *root, struct pstore_record *record)
ca01d6dd 339{
ca01d6dd 340 struct dentry *dentry;
b775a054 341 struct inode *inode __free(pstore_iput) = NULL;
ca01d6dd 342 char name[PSTORE_NAMELEN];
24a0b5e1 343 struct pstore_private *private __free(pstore_private) = NULL, *pos;
1edd1aa3 344 size_t size = record->size + record->ecc_notice_size;
6dda9266 345
27e5041a
KC
346 if (WARN_ON(!inode_is_locked(d_inode(root))))
347 return -EINVAL;
3a7d2fd1 348
e2eeddef
KC
349 guard(mutex)(&records_list_lock);
350
27e5041a 351 /* Skip records that are already present in the filesystem. */
47af61ff 352 list_for_each_entry(pos, &records_list, list) {
83f70f07
KC
353 if (pos->record->type == record->type &&
354 pos->record->id == record->id &&
27e5041a 355 pos->record->psi == record->psi)
e2eeddef 356 return -EEXIST;
6dda9266 357 }
ca01d6dd 358
3a7d2fd1 359 inode = pstore_get_inode(root->d_sb);
ca01d6dd 360 if (!inode)
e2eeddef 361 return -ENOMEM;
22a71c30
AV
362 inode->i_mode = S_IFREG | 0444;
363 inode->i_fop = &pstore_file_operations;
f0f23e54
JFG
364 scnprintf(name, sizeof(name), "%s-%s-%llu%s",
365 pstore_type_to_name(record->type),
366 record->psi->name, record->id,
367 record->compressed ? ".enc.z" : "");
ca01d6dd 368
4c6d80e1
NM
369 private = kzalloc(sizeof(*private), GFP_KERNEL);
370 if (!private)
b775a054 371 return -ENOMEM;
4c6d80e1 372
ca01d6dd 373 dentry = d_alloc_name(root, name);
c39524e6 374 if (!dentry)
24a0b5e1 375 return -ENOMEM;
ca01d6dd 376
609e28bb 377 private->dentry = dentry;
4c6d80e1 378 private->record = record;
83f70f07 379 inode->i_size = private->total_size = size;
ca01d6dd
TL
380 inode->i_private = private;
381
1edd1aa3 382 if (record->time.tv_sec)
1b3c527f
JL
383 inode_set_mtime_to_ts(inode,
384 inode_set_ctime_to_ts(inode, record->time));
ca01d6dd 385
b775a054 386 d_add(dentry, no_free_ptr(inode));
ca01d6dd 387
24a0b5e1 388 list_add(&(no_free_ptr(private))->list, &records_list);
6dda9266 389
fbe0aa1f 390 return 0;
ca01d6dd
TL
391}
392
3a7d2fd1
KC
393/*
394 * Read all the records from the persistent store. Create
395 * files in our filesystem. Don't warn about -EEXIST errors
396 * when we are re-scanning the backing store looking to add new
397 * error records.
398 */
399void pstore_get_records(int quiet)
400{
3a7d2fd1
KC
401 struct dentry *root;
402
27e5041a
KC
403 root = psinfo_lock_root();
404 if (!root)
3a7d2fd1
KC
405 return;
406
27e5041a 407 pstore_get_backend_records(psinfo, root, quiet);
3a7d2fd1
KC
408 inode_unlock(d_inode(root));
409}
410
f584714c 411static int pstore_fill_super(struct super_block *sb, struct fs_context *fc)
ca01d6dd 412{
f584714c 413 struct pstore_context *ctx = fc->fs_private;
318ceed0 414 struct inode *inode;
ca01d6dd 415
ca01d6dd 416 sb->s_maxbytes = MAX_LFS_FILESIZE;
09cbfeaf
KS
417 sb->s_blocksize = PAGE_SIZE;
418 sb->s_blocksize_bits = PAGE_SHIFT;
ca01d6dd
TL
419 sb->s_magic = PSTOREFS_MAGIC;
420 sb->s_op = &pstore_ops;
421 sb->s_time_gran = 1;
422
f584714c 423 pstore_set_kmsg_bytes(ctx->kmsg_bytes);
366f7e7a 424
22a71c30 425 inode = pstore_get_inode(sb);
318ceed0 426 if (inode) {
d7caa336 427 inode->i_mode = S_IFDIR | 0750;
318ceed0 428 inode->i_op = &pstore_dir_inode_operations;
22a71c30
AV
429 inode->i_fop = &simple_dir_operations;
430 inc_nlink(inode);
ca01d6dd 431 }
318ceed0
AV
432 sb->s_root = d_make_root(inode);
433 if (!sb->s_root)
434 return -ENOMEM;
ca01d6dd 435
e2eeddef
KC
436 scoped_guard(mutex, &pstore_sb_lock)
437 pstore_sb = sb;
27e5041a 438
6dda9266 439 pstore_get_records(0);
ca01d6dd
TL
440
441 return 0;
ca01d6dd
TL
442}
443
f584714c
ES
444static int pstore_get_tree(struct fs_context *fc)
445{
446 if (fc->root)
447 return pstore_reconfigure(fc);
448
449 return get_tree_single(fc, pstore_fill_super);
450}
451
452static void pstore_free_fc(struct fs_context *fc)
ca01d6dd 453{
f584714c 454 kfree(fc->fs_private);
ca01d6dd
TL
455}
456
f584714c
ES
457static const struct fs_context_operations pstore_context_ops = {
458 .parse_param = pstore_parse_param,
459 .get_tree = pstore_get_tree,
460 .reconfigure = pstore_reconfigure,
461 .free = pstore_free_fc,
462};
463
ca01d6dd
TL
464static void pstore_kill_sb(struct super_block *sb)
465{
e2eeddef 466 guard(mutex)(&pstore_sb_lock);
9c7d83ae 467 WARN_ON(pstore_sb && pstore_sb != sb);
27e5041a 468
ca01d6dd
TL
469 kill_litter_super(sb);
470 pstore_sb = NULL;
27e5041a 471
e2eeddef 472 guard(mutex)(&records_list_lock);
7a0ad546 473 INIT_LIST_HEAD(&records_list);
ca01d6dd
TL
474}
475
f584714c
ES
476static int pstore_init_fs_context(struct fs_context *fc)
477{
478 struct pstore_context *ctx;
479
480 ctx = kzalloc(sizeof(struct pstore_context), GFP_KERNEL);
481 if (!ctx)
482 return -ENOMEM;
483
484 /*
485 * Global kmsg_bytes is initialized to default, and updated
486 * every time we (re)mount the single-sb filesystem with the
487 * option specified.
488 */
489 ctx->kmsg_bytes = kmsg_bytes;
490
491 fc->fs_private = ctx;
492 fc->ops = &pstore_context_ops;
493
494 return 0;
495}
496
ca01d6dd 497static struct file_system_type pstore_fs_type = {
ee1d2674 498 .owner = THIS_MODULE,
ca01d6dd 499 .name = "pstore",
ca01d6dd 500 .kill_sb = pstore_kill_sb,
f584714c
ES
501 .init_fs_context = pstore_init_fs_context,
502 .parameters = pstore_param_spec,
ca01d6dd
TL
503};
504
cb095afd 505int __init pstore_init_fs(void)
ca01d6dd 506{
f9bb4882 507 int err;
fb0af3f2
JB
508
509 /* Create a convenient mount point for people to access pstore */
f9bb4882
EB
510 err = sysfs_create_mount_point(fs_kobj, "pstore");
511 if (err)
fb0af3f2 512 goto out;
fb0af3f2
JB
513
514 err = register_filesystem(&pstore_fs_type);
515 if (err < 0)
f9bb4882 516 sysfs_remove_mount_point(fs_kobj, "pstore");
fb0af3f2
JB
517
518out:
519 return err;
ca01d6dd 520}
ca01d6dd 521
cb095afd 522void __exit pstore_exit_fs(void)
ee1d2674
GT
523{
524 unregister_filesystem(&pstore_fs_type);
525 sysfs_remove_mount_point(fs_kobj, "pstore");
526}