]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/orangefs/devorangefs-req.c
if ORANGEFS_VFS_OP_FILE_IO request had been given up, don't bother waiting
[mirror_ubuntu-zesty-kernel.git] / fs / orangefs / devorangefs-req.c
CommitLineData
5db11c21
MM
1/*
2 * (C) 2001 Clemson University and The University of Chicago
3 *
4 * Changes by Acxiom Corporation to add protocol version to kernel
5 * communication, Copyright Acxiom Corporation, 2005.
6 *
7 * See COPYING in top-level directory.
8 */
9
10#include "protocol.h"
575e9461
MM
11#include "orangefs-kernel.h"
12#include "orangefs-dev-proto.h"
13#include "orangefs-bufmap.h"
5db11c21
MM
14
15#include <linux/debugfs.h>
16#include <linux/slab.h>
17
18/* this file implements the /dev/pvfs2-req device node */
19
20static int open_access_count;
21
22#define DUMP_DEVICE_ERROR() \
23do { \
24 gossip_err("*****************************************************\n");\
8bb8aefd 25 gossip_err("ORANGEFS Device Error: You cannot open the device file "); \
5db11c21 26 gossip_err("\n/dev/%s more than once. Please make sure that\nthere " \
8bb8aefd 27 "are no ", ORANGEFS_REQDEVICE_NAME); \
5db11c21
MM
28 gossip_err("instances of a program using this device\ncurrently " \
29 "running. (You must verify this!)\n"); \
30 gossip_err("For example, you can use the lsof program as follows:\n");\
31 gossip_err("'lsof | grep %s' (run this as root)\n", \
8bb8aefd 32 ORANGEFS_REQDEVICE_NAME); \
5db11c21
MM
33 gossip_err(" open_access_count = %d\n", open_access_count); \
34 gossip_err("*****************************************************\n");\
35} while (0)
36
37static int hash_func(__u64 tag, int table_size)
38{
2c590d5f 39 return do_div(tag, (unsigned int)table_size);
5db11c21
MM
40}
41
8bb8aefd 42static void orangefs_devreq_add_op(struct orangefs_kernel_op_s *op)
5db11c21
MM
43{
44 int index = hash_func(op->tag, hash_table_size);
45
5db11c21 46 list_add_tail(&op->list, &htable_ops_in_progress[index]);
5db11c21
MM
47}
48
8bb8aefd 49static struct orangefs_kernel_op_s *orangefs_devreq_remove_op(__u64 tag)
5db11c21 50{
8bb8aefd 51 struct orangefs_kernel_op_s *op, *next;
5db11c21
MM
52 int index;
53
54 index = hash_func(tag, hash_table_size);
55
56 spin_lock(&htable_ops_in_progress_lock);
57 list_for_each_entry_safe(op,
58 next,
59 &htable_ops_in_progress[index],
60 list) {
ed42fe05
AV
61 if (op->tag == tag && !op_state_purged(op)) {
62 list_del_init(&op->list);
63 get_op(op); /* increase ref count. */
5db11c21
MM
64 spin_unlock(&htable_ops_in_progress_lock);
65 return op;
66 }
67 }
68
69 spin_unlock(&htable_ops_in_progress_lock);
70 return NULL;
71}
72
8bb8aefd 73static int orangefs_devreq_open(struct inode *inode, struct file *file)
5db11c21
MM
74{
75 int ret = -EINVAL;
76
77 if (!(file->f_flags & O_NONBLOCK)) {
97f10027
MM
78 gossip_err("%s: device cannot be opened in blocking mode\n",
79 __func__);
5db11c21
MM
80 goto out;
81 }
82 ret = -EACCES;
97f10027 83 gossip_debug(GOSSIP_DEV_DEBUG, "client-core: opening device\n");
5db11c21
MM
84 mutex_lock(&devreq_mutex);
85
86 if (open_access_count == 0) {
fee25ce1 87 open_access_count = 1;
fb6d2526 88 ret = 0;
5db11c21
MM
89 } else {
90 DUMP_DEVICE_ERROR();
91 }
92 mutex_unlock(&devreq_mutex);
93
94out:
95
96 gossip_debug(GOSSIP_DEV_DEBUG,
97 "pvfs2-client-core: open device complete (ret = %d)\n",
98 ret);
99 return ret;
100}
101
97f10027 102/* Function for read() callers into the device */
8bb8aefd 103static ssize_t orangefs_devreq_read(struct file *file,
5db11c21
MM
104 char __user *buf,
105 size_t count, loff_t *offset)
106{
8bb8aefd
YL
107 struct orangefs_kernel_op_s *op, *temp;
108 __s32 proto_ver = ORANGEFS_KERNEL_PROTO_VERSION;
109 static __s32 magic = ORANGEFS_DEVREQ_MAGIC;
110 struct orangefs_kernel_op_s *cur_op = NULL;
24c8d080 111 unsigned long ret;
5db11c21 112
24c8d080 113 /* We do not support blocking IO. */
5db11c21 114 if (!(file->f_flags & O_NONBLOCK)) {
97f10027
MM
115 gossip_err("%s: blocking read from client-core.\n",
116 __func__);
5db11c21 117 return -EINVAL;
24c8d080
MB
118 }
119
120 /*
a762ae6d 121 * The client will do an ioctl to find MAX_DEV_REQ_UPSIZE, then
24c8d080
MB
122 * always read with that size buffer.
123 */
a762ae6d 124 if (count != MAX_DEV_REQ_UPSIZE) {
24c8d080
MB
125 gossip_err("orangefs: client-core tried to read wrong size\n");
126 return -EINVAL;
127 }
128
ed42fe05 129restart:
24c8d080 130 /* Get next op (if any) from top of list. */
8bb8aefd
YL
131 spin_lock(&orangefs_request_list_lock);
132 list_for_each_entry_safe(op, temp, &orangefs_request_list, list) {
24c8d080
MB
133 __s32 fsid;
134 /* This lock is held past the end of the loop when we break. */
135 spin_lock(&op->lock);
ed42fe05
AV
136 if (unlikely(op_state_purged(op))) {
137 spin_unlock(&op->lock);
138 continue;
139 }
24c8d080
MB
140
141 fsid = fsid_of_op(op);
8bb8aefd 142 if (fsid != ORANGEFS_FS_ID_NULL) {
24c8d080
MB
143 int ret;
144 /* Skip ops whose filesystem needs to be mounted. */
145 ret = fs_mount_pending(fsid);
146 if (ret == 1) {
5db11c21 147 gossip_debug(GOSSIP_DEV_DEBUG,
24c8d080
MB
148 "orangefs: skipping op tag %llu %s\n",
149 llu(op->tag), get_opname_string(op));
150 spin_unlock(&op->lock);
151 continue;
97f10027
MM
152 /*
153 * Skip ops whose filesystem we don't know about unless
154 * it is being mounted.
155 */
24c8d080
MB
156 /* XXX: is there a better way to detect this? */
157 } else if (ret == -1 &&
97f10027
MM
158 !(op->upcall.type ==
159 ORANGEFS_VFS_OP_FS_MOUNT ||
160 op->upcall.type ==
161 ORANGEFS_VFS_OP_GETATTR)) {
24c8d080
MB
162 gossip_debug(GOSSIP_DEV_DEBUG,
163 "orangefs: skipping op tag %llu %s\n",
164 llu(op->tag), get_opname_string(op));
165 gossip_err(
166 "orangefs: ERROR: fs_mount_pending %d\n",
167 fsid);
168 spin_unlock(&op->lock);
5db11c21 169 continue;
5db11c21
MM
170 }
171 }
24c8d080
MB
172 /*
173 * Either this op does not pertain to a filesystem, is mounting
174 * a filesystem, or pertains to a mounted filesystem. Let it
175 * through.
176 */
177 cur_op = op;
178 break;
179 }
180
181 /*
182 * At this point we either have a valid op and can continue or have not
183 * found an op and must ask the client to try again later.
184 */
185 if (!cur_op) {
8bb8aefd 186 spin_unlock(&orangefs_request_list_lock);
24c8d080 187 return -EAGAIN;
5db11c21
MM
188 }
189
24c8d080
MB
190 gossip_debug(GOSSIP_DEV_DEBUG, "orangefs: reading op tag %llu %s\n",
191 llu(cur_op->tag), get_opname_string(cur_op));
5db11c21 192
24c8d080
MB
193 /*
194 * Such an op should never be on the list in the first place. If so, we
195 * will abort.
196 */
197 if (op_state_in_progress(cur_op) || op_state_serviced(cur_op)) {
198 gossip_err("orangefs: ERROR: Current op already queued.\n");
199 list_del(&cur_op->list);
5db11c21 200 spin_unlock(&cur_op->lock);
8bb8aefd 201 spin_unlock(&orangefs_request_list_lock);
24c8d080 202 return -EAGAIN;
5db11c21 203 }
ed42fe05
AV
204 list_del_init(&cur_op->list);
205 get_op(op);
8bb8aefd 206 spin_unlock(&orangefs_request_list_lock);
ed42fe05 207
24c8d080
MB
208 spin_unlock(&cur_op->lock);
209
210 /* Push the upcall out. */
211 ret = copy_to_user(buf, &proto_ver, sizeof(__s32));
212 if (ret != 0)
213 goto error;
214 ret = copy_to_user(buf+sizeof(__s32), &magic, sizeof(__s32));
215 if (ret != 0)
216 goto error;
217 ret = copy_to_user(buf+2 * sizeof(__s32), &cur_op->tag, sizeof(__u64));
218 if (ret != 0)
219 goto error;
220 ret = copy_to_user(buf+2*sizeof(__s32)+sizeof(__u64), &cur_op->upcall,
8bb8aefd 221 sizeof(struct orangefs_upcall_s));
24c8d080
MB
222 if (ret != 0)
223 goto error;
224
ed42fe05
AV
225 spin_lock(&htable_ops_in_progress_lock);
226 spin_lock(&cur_op->lock);
227 if (unlikely(op_state_given_up(cur_op))) {
228 spin_unlock(&cur_op->lock);
229 spin_unlock(&htable_ops_in_progress_lock);
230 op_release(cur_op);
231 goto restart;
232 }
233
234 /*
235 * Set the operation to be in progress and move it between lists since
236 * it has been sent to the client.
237 */
238 set_op_state_inprogress(cur_op);
239 orangefs_devreq_add_op(cur_op);
240 spin_unlock(&cur_op->lock);
241 spin_unlock(&htable_ops_in_progress_lock);
242 op_release(cur_op);
243
24c8d080 244 /* The client only asks to read one size buffer. */
a762ae6d 245 return MAX_DEV_REQ_UPSIZE;
24c8d080
MB
246error:
247 /*
248 * We were unable to copy the op data to the client. Put the op back in
249 * list. If client has crashed, the op will be purged later when the
250 * device is released.
251 */
252 gossip_err("orangefs: Failed to copy data to user space\n");
8bb8aefd 253 spin_lock(&orangefs_request_list_lock);
24c8d080 254 spin_lock(&cur_op->lock);
ed42fe05
AV
255 if (likely(!op_state_given_up(cur_op))) {
256 set_op_state_waiting(cur_op);
257 list_add(&cur_op->list, &orangefs_request_list);
258 }
24c8d080 259 spin_unlock(&cur_op->lock);
8bb8aefd 260 spin_unlock(&orangefs_request_list_lock);
ed42fe05 261 op_release(cur_op);
24c8d080 262 return -EFAULT;
5db11c21
MM
263}
264
97f10027 265/*
b3ae4755
MM
266 * Function for writev() callers into the device.
267 *
268 * Userspace should have written:
269 * - __u32 version
270 * - __u32 magic
271 * - __u64 tag
272 * - struct orangefs_downcall_s
273 * - trailer buffer (in the case of READDIR operations)
97f10027 274 */
b3ae4755
MM
275static ssize_t orangefs_devreq_write_iter(struct kiocb *iocb,
276 struct iov_iter *iter)
5db11c21 277{
b3ae4755 278 ssize_t ret;
8bb8aefd 279 struct orangefs_kernel_op_s *op = NULL;
b3ae4755
MM
280 struct {
281 __u32 version;
282 __u32 magic;
283 __u64 tag;
284 } head;
285 int total = ret = iov_iter_count(iter);
286 int n;
287 int downcall_size = sizeof(struct orangefs_downcall_s);
288 int head_size = sizeof(head);
289
290 gossip_debug(GOSSIP_DEV_DEBUG, "%s: total:%d: ret:%zd:\n",
291 __func__,
292 total,
293 ret);
5db11c21 294
b3ae4755 295 if (total < MAX_DEV_REQ_DOWNSIZE) {
cf0c2771 296 gossip_err("%s: total:%d: must be at least:%u:\n",
b3ae4755
MM
297 __func__,
298 total,
cf0c2771 299 (unsigned int) MAX_DEV_REQ_DOWNSIZE);
ed42fe05 300 return -EFAULT;
5db11c21 301 }
b3ae4755
MM
302
303 n = copy_from_iter(&head, head_size, iter);
304 if (n < head_size) {
305 gossip_err("%s: failed to copy head.\n", __func__);
ed42fe05 306 return -EFAULT;
97f10027 307 }
b3ae4755
MM
308
309 if (head.version < ORANGEFS_MINIMUM_USERSPACE_VERSION) {
310 gossip_err("%s: userspace claims version"
311 "%d, minimum version required: %d.\n",
312 __func__,
313 head.version,
314 ORANGEFS_MINIMUM_USERSPACE_VERSION);
ed42fe05 315 return -EPROTO;
5db11c21 316 }
5db11c21 317
b3ae4755
MM
318 if (head.magic != ORANGEFS_DEVREQ_MAGIC) {
319 gossip_err("Error: Device magic number does not match.\n");
ed42fe05 320 return -EPROTO;
b3ae4755 321 }
5db11c21 322
b3ae4755
MM
323 op = orangefs_devreq_remove_op(head.tag);
324 if (!op) {
325 gossip_err("WARNING: No one's waiting for tag %llu\n",
326 llu(head.tag));
ed42fe05 327 return ret;
b3ae4755 328 }
5db11c21 329
b3ae4755
MM
330 n = copy_from_iter(&op->downcall, downcall_size, iter);
331 if (n != downcall_size) {
332 gossip_err("%s: failed to copy downcall.\n", __func__);
b3ae4755 333 ret = -EFAULT;
ed42fe05 334 goto Broken;
5db11c21
MM
335 }
336
b3ae4755
MM
337 if (op->downcall.status)
338 goto wakeup;
97f10027 339
b3ae4755
MM
340 /*
341 * We've successfully peeled off the head and the downcall.
342 * Something has gone awry if total doesn't equal the
343 * sum of head_size, downcall_size and trailer_size.
344 */
345 if ((head_size + downcall_size + op->downcall.trailer_size) != total) {
346 gossip_err("%s: funky write, head_size:%d"
347 ": downcall_size:%d: trailer_size:%lld"
348 ": total size:%d:\n",
349 __func__,
350 head_size,
351 downcall_size,
352 op->downcall.trailer_size,
353 total);
b3ae4755 354 ret = -EFAULT;
ed42fe05 355 goto Broken;
b3ae4755 356 }
97f10027 357
b3ae4755
MM
358 /* Only READDIR operations should have trailers. */
359 if ((op->downcall.type != ORANGEFS_VFS_OP_READDIR) &&
360 (op->downcall.trailer_size != 0)) {
361 gossip_err("%s: %x operation with trailer.",
362 __func__,
363 op->downcall.type);
b3ae4755 364 ret = -EFAULT;
ed42fe05 365 goto Broken;
b3ae4755 366 }
97f10027 367
b3ae4755
MM
368 /* READDIR operations should always have trailers. */
369 if ((op->downcall.type == ORANGEFS_VFS_OP_READDIR) &&
370 (op->downcall.trailer_size == 0)) {
371 gossip_err("%s: %x operation with no trailer.",
372 __func__,
373 op->downcall.type);
b3ae4755 374 ret = -EFAULT;
ed42fe05 375 goto Broken;
b3ae4755 376 }
97f10027 377
b3ae4755
MM
378 if (op->downcall.type != ORANGEFS_VFS_OP_READDIR)
379 goto wakeup;
5db11c21 380
b3ae4755
MM
381 op->downcall.trailer_buf =
382 vmalloc(op->downcall.trailer_size);
383 if (op->downcall.trailer_buf == NULL) {
384 gossip_err("%s: failed trailer vmalloc.\n",
385 __func__);
b3ae4755 386 ret = -ENOMEM;
ed42fe05 387 goto Broken;
b3ae4755
MM
388 }
389 memset(op->downcall.trailer_buf, 0, op->downcall.trailer_size);
390 n = copy_from_iter(op->downcall.trailer_buf,
391 op->downcall.trailer_size,
392 iter);
393 if (n != op->downcall.trailer_size) {
394 gossip_err("%s: failed to copy trailer.\n", __func__);
395 vfree(op->downcall.trailer_buf);
b3ae4755 396 ret = -EFAULT;
ed42fe05 397 goto Broken;
b3ae4755 398 }
97f10027 399
b3ae4755 400wakeup:
97f10027 401
b3ae4755
MM
402 /*
403 * If this operation is an I/O operation we need to wait
404 * for all data to be copied before we can return to avoid
405 * buffer corruption and races that can pull the buffers
406 * out from under us.
407 *
408 * Essentially we're synchronizing with other parts of the
409 * vfs implicitly by not allowing the user space
410 * application reading/writing this device to return until
411 * the buffers are done being used.
412 */
413 if (op->downcall.type == ORANGEFS_VFS_OP_FILE_IO) {
b3ae4755 414 DEFINE_WAIT(wait_entry);
97f10027
MM
415
416 /*
b3ae4755
MM
417 * tell the vfs op waiting on a waitqueue
418 * that this op is done
97f10027 419 */
b3ae4755 420 spin_lock(&op->lock);
4f55e397
AV
421 if (unlikely(op_state_given_up(op))) {
422 spin_unlock(&op->lock);
423 goto out;
424 }
425 set_op_state_serviced(op);
b3ae4755 426 spin_unlock(&op->lock);
5db11c21 427
b3ae4755 428 while (1) {
5db11c21 429 spin_lock(&op->lock);
b3ae4755
MM
430 prepare_to_wait_exclusive(
431 &op->io_completion_waitq,
432 &wait_entry,
433 TASK_INTERRUPTIBLE);
434 if (op->io_completed) {
5db11c21 435 spin_unlock(&op->lock);
5db11c21
MM
436 break;
437 }
ce6c414e 438 spin_unlock(&op->lock);
4f55e397
AV
439 if (unlikely(signal_pending(current))) {
440 gossip_debug(GOSSIP_DEV_DEBUG,
441 "%s: signal on I/O wait, aborting\n",
442 __func__);
443 break;
b3ae4755 444 }
5db11c21 445
4f55e397
AV
446 if (!schedule_timeout(op_timeout_secs * HZ)) {
447 gossip_debug(GOSSIP_DEV_DEBUG,
448 "%s: timed out.\n",
449 __func__);
450 break;
451 }
5db11c21 452 }
b3ae4755
MM
453
454 spin_lock(&op->lock);
455 finish_wait(&op->io_completion_waitq, &wait_entry);
456 spin_unlock(&op->lock);
5db11c21 457 } else {
b3ae4755
MM
458 /*
459 * tell the vfs op waiting on a waitqueue that
831d0949 460 * this op is done -
b3ae4755
MM
461 * for every other operation (i.e. non-I/O), we need to
462 * wake up the callers for downcall completion
463 * notification
464 */
831d0949 465 spin_lock(&op->lock);
ed42fe05
AV
466 if (!op_state_given_up(op))
467 set_op_state_serviced(op);
831d0949 468 spin_unlock(&op->lock);
5db11c21 469 }
b3ae4755 470out:
ed42fe05 471 op_release(op);
b3ae4755 472 return ret;
ed42fe05
AV
473
474Broken:
475 spin_lock(&op->lock);
476 if (!op_state_given_up(op)) {
477 op->downcall.status = ret;
478 set_op_state_serviced(op);
479 }
480 spin_unlock(&op->lock);
481 goto out;
5db11c21
MM
482}
483
484/* Returns whether any FS are still pending remounted */
485static int mark_all_pending_mounts(void)
486{
487 int unmounted = 1;
8bb8aefd 488 struct orangefs_sb_info_s *orangefs_sb = NULL;
5db11c21 489
8bb8aefd
YL
490 spin_lock(&orangefs_superblocks_lock);
491 list_for_each_entry(orangefs_sb, &orangefs_superblocks, list) {
5db11c21 492 /* All of these file system require a remount */
8bb8aefd 493 orangefs_sb->mount_pending = 1;
5db11c21
MM
494 unmounted = 0;
495 }
8bb8aefd 496 spin_unlock(&orangefs_superblocks_lock);
5db11c21
MM
497 return unmounted;
498}
499
500/*
501 * Determine if a given file system needs to be remounted or not
502 * Returns -1 on error
503 * 0 if already mounted
504 * 1 if needs remount
505 */
506int fs_mount_pending(__s32 fsid)
507{
508 int mount_pending = -1;
8bb8aefd 509 struct orangefs_sb_info_s *orangefs_sb = NULL;
5db11c21 510
8bb8aefd
YL
511 spin_lock(&orangefs_superblocks_lock);
512 list_for_each_entry(orangefs_sb, &orangefs_superblocks, list) {
513 if (orangefs_sb->fs_id == fsid) {
514 mount_pending = orangefs_sb->mount_pending;
5db11c21
MM
515 break;
516 }
517 }
8bb8aefd 518 spin_unlock(&orangefs_superblocks_lock);
5db11c21
MM
519 return mount_pending;
520}
521
522/*
523 * NOTE: gets called when the last reference to this device is dropped.
524 * Using the open_access_count variable, we enforce a reference count
525 * on this file so that it can be opened by only one process at a time.
526 * the devreq_mutex is used to make sure all i/o has completed
8bb8aefd 527 * before we call orangefs_bufmap_finalize, and similar such tricky
5db11c21
MM
528 * situations
529 */
8bb8aefd 530static int orangefs_devreq_release(struct inode *inode, struct file *file)
5db11c21
MM
531{
532 int unmounted = 0;
533
534 gossip_debug(GOSSIP_DEV_DEBUG,
535 "%s:pvfs2-client-core: exiting, closing device\n",
536 __func__);
537
538 mutex_lock(&devreq_mutex);
7d221485 539 if (orangefs_get_bufmap_init())
90d26aa8 540 orangefs_bufmap_finalize();
5db11c21 541
fee25ce1 542 open_access_count = -1;
5db11c21
MM
543
544 unmounted = mark_all_pending_mounts();
8bb8aefd 545 gossip_debug(GOSSIP_DEV_DEBUG, "ORANGEFS Device Close: Filesystem(s) %s\n",
5db11c21 546 (unmounted ? "UNMOUNTED" : "MOUNTED"));
5db11c21
MM
547
548 /*
549 * Walk through the list of ops in the request list, mark them
550 * as purged and wake them up.
551 */
552 purge_waiting_ops();
553 /*
554 * Walk through the hash table of in progress operations; mark
555 * them as purged and wake them up
556 */
557 purge_inprogress_ops();
558 gossip_debug(GOSSIP_DEV_DEBUG,
559 "pvfs2-client-core: device close complete\n");
fee25ce1
AV
560 open_access_count = 0;
561 mutex_unlock(&devreq_mutex);
5db11c21
MM
562 return 0;
563}
564
565int is_daemon_in_service(void)
566{
567 int in_service;
568
569 /*
570 * What this function does is checks if client-core is alive
571 * based on the access count we maintain on the device.
572 */
573 mutex_lock(&devreq_mutex);
574 in_service = open_access_count == 1 ? 0 : -EIO;
575 mutex_unlock(&devreq_mutex);
576 return in_service;
577}
578
579static inline long check_ioctl_command(unsigned int command)
580{
581 /* Check for valid ioctl codes */
8bb8aefd 582 if (_IOC_TYPE(command) != ORANGEFS_DEV_MAGIC) {
5db11c21
MM
583 gossip_err("device ioctl magic numbers don't match! Did you rebuild pvfs2-client-core/libpvfs2? [cmd %x, magic %x != %x]\n",
584 command,
585 _IOC_TYPE(command),
8bb8aefd 586 ORANGEFS_DEV_MAGIC);
5db11c21
MM
587 return -EINVAL;
588 }
589 /* and valid ioctl commands */
8bb8aefd 590 if (_IOC_NR(command) >= ORANGEFS_DEV_MAXNR || _IOC_NR(command) <= 0) {
5db11c21 591 gossip_err("Invalid ioctl command number [%d >= %d]\n",
8bb8aefd 592 _IOC_NR(command), ORANGEFS_DEV_MAXNR);
5db11c21
MM
593 return -ENOIOCTLCMD;
594 }
595 return 0;
596}
597
598static long dispatch_ioctl_command(unsigned int command, unsigned long arg)
599{
8bb8aefd 600 static __s32 magic = ORANGEFS_DEVREQ_MAGIC;
a762ae6d
MB
601 static __s32 max_up_size = MAX_DEV_REQ_UPSIZE;
602 static __s32 max_down_size = MAX_DEV_REQ_DOWNSIZE;
8bb8aefd 603 struct ORANGEFS_dev_map_desc user_desc;
5db11c21
MM
604 int ret = 0;
605 struct dev_mask_info_s mask_info = { 0 };
606 struct dev_mask2_info_s mask2_info = { 0, 0 };
607 int upstream_kmod = 1;
608 struct list_head *tmp = NULL;
8bb8aefd 609 struct orangefs_sb_info_s *orangefs_sb = NULL;
5db11c21
MM
610
611 /* mtmoore: add locking here */
612
613 switch (command) {
8bb8aefd 614 case ORANGEFS_DEV_GET_MAGIC:
5db11c21
MM
615 return ((put_user(magic, (__s32 __user *) arg) == -EFAULT) ?
616 -EIO :
617 0);
8bb8aefd 618 case ORANGEFS_DEV_GET_MAX_UPSIZE:
5db11c21
MM
619 return ((put_user(max_up_size,
620 (__s32 __user *) arg) == -EFAULT) ?
621 -EIO :
622 0);
8bb8aefd 623 case ORANGEFS_DEV_GET_MAX_DOWNSIZE:
5db11c21
MM
624 return ((put_user(max_down_size,
625 (__s32 __user *) arg) == -EFAULT) ?
626 -EIO :
627 0);
8bb8aefd 628 case ORANGEFS_DEV_MAP:
5db11c21 629 ret = copy_from_user(&user_desc,
8bb8aefd 630 (struct ORANGEFS_dev_map_desc __user *)
5db11c21 631 arg,
8bb8aefd 632 sizeof(struct ORANGEFS_dev_map_desc));
7d221485 633 if (orangefs_get_bufmap_init()) {
90d26aa8
MB
634 return -EINVAL;
635 } else {
636 return ret ?
637 -EIO :
638 orangefs_bufmap_initialize(&user_desc);
639 }
8bb8aefd 640 case ORANGEFS_DEV_REMOUNT_ALL:
5db11c21 641 gossip_debug(GOSSIP_DEV_DEBUG,
97f10027
MM
642 "%s: got ORANGEFS_DEV_REMOUNT_ALL\n",
643 __func__);
5db11c21
MM
644
645 /*
8bb8aefd 646 * remount all mounted orangefs volumes to regain the lost
5db11c21
MM
647 * dynamic mount tables (if any) -- NOTE: this is done
648 * without keeping the superblock list locked due to the
649 * upcall/downcall waiting. also, the request semaphore is
650 * used to ensure that no operations will be serviced until
651 * all of the remounts are serviced (to avoid ops between
652 * mounts to fail)
653 */
654 ret = mutex_lock_interruptible(&request_mutex);
655 if (ret < 0)
656 return ret;
657 gossip_debug(GOSSIP_DEV_DEBUG,
97f10027
MM
658 "%s: priority remount in progress\n",
659 __func__);
8bb8aefd
YL
660 list_for_each(tmp, &orangefs_superblocks) {
661 orangefs_sb =
97f10027
MM
662 list_entry(tmp,
663 struct orangefs_sb_info_s,
664 list);
8bb8aefd 665 if (orangefs_sb && (orangefs_sb->sb)) {
5db11c21 666 gossip_debug(GOSSIP_DEV_DEBUG,
97f10027
MM
667 "%s: Remounting SB %p\n",
668 __func__,
8bb8aefd 669 orangefs_sb);
5db11c21 670
8bb8aefd 671 ret = orangefs_remount(orangefs_sb->sb);
5db11c21
MM
672 if (ret) {
673 gossip_debug(GOSSIP_DEV_DEBUG,
674 "SB %p remount failed\n",
8bb8aefd 675 orangefs_sb);
97f10027 676 break;
5db11c21
MM
677 }
678 }
679 }
680 gossip_debug(GOSSIP_DEV_DEBUG,
97f10027
MM
681 "%s: priority remount complete\n",
682 __func__);
5db11c21
MM
683 mutex_unlock(&request_mutex);
684 return ret;
685
8bb8aefd 686 case ORANGEFS_DEV_UPSTREAM:
5db11c21
MM
687 ret = copy_to_user((void __user *)arg,
688 &upstream_kmod,
689 sizeof(upstream_kmod));
690
691 if (ret != 0)
692 return -EIO;
693 else
694 return ret;
695
8bb8aefd 696 case ORANGEFS_DEV_CLIENT_MASK:
5db11c21
MM
697 ret = copy_from_user(&mask2_info,
698 (void __user *)arg,
699 sizeof(struct dev_mask2_info_s));
700
701 if (ret != 0)
702 return -EIO;
703
704 client_debug_mask.mask1 = mask2_info.mask1_value;
705 client_debug_mask.mask2 = mask2_info.mask2_value;
706
707 pr_info("%s: client debug mask has been been received "
708 ":%llx: :%llx:\n",
709 __func__,
710 (unsigned long long)client_debug_mask.mask1,
711 (unsigned long long)client_debug_mask.mask2);
712
713 return ret;
714
8bb8aefd 715 case ORANGEFS_DEV_CLIENT_STRING:
5db11c21
MM
716 ret = copy_from_user(&client_debug_array_string,
717 (void __user *)arg,
8bb8aefd 718 ORANGEFS_MAX_DEBUG_STRING_LEN);
5db11c21 719 if (ret != 0) {
97f10027 720 pr_info("%s: CLIENT_STRING: copy_from_user failed\n",
5db11c21
MM
721 __func__);
722 return -EIO;
723 }
724
97f10027 725 pr_info("%s: client debug array string has been received.\n",
5db11c21
MM
726 __func__);
727
728 if (!help_string_initialized) {
729
730 /* Free the "we don't know yet" default string... */
731 kfree(debug_help_string);
732
733 /* build a proper debug help string */
734 if (orangefs_prepare_debugfs_help_string(0)) {
97f10027 735 gossip_err("%s: no debug help string \n",
5db11c21
MM
736 __func__);
737 return -EIO;
738 }
739
740 /* Replace the boilerplate boot-time debug-help file. */
741 debugfs_remove(help_file_dentry);
742
743 help_file_dentry =
744 debugfs_create_file(
745 ORANGEFS_KMOD_DEBUG_HELP_FILE,
746 0444,
747 debug_dir,
748 debug_help_string,
749 &debug_help_fops);
750
751 if (!help_file_dentry) {
752 gossip_err("%s: debugfs_create_file failed for"
753 " :%s:!\n",
754 __func__,
755 ORANGEFS_KMOD_DEBUG_HELP_FILE);
756 return -EIO;
757 }
758 }
759
760 debug_mask_to_string(&client_debug_mask, 1);
761
762 debugfs_remove(client_debug_dentry);
763
8bb8aefd 764 orangefs_client_debug_init();
5db11c21
MM
765
766 help_string_initialized++;
767
768 return ret;
769
8bb8aefd 770 case ORANGEFS_DEV_DEBUG:
5db11c21
MM
771 ret = copy_from_user(&mask_info,
772 (void __user *)arg,
773 sizeof(mask_info));
774
775 if (ret != 0)
776 return -EIO;
777
778 if (mask_info.mask_type == KERNEL_MASK) {
779 if ((mask_info.mask_value == 0)
780 && (kernel_mask_set_mod_init)) {
781 /*
782 * the kernel debug mask was set when the
783 * kernel module was loaded; don't override
784 * it if the client-core was started without
8bb8aefd 785 * a value for ORANGEFS_KMODMASK.
5db11c21
MM
786 */
787 return 0;
788 }
789 debug_mask_to_string(&mask_info.mask_value,
790 mask_info.mask_type);
791 gossip_debug_mask = mask_info.mask_value;
97f10027 792 pr_info("%s: kernel debug mask has been modified to "
5db11c21 793 ":%s: :%llx:\n",
97f10027 794 __func__,
5db11c21
MM
795 kernel_debug_string,
796 (unsigned long long)gossip_debug_mask);
797 } else if (mask_info.mask_type == CLIENT_MASK) {
798 debug_mask_to_string(&mask_info.mask_value,
799 mask_info.mask_type);
97f10027 800 pr_info("%s: client debug mask has been modified to"
5db11c21 801 ":%s: :%llx:\n",
97f10027 802 __func__,
5db11c21
MM
803 client_debug_string,
804 llu(mask_info.mask_value));
805 } else {
806 gossip_lerr("Invalid mask type....\n");
807 return -EINVAL;
808 }
809
810 return ret;
811
812 default:
813 return -ENOIOCTLCMD;
814 }
815 return -ENOIOCTLCMD;
816}
817
8bb8aefd 818static long orangefs_devreq_ioctl(struct file *file,
5db11c21
MM
819 unsigned int command, unsigned long arg)
820{
821 long ret;
822
823 /* Check for properly constructed commands */
824 ret = check_ioctl_command(command);
825 if (ret < 0)
826 return (int)ret;
827
828 return (int)dispatch_ioctl_command(command, arg);
829}
830
831#ifdef CONFIG_COMPAT /* CONFIG_COMPAT is in .config */
832
8bb8aefd
YL
833/* Compat structure for the ORANGEFS_DEV_MAP ioctl */
834struct ORANGEFS_dev_map_desc32 {
5db11c21
MM
835 compat_uptr_t ptr;
836 __s32 total_size;
837 __s32 size;
838 __s32 count;
839};
840
841static unsigned long translate_dev_map26(unsigned long args, long *error)
842{
8bb8aefd 843 struct ORANGEFS_dev_map_desc32 __user *p32 = (void __user *)args;
5db11c21
MM
844 /*
845 * Depending on the architecture, allocate some space on the
846 * user-call-stack based on our expected layout.
847 */
8bb8aefd 848 struct ORANGEFS_dev_map_desc __user *p =
5db11c21 849 compat_alloc_user_space(sizeof(*p));
84d02150 850 compat_uptr_t addr;
5db11c21
MM
851
852 *error = 0;
853 /* get the ptr from the 32 bit user-space */
854 if (get_user(addr, &p32->ptr))
855 goto err;
856 /* try to put that into a 64-bit layout */
857 if (put_user(compat_ptr(addr), &p->ptr))
858 goto err;
859 /* copy the remaining fields */
860 if (copy_in_user(&p->total_size, &p32->total_size, sizeof(__s32)))
861 goto err;
862 if (copy_in_user(&p->size, &p32->size, sizeof(__s32)))
863 goto err;
864 if (copy_in_user(&p->count, &p32->count, sizeof(__s32)))
865 goto err;
866 return (unsigned long)p;
867err:
868 *error = -EFAULT;
869 return 0;
870}
871
872/*
873 * 32 bit user-space apps' ioctl handlers when kernel modules
874 * is compiled as a 64 bit one
875 */
8bb8aefd 876static long orangefs_devreq_compat_ioctl(struct file *filp, unsigned int cmd,
5db11c21
MM
877 unsigned long args)
878{
879 long ret;
880 unsigned long arg = args;
881
882 /* Check for properly constructed commands */
883 ret = check_ioctl_command(cmd);
884 if (ret < 0)
885 return ret;
8bb8aefd 886 if (cmd == ORANGEFS_DEV_MAP) {
5db11c21
MM
887 /*
888 * convert the arguments to what we expect internally
889 * in kernel space
890 */
891 arg = translate_dev_map26(args, &ret);
892 if (ret < 0) {
893 gossip_err("Could not translate dev map\n");
894 return ret;
895 }
896 }
897 /* no other ioctl requires translation */
898 return dispatch_ioctl_command(cmd, arg);
899}
900
2c590d5f
MM
901#endif /* CONFIG_COMPAT is in .config */
902
5db11c21 903/* the assigned character device major number */
8bb8aefd 904static int orangefs_dev_major;
5db11c21
MM
905
906/*
8bb8aefd 907 * Initialize orangefs device specific state:
5db11c21
MM
908 * Must be called at module load time only
909 */
8bb8aefd 910int orangefs_dev_init(void)
5db11c21 911{
8bb8aefd
YL
912 /* register orangefs-req device */
913 orangefs_dev_major = register_chrdev(0,
914 ORANGEFS_REQDEVICE_NAME,
915 &orangefs_devreq_file_operations);
916 if (orangefs_dev_major < 0) {
5db11c21
MM
917 gossip_debug(GOSSIP_DEV_DEBUG,
918 "Failed to register /dev/%s (error %d)\n",
8bb8aefd 919 ORANGEFS_REQDEVICE_NAME, orangefs_dev_major);
8bb8aefd 920 return orangefs_dev_major;
5db11c21
MM
921 }
922
923 gossip_debug(GOSSIP_DEV_DEBUG,
924 "*** /dev/%s character device registered ***\n",
8bb8aefd 925 ORANGEFS_REQDEVICE_NAME);
5db11c21 926 gossip_debug(GOSSIP_DEV_DEBUG, "'mknod /dev/%s c %d 0'.\n",
8bb8aefd 927 ORANGEFS_REQDEVICE_NAME, orangefs_dev_major);
5db11c21
MM
928 return 0;
929}
930
8bb8aefd 931void orangefs_dev_cleanup(void)
5db11c21 932{
8bb8aefd 933 unregister_chrdev(orangefs_dev_major, ORANGEFS_REQDEVICE_NAME);
5db11c21
MM
934 gossip_debug(GOSSIP_DEV_DEBUG,
935 "*** /dev/%s character device unregistered ***\n",
8bb8aefd 936 ORANGEFS_REQDEVICE_NAME);
5db11c21
MM
937}
938
8bb8aefd 939static unsigned int orangefs_devreq_poll(struct file *file,
5db11c21
MM
940 struct poll_table_struct *poll_table)
941{
942 int poll_revent_mask = 0;
943
83595db0 944 poll_wait(file, &orangefs_request_list_waitq, poll_table);
5db11c21 945
83595db0
AV
946 if (!list_empty(&orangefs_request_list))
947 poll_revent_mask |= POLL_IN;
5db11c21
MM
948 return poll_revent_mask;
949}
950
8bb8aefd 951const struct file_operations orangefs_devreq_file_operations = {
5db11c21 952 .owner = THIS_MODULE,
8bb8aefd
YL
953 .read = orangefs_devreq_read,
954 .write_iter = orangefs_devreq_write_iter,
955 .open = orangefs_devreq_open,
956 .release = orangefs_devreq_release,
957 .unlocked_ioctl = orangefs_devreq_ioctl,
5db11c21
MM
958
959#ifdef CONFIG_COMPAT /* CONFIG_COMPAT is in .config */
8bb8aefd 960 .compat_ioctl = orangefs_devreq_compat_ioctl,
5db11c21 961#endif
8bb8aefd 962 .poll = orangefs_devreq_poll
5db11c21 963};