1/*
2 * f2fs IO tracer
3 *
4 * Copyright (c) 2014 Motorola Mobility
5 * Copyright (c) 2014 Jaegeuk Kim <jaegeuk@kernel.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/fs.h>
12#include <linux/f2fs_fs.h>
13#include <linux/sched.h>
14#include <linux/radix-tree.h>
15
16#include "f2fs.h"
17#include "trace.h"
18
19static RADIX_TREE(pids, GFP_ATOMIC);
20static spinlock_t pids_lock;
21static struct last_io_info last_io;
22
23static inline void __print_last_io(void)
24{
25	if (!last_io.len)
26		return;
27
28	trace_printk("%3x:%3x %4x %-16s %2x %5x %12x %4x\n",
29			last_io.major, last_io.minor,
30			last_io.pid, "----------------",
31			last_io.type,
32			last_io.fio.rw, last_io.fio.blk_addr,
33			last_io.len);
34	memset(&last_io, 0, sizeof(last_io));
35}
36
37static int __file_type(struct inode *inode, pid_t pid)
38{
39	if (f2fs_is_atomic_file(inode))
40		return __ATOMIC_FILE;
41	else if (f2fs_is_volatile_file(inode))
42		return __VOLATILE_FILE;
43	else if (S_ISDIR(inode->i_mode))
44		return __DIR_FILE;
45	else if (inode->i_ino == F2FS_NODE_INO(F2FS_I_SB(inode)))
46		return __NODE_FILE;
47	else if (inode->i_ino == F2FS_META_INO(F2FS_I_SB(inode)))
48		return __META_FILE;
49	else if (pid)
50		return __NORMAL_FILE;
51	else
52		return __MISC_FILE;
53}
54
55void f2fs_trace_pid(struct page *page)
56{
57	struct inode *inode = page->mapping->host;
58	pid_t pid = task_pid_nr(current);
59	void *p;
60
61	page->private = pid;
62
63	if (radix_tree_preload(GFP_NOFS))
64		return;
65
66	spin_lock(&pids_lock);
67	p = radix_tree_lookup(&pids, pid);
68	if (p == current)
69		goto out;
70	if (p)
71		radix_tree_delete(&pids, pid);
72
73	f2fs_radix_tree_insert(&pids, pid, current);
74
75	trace_printk("%3x:%3x %4x %-16s\n",
76			MAJOR(inode->i_sb->s_dev), MINOR(inode->i_sb->s_dev),
77			pid, current->comm);
78out:
79	spin_unlock(&pids_lock);
80	radix_tree_preload_end();
81}
82
83void f2fs_trace_ios(struct page *page, struct f2fs_io_info *fio, int flush)
84{
85	struct inode *inode;
86	pid_t pid;
87	int major, minor;
88
89	if (flush) {
90		__print_last_io();
91		return;
92	}
93
94	inode = page->mapping->host;
95	pid = page_private(page);
96
97	major = MAJOR(inode->i_sb->s_dev);
98	minor = MINOR(inode->i_sb->s_dev);
99
100	if (last_io.major == major && last_io.minor == minor &&
101			last_io.pid == pid &&
102			last_io.type == __file_type(inode, pid) &&
103			last_io.fio.rw == fio->rw &&
104			last_io.fio.blk_addr + last_io.len == fio->blk_addr) {
105		last_io.len++;
106		return;
107	}
108
109	__print_last_io();
110
111	last_io.major = major;
112	last_io.minor = minor;
113	last_io.pid = pid;
114	last_io.type = __file_type(inode, pid);
115	last_io.fio = *fio;
116	last_io.len = 1;
117	return;
118}
119
120void f2fs_build_trace_ios(void)
121{
122	spin_lock_init(&pids_lock);
123}
124
125#define PIDVEC_SIZE	128
126static unsigned int gang_lookup_pids(pid_t *results, unsigned long first_index,
127							unsigned int max_items)
128{
129	struct radix_tree_iter iter;
130	void **slot;
131	unsigned int ret = 0;
132
133	if (unlikely(!max_items))
134		return 0;
135
136	radix_tree_for_each_slot(slot, &pids, &iter, first_index) {
137		results[ret] = iter.index;
138		if (++ret == PIDVEC_SIZE)
139			break;
140	}
141	return ret;
142}
143
144void f2fs_destroy_trace_ios(void)
145{
146	pid_t pid[PIDVEC_SIZE];
147	pid_t next_pid = 0;
148	unsigned int found;
149
150	spin_lock(&pids_lock);
151	while ((found = gang_lookup_pids(pid, next_pid, PIDVEC_SIZE))) {
152		unsigned idx;
153
154		next_pid = pid[found - 1] + 1;
155		for (idx = 0; idx < found; idx++)
156			radix_tree_delete(&pids, pid[idx]);
157	}
158	spin_unlock(&pids_lock);
159}
160