summaryrefstree
path: root/kernel/fs/proc/fslog.c
blob: 47649947a519bf22b9a4ede1cb8962f74aa20ba7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/*
 *  linux/fs/proc/fslog.c
 *
 */

#include <linux/types.h>
#include <linux/errno.h>
#include <linux/time.h>
#include <linux/kernel.h>
#include <linux/security.h>
#include <linux/syscalls.h>
#include <linux/proc_fs.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/interrupt.h>
#include <asm/uaccess.h>
#include <linux/fs.h>
#include <linux/seq_file.h>
#include <linux/vmalloc.h>
#include <linux/mm.h>

/* For compatibility */
#include <linux/fslog.h>

#define FSLOG_ACTION_CLOSE          	0
#define FSLOG_ACTION_OPEN           	1
#define FSLOG_ACTION_READ		2
#define FSLOG_ACTION_READ_ALL		3
#define FSLOG_ACTION_WRITE    		4
#define FSLOG_ACTION_SIZE_BUFFER   	5

#define S_PREFIX_MAX			32
#define FSLOG_BUF_LINE_MAX		(1024 - S_PREFIX_MAX)

#define FSLOG_BUF_ALIGN			__alignof__(struct fslog_metadata)

#ifdef CONFIG_PROC_FSLOG_LOWMEM
#define FSLOG_BUFLEN_STLOG		(32 * 1024)
#define FSLOG_BUFLEN_DLOG_EFS		(16 * 1024)
#define FSLOG_BUFLEN_DLOG_RMDIR		(16 * 1024)
#define FSLOG_BUFLEN_DLOG_ETC		(64 * 1024)
#define FSLOG_BUFLEN_DLOG_MM		(128 * 1024)
#else /* device has DRAM of high capacity */
#define FSLOG_BUFLEN_STLOG		(32 * 1024)
#define FSLOG_BUFLEN_DLOG_EFS		(16 * 1024)
#define FSLOG_BUFLEN_DLOG_RMDIR		(16 * 1024)
#define FSLOG_BUFLEN_DLOG_ETC		(192 * 1024)
#define FSLOG_BUFLEN_DLOG_MM		(256 * 1024)
#endif

