usb: dwc3: add functions to set force mode
[firefly-linux-kernel-4.4.55.git] / drivers / misc / uid_stat.c
1 /* drivers/misc/uid_stat.c
2  *
3  * Copyright (C) 2008 - 2009 Google, Inc.
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15
16 #include <linux/atomic.h>
17
18 #include <linux/err.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/list.h>
22 #include <linux/proc_fs.h>
23 #include <linux/seq_file.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock.h>
26 #include <linux/stat.h>
27 #include <linux/uid_stat.h>
28 #include <net/activity_stats.h>
29
30 static DEFINE_SPINLOCK(uid_lock);
31 static LIST_HEAD(uid_list);
32 static struct proc_dir_entry *parent;
33
34 struct uid_stat {
35         struct list_head link;
36         uid_t uid;
37         atomic_t tcp_rcv;
38         atomic_t tcp_snd;
39 };
40
41 static struct uid_stat *find_uid_stat(uid_t uid) {
42         struct uid_stat *entry;
43
44         list_for_each_entry(entry, &uid_list, link) {
45                 if (entry->uid == uid) {
46                         return entry;
47                 }
48         }
49         return NULL;
50 }
51
52 static int uid_stat_atomic_int_show(struct seq_file *m, void *v)
53 {
54         unsigned int bytes;
55         atomic_t *counter = m->private;
56
57         bytes = (unsigned int) (atomic_read(counter) + INT_MIN);
58         seq_printf(m, "%u\n", bytes);
59         return seq_has_overflowed(m) ? -ENOSPC : 0;
60 }
61
62 static int uid_stat_read_atomic_int_open(struct inode *inode, struct file *file)
63 {
64         return single_open(file, uid_stat_atomic_int_show, PDE_DATA(inode));
65 }
66
67 static const struct file_operations uid_stat_read_atomic_int_fops = {
68         .open           = uid_stat_read_atomic_int_open,
69         .read           = seq_read,
70         .llseek         = seq_lseek,
71         .release        = seq_release,
72 };
73
74 /* Create a new entry for tracking the specified uid. */
75 static struct uid_stat *create_stat(uid_t uid) {
76         struct uid_stat *new_uid;
77         /* Create the uid stat struct and append it to the list. */
78         new_uid = kmalloc(sizeof(struct uid_stat), GFP_ATOMIC);
79         if (!new_uid)
80                 return NULL;
81
82         new_uid->uid = uid;
83         /* Counters start at INT_MIN, so we can track 4GB of network traffic. */
84         atomic_set(&new_uid->tcp_rcv, INT_MIN);
85         atomic_set(&new_uid->tcp_snd, INT_MIN);
86
87         list_add_tail(&new_uid->link, &uid_list);
88         return new_uid;
89 }
90
91 static void create_stat_proc(struct uid_stat *new_uid)
92 {
93         char uid_s[32];
94         struct proc_dir_entry *entry;
95         sprintf(uid_s, "%d", new_uid->uid);
96         entry = proc_mkdir(uid_s, parent);
97
98         /* Keep reference to uid_stat so we know what uid to read stats from. */
99         proc_create_data("tcp_snd", S_IRUGO, entry,
100                          &uid_stat_read_atomic_int_fops, &new_uid->tcp_snd);
101
102         proc_create_data("tcp_rcv", S_IRUGO, entry,
103                          &uid_stat_read_atomic_int_fops, &new_uid->tcp_rcv);
104 }
105
106 static struct uid_stat *find_or_create_uid_stat(uid_t uid)
107 {
108         struct uid_stat *entry;
109         unsigned long flags;
110         spin_lock_irqsave(&uid_lock, flags);
111         entry = find_uid_stat(uid);
112         if (entry) {
113                 spin_unlock_irqrestore(&uid_lock, flags);
114                 return entry;
115         }
116         entry = create_stat(uid);
117         spin_unlock_irqrestore(&uid_lock, flags);
118         if (entry)
119                 create_stat_proc(entry);
120         return entry;
121 }
122
123 int uid_stat_tcp_snd(uid_t uid, int size) {
124         struct uid_stat *entry;
125         activity_stats_update();
126         entry = find_or_create_uid_stat(uid);
127         if (!entry)
128                 return -1;
129         atomic_add(size, &entry->tcp_snd);
130         return 0;
131 }
132
133 int uid_stat_tcp_rcv(uid_t uid, int size) {
134         struct uid_stat *entry;
135         activity_stats_update();
136         entry = find_or_create_uid_stat(uid);
137         if (!entry)
138                 return -1;
139         atomic_add(size, &entry->tcp_rcv);
140         return 0;
141 }
142
143 static int __init uid_stat_init(void)
144 {
145         parent = proc_mkdir("uid_stat", NULL);
146         if (!parent) {
147                 pr_err("uid_stat: failed to create proc entry\n");
148                 return -1;
149         }
150         return 0;
151 }
152
153 __initcall(uid_stat_init);