]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/staging/dgrp/dgrp_mon_ops.c
dgrp procfs fixes, part 5: per-node files
[mirror_ubuntu-bionic-kernel.git] / drivers / staging / dgrp / dgrp_mon_ops.c
1 /*****************************************************************************
2 *
3 * Copyright 1999 Digi International (www.digi.com)
4 * James Puzzo <jamesp at digi dot com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
13 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 */
17
18 /*
19 *
20 * Filename:
21 *
22 * dgrp_mon_ops.c
23 *
24 * Description:
25 *
26 * Handle the file operations required for the "monitor" devices.
27 * Includes those functions required to register the "mon" devices
28 * in "/proc".
29 *
30 * Author:
31 *
32 * James A. Puzzo
33 *
34 */
35
36 #include <linux/module.h>
37 #include <linux/tty.h>
38 #include <linux/sched.h>
39 #include <asm/unaligned.h>
40 #include <linux/proc_fs.h>
41 #include <linux/uaccess.h>
42
43 #include "dgrp_common.h"
44
45 /* File operation declarations */
46 static int dgrp_mon_open(struct inode *, struct file *);
47 static int dgrp_mon_release(struct inode *, struct file *);
48 static ssize_t dgrp_mon_read(struct file *, char __user *, size_t, loff_t *);
49 static long dgrp_mon_ioctl(struct file *file, unsigned int cmd,
50 unsigned long arg);
51
52 const struct file_operations dgrp_mon_ops = {
53 .owner = THIS_MODULE,
54 .read = dgrp_mon_read,
55 .unlocked_ioctl = dgrp_mon_ioctl,
56 .open = dgrp_mon_open,
57 .release = dgrp_mon_release,
58 };
59
60 /**
61 * dgrp_mon_open() -- open /proc/dgrp/ports device for a PortServer
62 * @inode: struct inode *
63 * @file: struct file *
64 *
65 * Open function to open the /proc/dgrp/ports device for a PortServer.
66 */
67 static int dgrp_mon_open(struct inode *inode, struct file *file)
68 {
69 struct nd_struct *nd;
70 struct proc_dir_entry *de;
71 struct timeval tv;
72 uint32_t time;
73 u8 *buf;
74 int rtn;
75
76 rtn = try_module_get(THIS_MODULE);
77 if (!rtn)
78 return -ENXIO;
79
80 rtn = 0;
81
82 if (!capable(CAP_SYS_ADMIN)) {
83 rtn = -EPERM;
84 goto done;
85 }
86
87 /*
88 * Make sure that the "private_data" field hasn't already been used.
89 */
90 if (file->private_data) {
91 rtn = -EINVAL;
92 goto done;
93 }
94
95 /*
96 * Get the node pointer, and fail if it doesn't exist.
97 */
98 de = PDE(inode);
99 if (!de) {
100 rtn = -ENXIO;
101 goto done;
102 }
103
104 nd = (struct nd_struct *)de->data;
105 if (!nd) {
106 rtn = -ENXIO;
107 goto done;
108 }
109
110 file->private_data = (void *) nd;
111
112 /*
113 * Allocate the monitor buffer.
114 */
115
116 /*
117 * Grab the MON lock.
118 */
119 down(&nd->nd_mon_semaphore);
120
121 if (nd->nd_mon_buf) {
122 rtn = -EBUSY;
123 goto done_up;
124 }
125
126 nd->nd_mon_buf = kmalloc(MON_MAX, GFP_KERNEL);
127
128 if (!nd->nd_mon_buf) {
129 rtn = -ENOMEM;
130 goto done_up;
131 }
132
133 /*
134 * Enter an RPDUMP file header into the buffer.
135 */
136
137 buf = nd->nd_mon_buf;
138
139 strcpy(buf, RPDUMP_MAGIC);
140 buf += strlen(buf) + 1;
141
142 do_gettimeofday(&tv);
143
144 /*
145 * tv.tv_sec might be a 64 bit quantity. Pare
146 * it down to 32 bits before attempting to encode
147 * it.
148 */
149 time = (uint32_t) (tv.tv_sec & 0xffffffff);
150
151 put_unaligned_be32(time, buf);
152 put_unaligned_be16(0, buf + 4);
153 buf += 6;
154
155 if (nd->nd_tx_module) {
156 buf[0] = RPDUMP_CLIENT;
157 put_unaligned_be32(0, buf + 1);
158 put_unaligned_be16(1, buf + 5);
159 buf[7] = 0xf0 + nd->nd_tx_module;
160 buf += 8;
161 }
162
163 if (nd->nd_rx_module) {
164 buf[0] = RPDUMP_SERVER;
165 put_unaligned_be32(0, buf + 1);
166 put_unaligned_be16(1, buf + 5);
167 buf[7] = 0xf0 + nd->nd_rx_module;
168 buf += 8;
169 }
170
171 nd->nd_mon_out = 0;
172 nd->nd_mon_in = buf - nd->nd_mon_buf;
173 nd->nd_mon_lbolt = jiffies;
174
175 done_up:
176 up(&nd->nd_mon_semaphore);
177
178 done:
179 if (rtn)
180 module_put(THIS_MODULE);
181 return rtn;
182 }
183
184
185 /**
186 * dgrp_mon_release() - Close the MON device for a particular PortServer
187 * @inode: struct inode *
188 * @file: struct file *
189 */
190 static int dgrp_mon_release(struct inode *inode, struct file *file)
191 {
192 struct nd_struct *nd;
193
194 /*
195 * Get the node pointer, and quit if it doesn't exist.
196 */
197 nd = (struct nd_struct *)(file->private_data);
198 if (!nd)
199 goto done;
200
201 /*
202 * Free the monitor buffer.
203 */
204
205 down(&nd->nd_mon_semaphore);
206
207 kfree(nd->nd_mon_buf);
208 nd->nd_mon_buf = NULL;
209 nd->nd_mon_out = nd->nd_mon_in;
210
211 /*
212 * Wakeup any thread waiting for buffer space.
213 */
214
215 if (nd->nd_mon_flag & MON_WAIT_SPACE) {
216 nd->nd_mon_flag &= ~MON_WAIT_SPACE;
217 wake_up_interruptible(&nd->nd_mon_wqueue);
218 }
219
220 up(&nd->nd_mon_semaphore);
221
222 /*
223 * Make sure there is no thread in the middle of writing a packet.
224 */
225 down(&nd->nd_net_semaphore);
226 up(&nd->nd_net_semaphore);
227
228 done:
229 module_put(THIS_MODULE);
230 file->private_data = NULL;
231 return 0;
232 }
233
234 /**
235 * dgrp_mon_read() -- Copy data from the monitoring buffer to the user
236 */
237 static ssize_t dgrp_mon_read(struct file *file, char __user *buf, size_t count,
238 loff_t *ppos)
239 {
240 struct nd_struct *nd;
241 int r;
242 int offset = 0;
243 int res = 0;
244 ssize_t rtn;
245
246 /*
247 * Get the node pointer, and quit if it doesn't exist.
248 */
249 nd = (struct nd_struct *)(file->private_data);
250 if (!nd)
251 return -ENXIO;
252
253 /*
254 * Wait for some data to appear in the buffer.
255 */
256
257 down(&nd->nd_mon_semaphore);
258
259 for (;;) {
260 res = (nd->nd_mon_in - nd->nd_mon_out) & MON_MASK;
261
262 if (res)
263 break;
264
265 nd->nd_mon_flag |= MON_WAIT_DATA;
266
267 up(&nd->nd_mon_semaphore);
268
269 /*
270 * Go to sleep waiting until the condition becomes true.
271 */
272 rtn = wait_event_interruptible(nd->nd_mon_wqueue,
273 ((nd->nd_mon_flag & MON_WAIT_DATA) == 0));
274
275 if (rtn)
276 return rtn;
277
278 down(&nd->nd_mon_semaphore);
279 }
280
281 /*
282 * Read whatever is there.
283 */
284
285 if (res > count)
286 res = count;
287
288 r = MON_MAX - nd->nd_mon_out;
289
290 if (r <= res) {
291 rtn = copy_to_user((void __user *)buf,
292 nd->nd_mon_buf + nd->nd_mon_out, r);
293 if (rtn) {
294 up(&nd->nd_mon_semaphore);
295 return -EFAULT;
296 }
297
298 nd->nd_mon_out = 0;
299 res -= r;
300 offset = r;
301 }
302
303 rtn = copy_to_user((void __user *) buf + offset,
304 nd->nd_mon_buf + nd->nd_mon_out, res);
305 if (rtn) {
306 up(&nd->nd_mon_semaphore);
307 return -EFAULT;
308 }
309
310 nd->nd_mon_out += res;
311
312 *ppos += res;
313
314 up(&nd->nd_mon_semaphore);
315
316 /*
317 * Wakeup any thread waiting for buffer space.
318 */
319
320 if (nd->nd_mon_flag & MON_WAIT_SPACE) {
321 nd->nd_mon_flag &= ~MON_WAIT_SPACE;
322 wake_up_interruptible(&nd->nd_mon_wqueue);
323 }
324
325 return res;
326 }
327
328 /* ioctl is not valid on monitor device */
329 static long dgrp_mon_ioctl(struct file *file, unsigned int cmd,
330 unsigned long arg)
331 {
332 return -EINVAL;
333 }