#define FSLOG_FILE_MODE_VERSION		(S_IRUSR | S_IRGRP | S_IROTH)
#define FSLOG_FILE_MODE_DIR \
	(S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
#define FSLOG_FILE_MODE_STLOG \
	(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)
#define FSLOG_FILE_MODE_DLOG		(S_IRUSR | S_IRGRP)

struct fslog_sequence {
	u64 fslog_seq;
	u32 fslog_idx;
	u64 fslog_first_seq;
	u32 fslog_first_idx;
	u64 fslog_next_seq;
	u32 fslog_next_idx;
	u64 fslog_clear_seq;
	u32 fslog_clear_idx;
	u64 fslog_end_seq;
};

struct fslog_metadata {
	u64 ts_nsec;
	u16 len;
	u16 text_len;
	pid_t pid;
	pid_t tgid;
	char comm[TASK_COMM_LEN];
	char tgid_comm[TASK_COMM_LEN];
};

struct fslog_data {
	struct fslog_sequence fslog_seq;
	struct fslog_metadata fslog_meta;
	spinlock_t fslog_spinlock;
	wait_queue_head_t fslog_wait_queue;
	u32 fslog_buf_len;
	char *fslog_cbuf;
};

static struct proc_dir_entry *fslog_dir;

static int do_fslog(int type, struct fslog_data *fl_data, char __user *buf, int count);
static int do_fslog_write(struct fslog_data *fl_data, const char __user *buf, int len);
static int vfslog(struct fslog_data *fl_data, const char *fmt, va_list args);

#define DEFINE_FSLOG_VARIABLE(_name, size) \
static char fslog_cbuf_##_name[size]; \
\
static struct fslog_data fslog_data_##_name = { \
	.fslog_seq = { \
		.fslog_seq = 0, \
		.fslog_idx = 0, \
		.fslog_first_seq = 0, \
		.fslog_first_idx = 0, \
		.fslog_next_seq = 0, \
		.fslog_next_idx = 0, \
		.fslog_clear_seq = 0, \
		.fslog_clear_idx = 0, \
		.fslog_end_seq = -1 \
	}, \
	.fslog_meta = { \
		.ts_nsec = 0, \
		.len = 0, \
		.text_len = 0, \
		.pid = 0, \
		.tgid = 0, \
		.comm = {0, }, \
		.tgid_comm = {0, } \
	}, \
	.fslog_spinlock = __SPIN_LOCK_INITIALIZER(fslog_data_##_name.fslog_spinlock), \
	.fslog_wait_queue = __WAIT_QUEUE_HEAD_INITIALIZER(fslog_data_##_name.fslog_wait_queue), \
	.fslog_buf_len = size, \
	.fslog_cbuf = fslog_cbuf_##_name, \
}

static int fslog_release(struct inode *inode, struct file *file)
{
	struct fslog_data *fl_data = (struct fslog_data *)(file->private_data);
	return do_fslog(FSLOG_ACTION_CLOSE, fl_data, NULL, 0);
}

static ssize_t fslog_read(struct file *file, char __user *buf,
		                         size_t count, loff_t *ppos)
{
	struct fslog_data *fl_data = (struct fslog_data *)(file->private_data);
	return do_fslog(FSLOG_ACTION_READ_ALL, fl_data, buf, count);
}

static ssize_t fslog_write(struct file *file, const char __user *buf,
		                         size_t count, loff_t *ppos)
{
	struct fslog_data *fl_data = (struct fslog_data *)(file->private_data);
	return do_fslog(FSLOG_ACTION_WRITE, fl_data, (char __user *)buf, count);
}

loff_t fslog_llseek(struct file *file, loff_t offset, int whence)
{
	struct fslog_data *fl_data = (struct fslog_data *)(file->private_data);
	return (loff_t)do_fslog(FSLOG_ACTION_SIZE_BUFFER, fl_data, 0, 0);
}

#define DEFINE_FSLOG_OPERATION(_name) \
static int fslog_open_##_name(struct inode *inode, struct file *file) \
{ \
	file->f_flags |= O_NONBLOCK; \
	file->private_data = &fslog_data_##_name; \
	return do_fslog(FSLOG_ACTION_OPEN, &fslog_data_##_name, NULL, 0); \
} \
\
static const struct file_operations fslog_##_name##_operations = { \
	.read           = fslog_read, \
	.write          = fslog_write, \
	.open           = fslog_open_##_name, \
	.release        = fslog_release, \
	.llseek         = fslog_llseek, \
};

DEFINE_FSLOG_VARIABLE(dlog_mm, FSLOG_BUFLEN_DLOG_MM);
DEFINE_FSLOG_VARIABLE(dlog_efs, FSLOG_BUFLEN_DLOG_EFS);
DEFINE_FSLOG_VARIABLE(dlog_etc, FSLOG_BUFLEN_DLOG_ETC);
DEFINE_FSLOG_VARIABLE(dlog_rmdir, FSLOG_BUFLEN_DLOG_RMDIR);
DEFINE_FSLOG_VARIABLE(stlog, FSLOG_BUFLEN_STLOG);

DEFINE_FSLOG_OPERATION(dlog_mm);
DEFINE_FSLOG_OPERATION(dlog_efs);
DEFINE_FSLOG_OPERATION(dlog_etc);
DEFINE_FSLOG_OPERATION(dlog_rmdir);
DEFINE_FSLOG_OPERATION(stlog);

static const char FSLOG_VERSION_STR[] = "1.1.0\n";

static int fslog_version_show(struct seq_file *m, void *v)
{
	seq_printf(m, "%s", FSLOG_VERSION_STR);
	return 0;
}

static int fslog_version_open(struct inode *inode, struct file *file)
{
	return single_open(file, fslog_version_show, NULL);
}

static const struct file_operations fslog_ver_operations = {
	.open           = fslog_version_open,
	.read           = seq_read,
	.llseek         = seq_lseek,
	.release        = single_release,
};

#define DEFINE_FSLOG_CREATE(_name, log_name, file_mode) \
static void fslog_create_##_name(struct proc_dir_entry *fslog_dir) \
{ \
	proc_create(#log_name, file_mode, \
			fslog_dir, &fslog_##_name##_operations); \
}

DEFINE_FSLOG_CREATE(dlog_mm, dlog_mm, FSLOG_FILE_MODE_DLOG);
DEFINE_FSLOG_CREATE(dlog_efs, dlog_efs, FSLOG_FILE_MODE_DLOG);
DEFINE_FSLOG_CREATE(dlog_etc, dlog_etc, FSLOG_FILE_MODE_DLOG);
DEFINE_FSLOG_CREATE(dlog_rmdir, dlog_rmdir, FSLOG_FILE_MODE_DLOG);
DEFINE_FSLOG_CREATE(stlog, stlog, FSLOG_FILE_MODE_STLOG);

static int __init fslog_init(void)
{
	fslog_dir = proc_mkdir_mode("fslog", FSLOG_FILE_MODE_DIR, NULL);

	proc_create("version", FSLOG_FILE_MODE_VERSION, fslog_dir, &fslog_ver_operations);

	fslog_create_dlog_mm(fslog_dir);
	fslog_create_dlog_efs(fslog_dir);
	fslog_create_dlog_etc(fslog_dir);
	fslog_create_dlog_rmdir(fslog_dir);
	fslog_create_stlog(fslog_dir);

	/* To ensure sub-compatibility in versions below O OS */
	proc_symlink("stlog", NULL, "/proc/fslog/stlog");
	proc_symlink("stlog_version", NULL, "/proc/fslog/version");

	return 0;
}
module_init(fslog_init);

/* human readable text of the record */
static char *fslog_text(const struct fslog_metadata *msg)
{
	return (char *)msg + sizeof(struct fslog_metadata);
}

static struct fslog_metadata *fslog_buf_from_idx(char *fslog_cbuf, u32 idx)
{
	struct fslog_metadata *msg = (struct fslog_metadata *)(fslog_cbuf + idx);

	if (!msg->len)
		return (struct fslog_metadata *)fslog_cbuf;
	return msg;
}

static u32 fslog_next(char *fslog_cbuf, u32 idx)
{
	struct fslog_metadata *msg = (struct fslog_metadata *)(fslog_cbuf + idx);

	if (!msg->len) {
		msg = (struct fslog_metadata *)fslog_cbuf;
		return msg->len;
	}
	return idx + msg->len;
}

static void fslog_buf_store(const char *text, u16 text_len,
					struct fslog_data *fl_data,
					struct task_struct *owner)
{
	struct fslog_metadata *msg;
	struct fslog_sequence *fslog_seq = &(fl_data->fslog_seq);
	wait_queue_head_t *fslog_wait_queue = &(fl_data->fslog_wait_queue);
	char *fslog_cbuf = fl_data->fslog_cbuf;
	u32 size, pad_len, fslog_buf_len = fl_data->fslog_buf_len;
	struct task_struct *p = find_task_by_vpid(owner->tgid);

	/* number of '\0' padding bytes to next message */
	size = sizeof(struct fslog_metadata) + text_len;
	pad_len = (-size) & (FSLOG_BUF_ALIGN - 1);
	size += pad_len;

	while (fslog_seq->fslog_first_seq < fslog_seq->fslog_next_seq) {
		u32 free;

		if (fslog_seq->fslog_next_idx > fslog_seq->fslog_first_idx)
			free = max(fslog_buf_len - fslog_seq->fslog_next_idx,
							fslog_seq->fslog_first_idx);
		else
			free = fslog_seq->fslog_first_idx - fslog_seq->fslog_next_idx;

		if (free > size + sizeof(struct fslog_metadata))
			break;

		/* drop old messages until we have enough space */
		fslog_seq->fslog_first_idx = fslog_next(fslog_cbuf, fslog_seq->fslog_first_idx);
		fslog_seq->fslog_first_seq++;
	}

	if (fslog_seq->fslog_next_idx + size + sizeof(struct fslog_metadata) >= fslog_buf_len) {
		memset(fslog_cbuf + fslog_seq->fslog_next_idx, 0, sizeof(struct fslog_metadata));
		fslog_seq->fslog_next_idx = 0;
	}

	/* fill message */
	msg = (struct fslog_metadata *)(fslog_cbuf + fslog_seq->fslog_next_idx);
	memcpy(fslog_text(msg), text, text_len);
	msg->text_len = text_len;
	msg->pid = owner->pid;
	memcpy(msg->comm, owner->comm, TASK_COMM_LEN);
	if (p) {
		msg->tgid = owner->tgid;
		memcpy(msg->tgid_comm, p->comm, TASK_COMM_LEN);
	} else {
		msg->tgid = 0;
		msg->tgid_comm[0] = 0;
	}
	msg->ts_nsec = local_clock();
	msg->len = sizeof(struct fslog_metadata) + text_len + pad_len;

	/* insert message */
	fslog_seq->fslog_next_idx += msg->len;
	fslog_seq->fslog_next_seq++;
	wake_up_interruptible(fslog_wait_queue);

}

static size_t fslog_print_time(u64 ts, char *buf)
{
	unsigned long rem_nsec;

	rem_nsec = do_div(ts, 1000000000);

	if (!buf)
		return snprintf(NULL, 0, "[%5lu.000000] ", (unsigned long)ts);

	return sprintf(buf, "[%5lu.%06lu] ",
			(unsigned long)ts, rem_nsec / 1000);
}

static size_t fslog_print_pid(const struct fslog_metadata *msg, char *buf)
{
	if (!buf) {
		if (msg->pid == msg->tgid)
			return snprintf(NULL, 0, "[%15s, %5d] ",
					msg->comm, msg->pid);

		return snprintf(NULL, 0, "[%s(%d)|%s(%d)] ",
				msg->comm, msg->pid, msg->tgid_comm, msg->tgid);

	}

	if (msg->pid == msg->tgid)
		return sprintf(buf, "[%15s, %5d] ", msg->comm, msg->pid);
	
	return sprintf(buf, "[%s(%d)|%s(%d)] ",
			msg->comm, msg->pid, msg->tgid_comm, msg->tgid);
}

static size_t fslog_print_prefix(const struct fslog_metadata *msg, char *buf)
{
	size_t len = 0;

	len += fslog_print_time(msg->ts_nsec, buf ? buf + len : NULL);
	len += fslog_print_pid(msg, buf ? buf + len : NULL);
	return len;
}

static size_t fslog_print_text(const struct fslog_metadata *msg, char *buf, size_t size)
{
	const char *text = fslog_text(msg);
	size_t text_size = msg->text_len;
	bool prefix = true;
	bool newline = true;
	size_t len = 0;

	do {
		const char *next = memchr(text, '\n', text_size);
		size_t text_len;

		if (next) {
			text_len = next - text;
			next++;
			text_size -= next - text;
		} else {
			text_len = text_size;
		}

		if (buf) {
			if (fslog_print_prefix(msg, NULL) + text_len + 1 >= size - len)
				break;

			if (prefix)
				len += fslog_print_prefix(msg, buf + len);
			memcpy(buf + len, text, text_len);
			len += text_len;
			if (next || newline)
				buf[len++] = '\n';
		} else {
			/*  buffer size only calculation */
			if (prefix)
				len += fslog_print_prefix(msg, NULL);
			len += text_len;
			if (next || newline)
				len++;
		}

		prefix = true;
		text = next;
	} while (text);

	return len;
}

static int fslog_print_all(struct fslog_data *fl_data, char __user *buf, int size)
{
	char *text;
	int len = 0;
	struct fslog_sequence *fslog_seq = &(fl_data->fslog_seq);
	spinlock_t *fslog_spinlock = &(fl_data->fslog_spinlock);
	char *fslog_cbuf = fl_data->fslog_cbuf;
	u64 seq = fslog_seq->fslog_next_seq;
	u32 idx = fslog_seq->fslog_next_idx;

	text = kmalloc(FSLOG_BUF_LINE_MAX + S_PREFIX_MAX, GFP_KERNEL);
	if (!text)
		return -ENOMEM;
	spin_lock_irq(fslog_spinlock);

	if (fslog_seq->fslog_end_seq == -1)
		fslog_seq->fslog_end_seq = fslog_seq->fslog_next_seq;

	if (buf) {

		if (fslog_seq->fslog_clear_seq < fslog_seq->fslog_first_seq) {
			/* messages are gone, move to first available one */
			fslog_seq->fslog_clear_seq = fslog_seq->fslog_first_seq;
			fslog_seq->fslog_clear_idx = fslog_seq->fslog_first_idx;
		}

		seq = fslog_seq->fslog_clear_seq;
		idx = fslog_seq->fslog_clear_idx;

		while (seq < fslog_seq->fslog_end_seq) {
			struct fslog_metadata *msg =
					fslog_buf_from_idx(fslog_cbuf, idx);
			int textlen;

			textlen = fslog_print_text(msg, text,
						 FSLOG_BUF_LINE_MAX + S_PREFIX_MAX);
			if (textlen < 0) {
				len = textlen;
				break;
			} else if(len + textlen > size) {
				break;
			}
			idx = fslog_next(fslog_cbuf, idx);
			seq++;

			spin_unlock_irq(fslog_spinlock);
			if (copy_to_user(buf + len, text, textlen))
				len = -EFAULT;
			else
				len += textlen;
			spin_lock_irq(fslog_spinlock);

			if (seq < fslog_seq->fslog_first_seq) {
				/* messages are gone, move to next one */
				seq = fslog_seq->fslog_first_seq;
				idx = fslog_seq->fslog_first_idx;
			}
		}
	}

	fslog_seq->fslog_clear_seq = seq;
	fslog_seq->fslog_clear_idx = idx;

	spin_unlock_irq(fslog_spinlock);

	kfree(text);
	return len;
}

static int do_fslog(int type, struct fslog_data *fl_data, char __user *buf, int len)
{
	int error = 0;
	u32 fslog_buf_len = fl_data->fslog_buf_len;
	struct fslog_sequence *fslog_seq = &(fl_data->fslog_seq);

	switch (type) {
	case FSLOG_ACTION_CLOSE:	/* Close log */
		break;
	case FSLOG_ACTION_OPEN:	/* Open log */
		break;
	case FSLOG_ACTION_READ_ALL: /* cat /proc/fslog */ /* dumpstate */
		error = -EINVAL;
		if (!buf || len < 0)
			goto out;
		error = 0;
		if (!len)
			goto out;
		if (!access_ok(VERIFY_WRITE, buf, len)) {
			error = -EFAULT;
			goto out;
		}

		error = fslog_print_all(fl_data, buf, len);
		if (error == 0) {
			fslog_seq->fslog_clear_seq = fslog_seq->fslog_first_seq;
			fslog_seq->fslog_clear_idx = fslog_seq->fslog_first_idx;
			fslog_seq->fslog_end_seq = -1;
		}

		break;
	/* Size of the log buffer */
	case FSLOG_ACTION_WRITE:
		error = do_fslog_write(fl_data, (const char __user *)buf, len);
		break;
	case FSLOG_ACTION_SIZE_BUFFER:
		error = fslog_buf_len;
		break;
	default:
		error = -EINVAL;
		break;
	}
out:
	return error;
}

static int fslog(struct fslog_data *fl_data, const char *fmt, ...)
{
	va_list args;
	int r;

	va_start(args, fmt);
	r = vfslog(fl_data, fmt, args);

	va_end(args);

	return r;
}

static int do_fslog_write(struct fslog_data *fl_data, const char __user *buf, int len)
{
	int error = 0;
	char *kern_buf = 0;
	char *line = 0;

	if (!buf || len < 0)
		goto out;
	if (!len)
		goto out;
	if (len > FSLOG_BUF_LINE_MAX)
		return -EINVAL;

	kern_buf = kmalloc(len+1, GFP_KERNEL);
	if (kern_buf == NULL)
		return -ENOMEM;

	line = kern_buf;
	if (copy_from_user(line, buf, len)) {
		error = -EFAULT;
		goto out;
	}

	line[len] = '\0';
	error = fslog(fl_data, "%s", line);
	if ((line[len-1] == '\n') && (error == (len-1)))
		error++;
out:
	kfree(kern_buf);
	return error;

}

static int vfslog(struct fslog_data *fl_data, const char *fmt, va_list args)
{
	static char textbuf[FSLOG_BUF_LINE_MAX];
	char *text = textbuf;
	size_t text_len;
	unsigned long flags;
	int printed_len = 0;
	bool stored = false;
	spinlock_t *fslog_spinlock = &(fl_data->fslog_spinlock);

	local_irq_save(flags);

	spin_lock(fslog_spinlock);

	text_len = vscnprintf(text, sizeof(textbuf), fmt, args);

	/* mark and strip a trailing newline */
	if (text_len && text[text_len-1] == '\n')
		text_len--;

	if (!stored)
		fslog_buf_store(text, text_len, fl_data, current);

	printed_len += text_len;

	spin_unlock(fslog_spinlock);
	local_irq_restore(flags);

	return printed_len;
}

/*
 * fslog - print a deleted entry message
 * @fmt: format string
 */
#define DEFINE_FSLOG_FUNC(_name)\
int fslog_##_name(const char *fmt, ...)\
{\
	va_list args;\
	int r;\
\
	va_start(args, fmt);\
	r = vfslog(&fslog_data_##_name, fmt, args); \
\
	va_end(args);\
\
	return r;\
}

DEFINE_FSLOG_FUNC(dlog_mm);
DEFINE_FSLOG_FUNC(dlog_efs);
DEFINE_FSLOG_FUNC(dlog_etc);
DEFINE_FSLOG_FUNC(dlog_rmdir);
DEFINE_FSLOG_FUNC(stlog);