xref: /linux/fs/pstore/inode.c (revision 2d7f3d1a5866705be2393150e1ffdf67030ab88d)
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/mount.h>
18 #include <linux/seq_file.h>
19 #include <linux/ramfs.h>
20 #include <linux/parser.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(sizeof(*data), GFP_KERNEL);
74 	if (!data)
75 		return NULL;
76 
77 	data->off = ps->total_size % REC_SIZE;
78 	data->off += *pos * REC_SIZE;
79 	if (data->off + REC_SIZE > ps->total_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->total_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 
109 	if (!data)
110 		return 0;
111 
112 	rec = (struct pstore_ftrace_record *)(ps->record->buf + data->off);
113 
114 	seq_printf(s, "CPU:%d ts:%llu %08lx  %08lx  %ps <- %pS\n",
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);
119 
120 	return 0;
121 }
122 
123 static 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 
130 static ssize_t pstore_file_read(struct file *file, char __user *userbuf,
131 						size_t count, loff_t *ppos)
132 {
133 	struct seq_file *sf = file->private_data;
134 	struct pstore_private *ps = sf->private;
135 
136 	if (ps->record->type == PSTORE_TYPE_FTRACE)
137 		return seq_read(file, userbuf, count, ppos);
138 	return simple_read_from_buffer(userbuf, count, ppos,
139 				       ps->record->buf, ps->total_size);
140 }
141 
142 static 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 
149 	if (ps->record->type == PSTORE_TYPE_FTRACE)
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 
162 static loff_t pstore_file_llseek(struct file *file, loff_t off, int whence)
163 {
164 	struct seq_file *sf = file->private_data;
165 
166 	if (sf->op)
167 		return seq_lseek(file, off, whence);
168 	return default_llseek(file, off, whence);
169 }
170 
171 static const struct file_operations pstore_file_operations = {
172 	.open		= pstore_file_open,
173 	.read		= pstore_file_read,
174 	.llseek		= pstore_file_llseek,
175 	.release	= seq_release,
176 };
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  */
182 static int pstore_unlink(struct inode *dir, struct dentry *dentry)
183 {
184 	struct pstore_private *p = d_inode(dentry)->i_private;
185 	struct pstore_record *record = p->record;
186 
187 	if (!record->psi->erase)
188 		return -EPERM;
189 
190 	/* Make sure we can't race while removing this file. */
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);
201 
202 	return simple_unlink(dir, dentry);
203 }
204 
205 static void pstore_evict_inode(struct inode *inode)
206 {
207 	struct pstore_private	*p = inode->i_private;
208 
209 	clear_inode(inode);
210 	free_pstore_private(p);
211 }
212 
213 static const struct inode_operations pstore_dir_inode_operations = {
214 	.lookup		= simple_lookup,
215 	.unlink		= pstore_unlink,
216 };
217 
218 static struct inode *pstore_get_inode(struct super_block *sb)
219 {
220 	struct inode *inode = new_inode(sb);
221 	if (inode) {
222 		inode->i_ino = get_next_ino();
223 		simple_inode_init_ts(inode);
224 	}
225 	return inode;
226 }
227 
228 enum {
229 	Opt_kmsg_bytes, Opt_err
230 };
231 
232 static const match_table_t tokens = {
233 	{Opt_kmsg_bytes, "kmsg_bytes=%u"},
234 	{Opt_err, NULL}
235 };
236 
237 static void parse_options(char *options)
238 {
239 	char		*p;
240 	substring_t	args[MAX_OPT_ARGS];
241 	int		option;
242 
243 	if (!options)
244 		return;
245 
246 	while ((p = strsep(&options, ",")) != NULL) {
247 		int token;
248 
249 		if (!*p)
250 			continue;
251 
252 		token = match_token(p, tokens, args);
253 		switch (token) {
254 		case Opt_kmsg_bytes:
255 			if (!match_int(&args[0], &option))
256 				pstore_set_kmsg_bytes(option);
257 			break;
258 		}
259 	}
260 }
261 
262 /*
263  * Display the mount options in /proc/mounts.
264  */
265 static int pstore_show_options(struct seq_file *m, struct dentry *root)
266 {
267 	if (kmsg_bytes != CONFIG_PSTORE_DEFAULT_KMSG_BYTES)
268 		seq_printf(m, ",kmsg_bytes=%lu", kmsg_bytes);
269 	return 0;
270 }
271 
272 static int pstore_remount(struct super_block *sb, int *flags, char *data)
273 {
274 	sync_filesystem(sb);
275 	parse_options(data);
276 
277 	return 0;
278 }
279 
280 static const struct super_operations pstore_ops = {
281 	.statfs		= simple_statfs,
282 	.drop_inode	= generic_delete_inode,
283 	.evict_inode	= pstore_evict_inode,
284 	.remount_fs	= pstore_remount,
285 	.show_options	= pstore_show_options,
286 };
287 
288 static struct dentry *psinfo_lock_root(void)
289 {
290 	struct dentry *root;
291 
292 	guard(mutex)(&pstore_sb_lock);
293 	/*
294 	 * Having no backend is fine -- no records appear.
295 	 * Not being mounted is fine -- nothing to do.
296 	 */
297 	if (!psinfo || !pstore_sb)
298 		return NULL;
299 
300 	root = pstore_sb->s_root;
301 	inode_lock(d_inode(root));
302 
303 	return root;
304 }
305 
306 int pstore_put_backend_records(struct pstore_info *psi)
307 {
308 	struct pstore_private *pos, *tmp;
309 	struct dentry *root;
310 	int rc = 0;
311 
312 	root = psinfo_lock_root();
313 	if (!root)
314 		return 0;
315 
316 	scoped_guard(mutex, &records_list_lock) {
317 		list_for_each_entry_safe(pos, tmp, &records_list, list) {
318 			if (pos->record->psi == psi) {
319 				list_del_init(&pos->list);
320 				rc = simple_unlink(d_inode(root), pos->dentry);
321 				if (WARN_ON(rc))
322 					break;
323 				d_drop(pos->dentry);
324 				dput(pos->dentry);
325 				pos->dentry = NULL;
326 			}
327 		}
328 	}
329 
330 	inode_unlock(d_inode(root));
331 
332 	return rc;
333 }
334 
335 /*
336  * Make a regular file in the root directory of our file system.
337  * Load it up with "size" bytes of data from "buf".
338  * Set the mtime & ctime to the date that this record was originally stored.
339  */
340 int pstore_mkfile(struct dentry *root, struct pstore_record *record)
341 {
342 	struct dentry		*dentry;
343 	struct inode		*inode __free(pstore_iput) = NULL;
344 	char			name[PSTORE_NAMELEN];
345 	struct pstore_private	*private __free(pstore_private) = NULL, *pos;
346 	size_t			size = record->size + record->ecc_notice_size;
347 
348 	if (WARN_ON(!inode_is_locked(d_inode(root))))
349 		return -EINVAL;
350 
351 	guard(mutex)(&records_list_lock);
352 
353 	/* Skip records that are already present in the filesystem. */
354 	list_for_each_entry(pos, &records_list, list) {
355 		if (pos->record->type == record->type &&
356 		    pos->record->id == record->id &&
357 		    pos->record->psi == record->psi)
358 			return -EEXIST;
359 	}
360 
361 	inode = pstore_get_inode(root->d_sb);
362 	if (!inode)
363 		return -ENOMEM;
364 	inode->i_mode = S_IFREG | 0444;
365 	inode->i_fop = &pstore_file_operations;
366 	scnprintf(name, sizeof(name), "%s-%s-%llu%s",
367 			pstore_type_to_name(record->type),
368 			record->psi->name, record->id,
369 			record->compressed ? ".enc.z" : "");
370 
371 	private = kzalloc(sizeof(*private), GFP_KERNEL);
372 	if (!private)
373 		return -ENOMEM;
374 
375 	dentry = d_alloc_name(root, name);
376 	if (!dentry)
377 		return -ENOMEM;
378 
379 	private->dentry = dentry;
380 	private->record = record;
381 	inode->i_size = private->total_size = size;
382 	inode->i_private = private;
383 
384 	if (record->time.tv_sec)
385 		inode_set_mtime_to_ts(inode,
386 				      inode_set_ctime_to_ts(inode, record->time));
387 
388 	d_add(dentry, no_free_ptr(inode));
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, void *data, int silent)
414 {
415 	struct inode *inode;
416 
417 	sb->s_maxbytes		= MAX_LFS_FILESIZE;
418 	sb->s_blocksize		= PAGE_SIZE;
419 	sb->s_blocksize_bits	= PAGE_SHIFT;
420 	sb->s_magic		= PSTOREFS_MAGIC;
421 	sb->s_op		= &pstore_ops;
422 	sb->s_time_gran		= 1;
423 
424 	parse_options(data);
425 
426 	inode = pstore_get_inode(sb);
427 	if (inode) {
428 		inode->i_mode = S_IFDIR | 0750;
429 		inode->i_op = &pstore_dir_inode_operations;
430 		inode->i_fop = &simple_dir_operations;
431 		inc_nlink(inode);
432 	}
433 	sb->s_root = d_make_root(inode);
434 	if (!sb->s_root)
435 		return -ENOMEM;
436 
437 	scoped_guard(mutex, &pstore_sb_lock)
438 		pstore_sb = sb;
439 
440 	pstore_get_records(0);
441 
442 	return 0;
443 }
444 
445 static struct dentry *pstore_mount(struct file_system_type *fs_type,
446 	int flags, const char *dev_name, void *data)
447 {
448 	return mount_single(fs_type, flags, data, pstore_fill_super);
449 }
450 
451 static void pstore_kill_sb(struct super_block *sb)
452 {
453 	guard(mutex)(&pstore_sb_lock);
454 	WARN_ON(pstore_sb && pstore_sb != sb);
455 
456 	kill_litter_super(sb);
457 	pstore_sb = NULL;
458 
459 	guard(mutex)(&records_list_lock);
460 	INIT_LIST_HEAD(&records_list);
461 }
462 
463 static struct file_system_type pstore_fs_type = {
464 	.owner          = THIS_MODULE,
465 	.name		= "pstore",
466 	.mount		= pstore_mount,
467 	.kill_sb	= pstore_kill_sb,
468 };
469 
470 int __init pstore_init_fs(void)
471 {
472 	int err;
473 
474 	/* Create a convenient mount point for people to access pstore */
475 	err = sysfs_create_mount_point(fs_kobj, "pstore");
476 	if (err)
477 		goto out;
478 
479 	err = register_filesystem(&pstore_fs_type);
480 	if (err < 0)
481 		sysfs_remove_mount_point(fs_kobj, "pstore");
482 
483 out:
484 	return err;
485 }
486 
487 void __exit pstore_exit_fs(void)
488 {
489 	unregister_filesystem(&pstore_fs_type);
490 	sysfs_remove_mount_point(fs_kobj, "pstore");
491 }
492