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