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