xref: /OK3568_Linux_fs/kernel/fs/pstore/inode.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 
27 #ifdef CONFIG_PSTORE_BOOT_LOG
28 #include <linux/pstore_ram.h>
29 #include <linux/io.h>
30 #endif
31 
32 #include "internal.h"
33 
34 #define	PSTORE_NAMELEN	64
35 
36 static DEFINE_MUTEX(records_list_lock);
37 static LIST_HEAD(records_list);
38 
39 static DEFINE_MUTEX(pstore_sb_lock);
40 static struct super_block *pstore_sb;
41 
42 struct pstore_private {
43 	struct list_head list;
44 	struct dentry *dentry;
45 	struct pstore_record *record;
46 	size_t total_size;
47 };
48 
49 struct pstore_ftrace_seq_data {
50 	const void *ptr;
51 	size_t off;
52 	size_t size;
53 };
54 
55 #define REC_SIZE sizeof(struct pstore_ftrace_record)
56 
free_pstore_private(struct pstore_private * private)57 static void free_pstore_private(struct pstore_private *private)
58 {
59 	if (!private)
60 		return;
61 	if (private->record) {
62 		kfree(private->record->buf);
63 		kfree(private->record);
64 	}
65 	kfree(private);
66 }
67 
pstore_ftrace_seq_start(struct seq_file * s,loff_t * pos)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;
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 		kfree(data);
81 		return NULL;
82 	}
83 
84 	return data;
85 
86 }
87 
pstore_ftrace_seq_stop(struct seq_file * s,void * v)88 static void pstore_ftrace_seq_stop(struct seq_file *s, void *v)
89 {
90 	kfree(v);
91 }
92 
pstore_ftrace_seq_next(struct seq_file * s,void * v,loff_t * pos)93 static void *pstore_ftrace_seq_next(struct seq_file *s, void *v, loff_t *pos)
94 {
95 	struct pstore_private *ps = s->private;
96 	struct pstore_ftrace_seq_data *data = v;
97 
98 	(*pos)++;
99 	data->off += REC_SIZE;
100 	if (data->off + REC_SIZE > ps->total_size)
101 		return NULL;
102 
103 	return data;
104 }
105 
pstore_ftrace_seq_show(struct seq_file * s,void * v)106 static int pstore_ftrace_seq_show(struct seq_file *s, void *v)
107 {
108 	struct pstore_private *ps = s->private;
109 	struct pstore_ftrace_seq_data *data = v;
110 	struct pstore_ftrace_record *rec;
111 
112 	if (!data)
113 		return 0;
114 
115 	rec = (struct pstore_ftrace_record *)(ps->record->buf + data->off);
116 
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 		   rec->ip, rec->parent_ip, (void *)rec->ip,
121 		   (void *)rec->parent_ip);
122 
123 	return 0;
124 }
125 
126 static const struct seq_operations pstore_ftrace_seq_ops = {
127 	.start	= pstore_ftrace_seq_start,
128 	.next	= pstore_ftrace_seq_next,
129 	.stop	= pstore_ftrace_seq_stop,
130 	.show	= pstore_ftrace_seq_show,
131 };
132 
pstore_file_read(struct file * file,char __user * userbuf,size_t count,loff_t * ppos)133 static ssize_t pstore_file_read(struct file *file, char __user *userbuf,
134 						size_t count, loff_t *ppos)
135 {
136 	struct seq_file *sf = file->private_data;
137 	struct pstore_private *ps = sf->private;
138 #ifdef CONFIG_PSTORE_BOOT_LOG
139 	size_t size = 0;
140 	struct pstore_record *record = ps->record;
141 
142 	if (record->type == PSTORE_TYPE_BOOT_LOG) {
143 		size = ramoops_pstore_read_for_boot_log(ps->record);
144 		size = simple_read_from_buffer(userbuf, count, ppos, record->buf, size);
145 		return size;
146 	}
147 #endif
148 	if (ps->record->type == PSTORE_TYPE_FTRACE)
149 		return seq_read(file, userbuf, count, ppos);
150 	return simple_read_from_buffer(userbuf, count, ppos,
151 				       ps->record->buf, ps->total_size);
152 }
153 
pstore_file_open(struct inode * inode,struct file * file)154 static int pstore_file_open(struct inode *inode, struct file *file)
155 {
156 	struct pstore_private *ps = inode->i_private;
157 	struct seq_file *sf;
158 	int err;
159 	const struct seq_operations *sops = NULL;
160 
161 	if (ps->record->type == PSTORE_TYPE_FTRACE)
162 		sops = &pstore_ftrace_seq_ops;
163 
164 	err = seq_open(file, sops);
165 	if (err < 0)
166 		return err;
167 
168 	sf = file->private_data;
169 	sf->private = ps;
170 
171 	return 0;
172 }
173 
pstore_file_llseek(struct file * file,loff_t off,int whence)174 static loff_t pstore_file_llseek(struct file *file, loff_t off, int whence)
175 {
176 	struct seq_file *sf = file->private_data;
177 
178 	if (sf->op)
179 		return seq_lseek(file, off, whence);
180 	return default_llseek(file, off, whence);
181 }
182 
183 static const struct file_operations pstore_file_operations = {
184 	.open		= pstore_file_open,
185 	.read		= pstore_file_read,
186 	.llseek		= pstore_file_llseek,
187 	.release	= seq_release,
188 };
189 
190 /*
191  * When a file is unlinked from our file system we call the
192  * platform driver to erase the record from persistent store.
193  */
pstore_unlink(struct inode * dir,struct dentry * dentry)194 static int pstore_unlink(struct inode *dir, struct dentry *dentry)
195 {
196 	struct pstore_private *p = d_inode(dentry)->i_private;
197 	struct pstore_record *record = p->record;
198 	int rc = 0;
199 
200 	if (!record->psi->erase)
201 		return -EPERM;
202 
203 	/* Make sure we can't race while removing this file. */
204 	mutex_lock(&records_list_lock);
205 	if (!list_empty(&p->list))
206 		list_del_init(&p->list);
207 	else
208 		rc = -ENOENT;
209 	p->dentry = NULL;
210 	mutex_unlock(&records_list_lock);
211 	if (rc)
212 		return rc;
213 
214 	mutex_lock(&record->psi->read_mutex);
215 	record->psi->erase(record);
216 	mutex_unlock(&record->psi->read_mutex);
217 
218 	return simple_unlink(dir, dentry);
219 }
220 
pstore_evict_inode(struct inode * inode)221 static void pstore_evict_inode(struct inode *inode)
222 {
223 	struct pstore_private	*p = inode->i_private;
224 
225 	clear_inode(inode);
226 	free_pstore_private(p);
227 }
228 
229 static const struct inode_operations pstore_dir_inode_operations = {
230 	.lookup		= simple_lookup,
231 	.unlink		= pstore_unlink,
232 };
233 
pstore_get_inode(struct super_block * sb)234 static struct inode *pstore_get_inode(struct super_block *sb)
235 {
236 	struct inode *inode = new_inode(sb);
237 	if (inode) {
238 		inode->i_ino = get_next_ino();
239 		inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
240 	}
241 	return inode;
242 }
243 
244 enum {
245 	Opt_kmsg_bytes, Opt_err
246 };
247 
248 static const match_table_t tokens = {
249 	{Opt_kmsg_bytes, "kmsg_bytes=%u"},
250 	{Opt_err, NULL}
251 };
252 
parse_options(char * options)253 static void parse_options(char *options)
254 {
255 	char		*p;
256 	substring_t	args[MAX_OPT_ARGS];
257 	int		option;
258 
259 	if (!options)
260 		return;
261 
262 	while ((p = strsep(&options, ",")) != NULL) {
263 		int token;
264 
265 		if (!*p)
266 			continue;
267 
268 		token = match_token(p, tokens, args);
269 		switch (token) {
270 		case Opt_kmsg_bytes:
271 			if (!match_int(&args[0], &option))
272 				pstore_set_kmsg_bytes(option);
273 			break;
274 		}
275 	}
276 }
277 
278 /*
279  * Display the mount options in /proc/mounts.
280  */
pstore_show_options(struct seq_file * m,struct dentry * root)281 static int pstore_show_options(struct seq_file *m, struct dentry *root)
282 {
283 	if (kmsg_bytes != PSTORE_DEFAULT_KMSG_BYTES)
284 		seq_printf(m, ",kmsg_bytes=%lu", kmsg_bytes);
285 	return 0;
286 }
287 
pstore_remount(struct super_block * sb,int * flags,char * data)288 static int pstore_remount(struct super_block *sb, int *flags, char *data)
289 {
290 	sync_filesystem(sb);
291 	parse_options(data);
292 
293 	return 0;
294 }
295 
296 static const struct super_operations pstore_ops = {
297 	.statfs		= simple_statfs,
298 	.drop_inode	= generic_delete_inode,
299 	.evict_inode	= pstore_evict_inode,
300 	.remount_fs	= pstore_remount,
301 	.show_options	= pstore_show_options,
302 };
303 
psinfo_lock_root(void)304 static struct dentry *psinfo_lock_root(void)
305 {
306 	struct dentry *root;
307 
308 	mutex_lock(&pstore_sb_lock);
309 	/*
310 	 * Having no backend is fine -- no records appear.
311 	 * Not being mounted is fine -- nothing to do.
312 	 */
313 	if (!psinfo || !pstore_sb) {
314 		mutex_unlock(&pstore_sb_lock);
315 		return NULL;
316 	}
317 
318 	root = pstore_sb->s_root;
319 	inode_lock(d_inode(root));
320 	mutex_unlock(&pstore_sb_lock);
321 
322 	return root;
323 }
324 
pstore_put_backend_records(struct pstore_info * psi)325 int pstore_put_backend_records(struct pstore_info *psi)
326 {
327 	struct pstore_private *pos, *tmp;
328 	struct dentry *root;
329 	int rc = 0;
330 
331 	root = psinfo_lock_root();
332 	if (!root)
333 		return 0;
334 
335 	mutex_lock(&records_list_lock);
336 	list_for_each_entry_safe(pos, tmp, &records_list, list) {
337 		if (pos->record->psi == psi) {
338 			list_del_init(&pos->list);
339 			rc = simple_unlink(d_inode(root), pos->dentry);
340 			if (WARN_ON(rc))
341 				break;
342 			d_drop(pos->dentry);
343 			dput(pos->dentry);
344 			pos->dentry = NULL;
345 		}
346 	}
347 	mutex_unlock(&records_list_lock);
348 
349 	inode_unlock(d_inode(root));
350 
351 	return rc;
352 }
353 
354 /*
355  * Make a regular file in the root directory of our file system.
356  * Load it up with "size" bytes of data from "buf".
357  * Set the mtime & ctime to the date that this record was originally stored.
358  */
pstore_mkfile(struct dentry * root,struct pstore_record * record)359 int pstore_mkfile(struct dentry *root, struct pstore_record *record)
360 {
361 	struct dentry		*dentry;
362 	struct inode		*inode;
363 	int			rc = 0;
364 	char			name[PSTORE_NAMELEN];
365 	struct pstore_private	*private, *pos;
366 	size_t			size = record->size + record->ecc_notice_size;
367 
368 	if (WARN_ON(!inode_is_locked(d_inode(root))))
369 		return -EINVAL;
370 
371 	rc = -EEXIST;
372 	/* Skip records that are already present in the filesystem. */
373 	mutex_lock(&records_list_lock);
374 	list_for_each_entry(pos, &records_list, list) {
375 		if (pos->record->type == record->type &&
376 		    pos->record->id == record->id &&
377 		    pos->record->psi == record->psi)
378 			goto fail;
379 	}
380 
381 	rc = -ENOMEM;
382 	inode = pstore_get_inode(root->d_sb);
383 	if (!inode)
384 		goto fail;
385 	inode->i_mode = S_IFREG | 0444;
386 	inode->i_fop = &pstore_file_operations;
387 	scnprintf(name, sizeof(name), "%s-%s-%llu%s",
388 			pstore_type_to_name(record->type),
389 			record->psi->name, record->id,
390 			record->compressed ? ".enc.z" : "");
391 
392 	private = kzalloc(sizeof(*private), GFP_KERNEL);
393 	if (!private)
394 		goto fail_inode;
395 
396 	dentry = d_alloc_name(root, name);
397 	if (!dentry)
398 		goto fail_private;
399 
400 	private->dentry = dentry;
401 	private->record = record;
402 	inode->i_size = private->total_size = size;
403 	inode->i_private = private;
404 
405 	if (record->time.tv_sec)
406 		inode->i_mtime = inode->i_ctime = record->time;
407 
408 	d_add(dentry, inode);
409 
410 	list_add(&private->list, &records_list);
411 	mutex_unlock(&records_list_lock);
412 
413 	return 0;
414 
415 fail_private:
416 	free_pstore_private(private);
417 fail_inode:
418 	iput(inode);
419 fail:
420 	mutex_unlock(&records_list_lock);
421 	return rc;
422 }
423 
424 /*
425  * Read all the records from the persistent store. Create
426  * files in our filesystem.  Don't warn about -EEXIST errors
427  * when we are re-scanning the backing store looking to add new
428  * error records.
429  */
pstore_get_records(int quiet)430 void pstore_get_records(int quiet)
431 {
432 	struct dentry *root;
433 
434 	root = psinfo_lock_root();
435 	if (!root)
436 		return;
437 
438 	pstore_get_backend_records(psinfo, root, quiet);
439 	inode_unlock(d_inode(root));
440 }
441 
pstore_fill_super(struct super_block * sb,void * data,int silent)442 static int pstore_fill_super(struct super_block *sb, void *data, int silent)
443 {
444 	struct inode *inode;
445 
446 	sb->s_maxbytes		= MAX_LFS_FILESIZE;
447 	sb->s_blocksize		= PAGE_SIZE;
448 	sb->s_blocksize_bits	= PAGE_SHIFT;
449 	sb->s_magic		= PSTOREFS_MAGIC;
450 	sb->s_op		= &pstore_ops;
451 	sb->s_time_gran		= 1;
452 
453 	parse_options(data);
454 
455 	inode = pstore_get_inode(sb);
456 	if (inode) {
457 		inode->i_mode = S_IFDIR | 0750;
458 		inode->i_op = &pstore_dir_inode_operations;
459 		inode->i_fop = &simple_dir_operations;
460 		inc_nlink(inode);
461 	}
462 	sb->s_root = d_make_root(inode);
463 	if (!sb->s_root)
464 		return -ENOMEM;
465 
466 	mutex_lock(&pstore_sb_lock);
467 	pstore_sb = sb;
468 	mutex_unlock(&pstore_sb_lock);
469 
470 	pstore_get_records(0);
471 
472 	return 0;
473 }
474 
pstore_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)475 static struct dentry *pstore_mount(struct file_system_type *fs_type,
476 	int flags, const char *dev_name, void *data)
477 {
478 	return mount_single(fs_type, flags, data, pstore_fill_super);
479 }
480 
pstore_kill_sb(struct super_block * sb)481 static void pstore_kill_sb(struct super_block *sb)
482 {
483 	mutex_lock(&pstore_sb_lock);
484 	WARN_ON(pstore_sb && pstore_sb != sb);
485 
486 	kill_litter_super(sb);
487 	pstore_sb = NULL;
488 
489 	mutex_lock(&records_list_lock);
490 	INIT_LIST_HEAD(&records_list);
491 	mutex_unlock(&records_list_lock);
492 
493 	mutex_unlock(&pstore_sb_lock);
494 }
495 
496 static struct file_system_type pstore_fs_type = {
497 	.owner          = THIS_MODULE,
498 	.name		= "pstore",
499 	.mount		= pstore_mount,
500 	.kill_sb	= pstore_kill_sb,
501 };
502 
pstore_init_fs(void)503 int __init pstore_init_fs(void)
504 {
505 	int err;
506 
507 	/* Create a convenient mount point for people to access pstore */
508 	err = sysfs_create_mount_point(fs_kobj, "pstore");
509 	if (err)
510 		goto out;
511 
512 	err = register_filesystem(&pstore_fs_type);
513 	if (err < 0)
514 		sysfs_remove_mount_point(fs_kobj, "pstore");
515 
516 out:
517 	return err;
518 }
519 
pstore_exit_fs(void)520 void __exit pstore_exit_fs(void)
521 {
522 	unregister_filesystem(&pstore_fs_type);
523 	sysfs_remove_mount_point(fs_kobj, "pstore");
524 }
525