blob: b182b025db86dd12eb59ed597f6b7b1e865878e2 [file] [log] [blame]
Mike Marshall5db11c22015-07-17 10:38:12 -04001/*
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"
Mike Marshall575e9462015-12-04 12:56:14 -050011#include "orangefs-kernel.h"
12#include "orangefs-dev-proto.h"
13#include "orangefs-bufmap.h"
Mike Marshall5db11c22015-07-17 10:38:12 -040014
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");\
Yi Liu8bb8aef2015-11-24 15:12:14 -050025 gossip_err("ORANGEFS Device Error: You cannot open the device file "); \
Mike Marshall5db11c22015-07-17 10:38:12 -040026 gossip_err("\n/dev/%s more than once. Please make sure that\nthere " \
Yi Liu8bb8aef2015-11-24 15:12:14 -050027 "are no ", ORANGEFS_REQDEVICE_NAME); \
Mike Marshall5db11c22015-07-17 10:38:12 -040028 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", \
Yi Liu8bb8aef2015-11-24 15:12:14 -050032 ORANGEFS_REQDEVICE_NAME); \
Mike Marshall5db11c22015-07-17 10:38:12 -040033 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{
Mike Marshall2c590d52015-07-24 10:37:15 -040039 return do_div(tag, (unsigned int)table_size);
Mike Marshall5db11c22015-07-17 10:38:12 -040040}
41
Yi Liu8bb8aef2015-11-24 15:12:14 -050042static void orangefs_devreq_add_op(struct orangefs_kernel_op_s *op)
Mike Marshall5db11c22015-07-17 10:38:12 -040043{
44 int index = hash_func(op->tag, hash_table_size);
45
46 spin_lock(&htable_ops_in_progress_lock);
47 list_add_tail(&op->list, &htable_ops_in_progress[index]);
48 spin_unlock(&htable_ops_in_progress_lock);
49}
50
Yi Liu8bb8aef2015-11-24 15:12:14 -050051static struct orangefs_kernel_op_s *orangefs_devreq_remove_op(__u64 tag)
Mike Marshall5db11c22015-07-17 10:38:12 -040052{
Yi Liu8bb8aef2015-11-24 15:12:14 -050053 struct orangefs_kernel_op_s *op, *next;
Mike Marshall5db11c22015-07-17 10:38:12 -040054 int index;
55
56 index = hash_func(tag, hash_table_size);
57
58 spin_lock(&htable_ops_in_progress_lock);
59 list_for_each_entry_safe(op,
60 next,
61 &htable_ops_in_progress[index],
62 list) {
63 if (op->tag == tag) {
64 list_del(&op->list);
65 spin_unlock(&htable_ops_in_progress_lock);
66 return op;
67 }
68 }
69
70 spin_unlock(&htable_ops_in_progress_lock);
71 return NULL;
72}
73
Yi Liu8bb8aef2015-11-24 15:12:14 -050074static int orangefs_devreq_open(struct inode *inode, struct file *file)
Mike Marshall5db11c22015-07-17 10:38:12 -040075{
76 int ret = -EINVAL;
77
78 if (!(file->f_flags & O_NONBLOCK)) {
Mike Marshall97f10022015-12-11 16:45:03 -050079 gossip_err("%s: device cannot be opened in blocking mode\n",
80 __func__);
Mike Marshall5db11c22015-07-17 10:38:12 -040081 goto out;
82 }
83 ret = -EACCES;
Mike Marshall97f10022015-12-11 16:45:03 -050084 gossip_debug(GOSSIP_DEV_DEBUG, "client-core: opening device\n");
Mike Marshall5db11c22015-07-17 10:38:12 -040085 mutex_lock(&devreq_mutex);
86
87 if (open_access_count == 0) {
88 ret = generic_file_open(inode, file);
89 if (ret == 0)
90 open_access_count++;
91 } else {
92 DUMP_DEVICE_ERROR();
93 }
94 mutex_unlock(&devreq_mutex);
95
96out:
97
98 gossip_debug(GOSSIP_DEV_DEBUG,
99 "pvfs2-client-core: open device complete (ret = %d)\n",
100 ret);
101 return ret;
102}
103
Mike Marshall97f10022015-12-11 16:45:03 -0500104/* Function for read() callers into the device */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500105static ssize_t orangefs_devreq_read(struct file *file,
Mike Marshall5db11c22015-07-17 10:38:12 -0400106 char __user *buf,
107 size_t count, loff_t *offset)
108{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500109 struct orangefs_kernel_op_s *op, *temp;
110 __s32 proto_ver = ORANGEFS_KERNEL_PROTO_VERSION;
111 static __s32 magic = ORANGEFS_DEVREQ_MAGIC;
112 struct orangefs_kernel_op_s *cur_op = NULL;
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500113 unsigned long ret;
Mike Marshall5db11c22015-07-17 10:38:12 -0400114
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500115 /* We do not support blocking IO. */
Mike Marshall5db11c22015-07-17 10:38:12 -0400116 if (!(file->f_flags & O_NONBLOCK)) {
Mike Marshall97f10022015-12-11 16:45:03 -0500117 gossip_err("%s: blocking read from client-core.\n",
118 __func__);
Mike Marshall5db11c22015-07-17 10:38:12 -0400119 return -EINVAL;
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500120 }
121
122 /*
123 * The client will do an ioctl to find MAX_ALIGNED_DEV_REQ_UPSIZE, then
124 * always read with that size buffer.
125 */
126 if (count != MAX_ALIGNED_DEV_REQ_UPSIZE) {
127 gossip_err("orangefs: client-core tried to read wrong size\n");
128 return -EINVAL;
129 }
130
131 /* Get next op (if any) from top of list. */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500132 spin_lock(&orangefs_request_list_lock);
133 list_for_each_entry_safe(op, temp, &orangefs_request_list, list) {
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500134 __s32 fsid;
135 /* This lock is held past the end of the loop when we break. */
136 spin_lock(&op->lock);
137
138 fsid = fsid_of_op(op);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500139 if (fsid != ORANGEFS_FS_ID_NULL) {
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500140 int ret;
141 /* Skip ops whose filesystem needs to be mounted. */
142 ret = fs_mount_pending(fsid);
143 if (ret == 1) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400144 gossip_debug(GOSSIP_DEV_DEBUG,
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500145 "orangefs: skipping op tag %llu %s\n",
146 llu(op->tag), get_opname_string(op));
147 spin_unlock(&op->lock);
Mike Marshall5db11c22015-07-17 10:38:12 -0400148 continue;
Mike Marshall97f10022015-12-11 16:45:03 -0500149 /*
150 * Skip ops whose filesystem we don't know about unless
151 * it is being mounted.
152 */
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500153 /* XXX: is there a better way to detect this? */
154 } else if (ret == -1 &&
Mike Marshall97f10022015-12-11 16:45:03 -0500155 !(op->upcall.type ==
156 ORANGEFS_VFS_OP_FS_MOUNT ||
157 op->upcall.type ==
158 ORANGEFS_VFS_OP_GETATTR)) {
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500159 gossip_debug(GOSSIP_DEV_DEBUG,
160 "orangefs: skipping op tag %llu %s\n",
161 llu(op->tag), get_opname_string(op));
162 gossip_err(
163 "orangefs: ERROR: fs_mount_pending %d\n",
164 fsid);
165 spin_unlock(&op->lock);
166 continue;
Mike Marshall5db11c22015-07-17 10:38:12 -0400167 }
168 }
Mike Marshall5db11c22015-07-17 10:38:12 -0400169 /*
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500170 * Either this op does not pertain to a filesystem, is mounting
171 * a filesystem, or pertains to a mounted filesystem. Let it
172 * through.
Mike Marshall5db11c22015-07-17 10:38:12 -0400173 */
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500174 cur_op = op;
175 break;
Mike Marshall5db11c22015-07-17 10:38:12 -0400176 }
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500177
178 /*
179 * At this point we either have a valid op and can continue or have not
180 * found an op and must ask the client to try again later.
181 */
182 if (!cur_op) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500183 spin_unlock(&orangefs_request_list_lock);
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500184 return -EAGAIN;
185 }
186
187 gossip_debug(GOSSIP_DEV_DEBUG, "orangefs: reading op tag %llu %s\n",
188 llu(cur_op->tag), get_opname_string(cur_op));
189
190 /*
191 * Such an op should never be on the list in the first place. If so, we
192 * will abort.
193 */
194 if (op_state_in_progress(cur_op) || op_state_serviced(cur_op)) {
195 gossip_err("orangefs: ERROR: Current op already queued.\n");
196 list_del(&cur_op->list);
197 spin_unlock(&cur_op->lock);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500198 spin_unlock(&orangefs_request_list_lock);
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500199 return -EAGAIN;
200 }
201
202 /*
203 * Set the operation to be in progress and move it between lists since
204 * it has been sent to the client.
205 */
206 set_op_state_inprogress(cur_op);
207
208 list_del(&cur_op->list);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500209 spin_unlock(&orangefs_request_list_lock);
210 orangefs_devreq_add_op(cur_op);
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500211 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,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500224 sizeof(struct orangefs_upcall_s));
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500225 if (ret != 0)
226 goto error;
227
228 /* The client only asks to read one size buffer. */
229 return MAX_ALIGNED_DEV_REQ_UPSIZE;
230error:
231 /*
232 * We were unable to copy the op data to the client. Put the op back in
233 * list. If client has crashed, the op will be purged later when the
234 * device is released.
235 */
236 gossip_err("orangefs: Failed to copy data to user space\n");
Yi Liu8bb8aef2015-11-24 15:12:14 -0500237 spin_lock(&orangefs_request_list_lock);
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500238 spin_lock(&cur_op->lock);
239 set_op_state_waiting(cur_op);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500240 orangefs_devreq_remove_op(cur_op->tag);
241 list_add(&cur_op->list, &orangefs_request_list);
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500242 spin_unlock(&cur_op->lock);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500243 spin_unlock(&orangefs_request_list_lock);
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500244 return -EFAULT;
Mike Marshall5db11c22015-07-17 10:38:12 -0400245}
246
Mike Marshall97f10022015-12-11 16:45:03 -0500247/*
248 * Function for writev() callers into the device. Readdir related
249 * operations have an extra iovec containing info about objects
250 * contained in directories.
251 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500252static ssize_t orangefs_devreq_writev(struct file *file,
Mike Marshall5db11c22015-07-17 10:38:12 -0400253 const struct iovec *iov,
254 size_t count,
255 loff_t *offset)
256{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500257 struct orangefs_kernel_op_s *op = NULL;
Mike Marshall5db11c22015-07-17 10:38:12 -0400258 void *buffer = NULL;
259 void *ptr = NULL;
260 unsigned long i = 0;
Mike Marshall97f10022015-12-11 16:45:03 -0500261 int num_remaining = MAX_ALIGNED_DEV_REQ_DOWNSIZE;
262 int ret = 0;
263 /* num elements in iovec without trailer */
264 int notrailer_count = 4;
265 /*
266 * If there's a trailer, its iov index will be equal to
267 * notrailer_count.
268 */
269 int trailer_index = notrailer_count;
Mike Marshall5db11c22015-07-17 10:38:12 -0400270 int payload_size = 0;
Mike Marshall97f10022015-12-11 16:45:03 -0500271 int returned_downcall_size = 0;
Mike Marshall5db11c22015-07-17 10:38:12 -0400272 __s32 magic = 0;
273 __s32 proto_ver = 0;
274 __u64 tag = 0;
275 ssize_t total_returned_size = 0;
276
Mike Marshall97f10022015-12-11 16:45:03 -0500277 /*
278 * There will always be at least notrailer_count iovecs, and
279 * when there's a trailer, one more than notrailer_count. Check
280 * count's sanity.
281 */
Mike Marshall5db11c22015-07-17 10:38:12 -0400282 if (count != notrailer_count && count != (notrailer_count + 1)) {
Mike Marshall97f10022015-12-11 16:45:03 -0500283 gossip_err("%s: count:%zu: notrailer_count :%d:\n",
284 __func__,
Mike Marshall5db11c22015-07-17 10:38:12 -0400285 count,
286 notrailer_count);
287 return -EPROTO;
288 }
Mike Marshall5db11c22015-07-17 10:38:12 -0400289
Mike Marshall97f10022015-12-11 16:45:03 -0500290
291 /* Copy the non-trailer iovec data into a device request buffer. */
292 buffer = dev_req_alloc();
293 if (!buffer) {
294 gossip_err("%s: dev_req_alloc failed.\n", __func__);
295 return -ENOMEM;
296 }
297 ptr = buffer;
Mike Marshall5db11c22015-07-17 10:38:12 -0400298 for (i = 0; i < notrailer_count; i++) {
299 if (iov[i].iov_len > num_remaining) {
300 gossip_err
301 ("writev error: Freeing buffer and returning\n");
302 dev_req_release(buffer);
303 return -EMSGSIZE;
304 }
305 ret = copy_from_user(ptr, iov[i].iov_base, iov[i].iov_len);
306 if (ret) {
307 gossip_err("Failed to copy data from user space\n");
308 dev_req_release(buffer);
309 return -EIO;
310 }
311 num_remaining -= iov[i].iov_len;
312 ptr += iov[i].iov_len;
313 payload_size += iov[i].iov_len;
314 }
315 total_returned_size = payload_size;
316
317 /* these elements are currently 8 byte aligned (8 bytes for (version +
318 * magic) 8 bytes for tag). If you add another element, either
319 * make it 8 bytes big, or use get_unaligned when asigning.
320 */
321 ptr = buffer;
Mike Marshall97f10022015-12-11 16:45:03 -0500322 proto_ver = *((__s32 *) ptr); /* unused */
Mike Marshall5db11c22015-07-17 10:38:12 -0400323 ptr += sizeof(__s32);
324
325 magic = *((__s32 *) ptr);
326 ptr += sizeof(__s32);
327
328 tag = *((__u64 *) ptr);
329 ptr += sizeof(__u64);
330
Yi Liu8bb8aef2015-11-24 15:12:14 -0500331 if (magic != ORANGEFS_DEVREQ_MAGIC) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400332 gossip_err("Error: Device magic number does not match.\n");
333 dev_req_release(buffer);
334 return -EPROTO;
335 }
336
Yi Liu8bb8aef2015-11-24 15:12:14 -0500337 op = orangefs_devreq_remove_op(tag);
Mike Marshall5db11c22015-07-17 10:38:12 -0400338 if (op) {
339 /* Increase ref count! */
340 get_op(op);
Mike Marshall97f10022015-12-11 16:45:03 -0500341
342 /* calculate the size of the returned downcall. */
343 returned_downcall_size =
344 payload_size - (2 * sizeof(__s32) + sizeof(__u64));
345
346 /* copy the passed in downcall into the op */
347 if (returned_downcall_size ==
348 sizeof(struct orangefs_downcall_s)) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400349 memcpy(&op->downcall,
350 ptr,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500351 sizeof(struct orangefs_downcall_s));
Mike Marshall97f10022015-12-11 16:45:03 -0500352 } else {
353 gossip_err("%s: returned downcall size:%d: \n",
354 __func__,
355 returned_downcall_size);
356 dev_req_release(buffer);
357 put_op(op);
358 return -EMSGSIZE;
Mike Marshall5db11c22015-07-17 10:38:12 -0400359 }
360
Mike Marshall97f10022015-12-11 16:45:03 -0500361 /* Don't tolerate an unexpected trailer iovec. */
362 if ((op->downcall.trailer_size == 0) &&
363 (count != notrailer_count)) {
364 gossip_err("%s: unexpected trailer iovec.\n",
365 __func__);
366 dev_req_release(buffer);
367 put_op(op);
368 return -EPROTO;
369 }
370
371 /* Don't consider the trailer if there's a bad status. */
372 if (op->downcall.status != 0)
373 goto no_trailer;
374
375 /* get the trailer if there is one. */
376 if (op->downcall.trailer_size == 0)
377 goto no_trailer;
378
379 gossip_debug(GOSSIP_DEV_DEBUG,
380 "%s: op->downcall.trailer_size %lld\n",
381 __func__,
382 op->downcall.trailer_size);
383
384 /*
385 * Bail if we think think there should be a trailer, but
386 * there's no iovec for it.
387 */
388 if (count != (notrailer_count + 1)) {
389 gossip_err("%s: trailer_size:%lld: count:%zu:\n",
390 __func__,
391 op->downcall.trailer_size,
392 count);
393 dev_req_release(buffer);
394 put_op(op);
395 return -EPROTO;
396 }
397
398 /* Verify that trailer_size is accurate. */
399 if (op->downcall.trailer_size != iov[trailer_index].iov_len) {
400 gossip_err("%s: trailer_size:%lld: != iov_len:%zd:\n",
401 __func__,
402 op->downcall.trailer_size,
403 iov[trailer_index].iov_len);
404 dev_req_release(buffer);
405 put_op(op);
406 return -EMSGSIZE;
407 }
408
409 total_returned_size += iov[trailer_index].iov_len;
410
411 /*
412 * Allocate a buffer, copy the trailer bytes into it and
413 * attach it to the downcall.
414 */
415 op->downcall.trailer_buf = vmalloc(iov[trailer_index].iov_len);
416 if (op->downcall.trailer_buf != NULL) {
417 gossip_debug(GOSSIP_DEV_DEBUG, "vmalloc: %p\n",
418 op->downcall.trailer_buf);
419 ret = copy_from_user(op->downcall.trailer_buf,
420 iov[trailer_index].iov_base,
421 iov[trailer_index].iov_len);
422 if (ret) {
423 gossip_err("%s: Failed to copy trailer.\n",
424 __func__);
425 dev_req_release(buffer);
426 gossip_debug(GOSSIP_DEV_DEBUG,
427 "vfree: %p\n",
428 op->downcall.trailer_buf);
429 vfree(op->downcall.trailer_buf);
430 op->downcall.trailer_buf = NULL;
431 put_op(op);
432 return -EIO;
433 }
434 } else {
435 /* Change downcall status */
436 gossip_err("writev: could not vmalloc for trailer!\n");
437 dev_req_release(buffer);
438 put_op(op);
439 return -ENOMEM;
440 }
441
442no_trailer:
443
444 /* if this operation is an I/O operation we need to wait
Mike Marshall5db11c22015-07-17 10:38:12 -0400445 * for all data to be copied before we can return to avoid
446 * buffer corruption and races that can pull the buffers
447 * out from under us.
448 *
449 * Essentially we're synchronizing with other parts of the
450 * vfs implicitly by not allowing the user space
451 * application reading/writing this device to return until
452 * the buffers are done being used.
453 */
Mike Marshall97f10022015-12-11 16:45:03 -0500454 if (op->upcall.type == ORANGEFS_VFS_OP_FILE_IO) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400455 int timed_out = 0;
456 DECLARE_WAITQUEUE(wait_entry, current);
457
Mike Marshall97f10022015-12-11 16:45:03 -0500458 /*
459 * tell the vfs op waiting on a waitqueue
Mike Marshall5db11c22015-07-17 10:38:12 -0400460 * that this op is done
461 */
462 spin_lock(&op->lock);
463 set_op_state_serviced(op);
464 spin_unlock(&op->lock);
465
466 add_wait_queue_exclusive(&op->io_completion_waitq,
467 &wait_entry);
468 wake_up_interruptible(&op->waitq);
469
470 while (1) {
471 set_current_state(TASK_INTERRUPTIBLE);
472
473 spin_lock(&op->lock);
474 if (op->io_completed) {
475 spin_unlock(&op->lock);
476 break;
477 }
478 spin_unlock(&op->lock);
479
480 if (!signal_pending(current)) {
481 int timeout =
482 MSECS_TO_JIFFIES(1000 *
483 op_timeout_secs);
484 if (!schedule_timeout(timeout)) {
Mike Marshall97f10022015-12-11 16:45:03 -0500485 gossip_debug(GOSSIP_DEV_DEBUG,
486 "%s: timed out.\n",
487 __func__);
Mike Marshall5db11c22015-07-17 10:38:12 -0400488 timed_out = 1;
489 break;
490 }
491 continue;
492 }
493
Mike Marshall97f10022015-12-11 16:45:03 -0500494 gossip_debug(GOSSIP_DEV_DEBUG,
495 "%s: signal on I/O wait, aborting\n",
496 __func__);
Mike Marshall5db11c22015-07-17 10:38:12 -0400497 break;
498 }
499
500 set_current_state(TASK_RUNNING);
501 remove_wait_queue(&op->io_completion_waitq,
502 &wait_entry);
503
504 /* NOTE: for I/O operations we handle releasing the op
505 * object except in the case of timeout. the reason we
506 * can't free the op in timeout cases is that the op
507 * service logic in the vfs retries operations using
508 * the same op ptr, thus it can't be freed.
509 */
510 if (!timed_out)
511 op_release(op);
512 } else {
513
514 /*
515 * tell the vfs op waiting on a waitqueue that
516 * this op is done
517 */
518 spin_lock(&op->lock);
519 set_op_state_serviced(op);
520 spin_unlock(&op->lock);
521 /*
Mike Marshall54804942015-10-05 13:44:24 -0400522 * for every other operation (i.e. non-I/O), we need to
523 * wake up the callers for downcall completion
524 * notification
Mike Marshall5db11c22015-07-17 10:38:12 -0400525 */
526 wake_up_interruptible(&op->waitq);
527 }
528 } else {
529 /* ignore downcalls that we're not interested in */
530 gossip_debug(GOSSIP_DEV_DEBUG,
531 "WARNING: No one's waiting for tag %llu\n",
532 llu(tag));
533 }
Mike Marshall97f10022015-12-11 16:45:03 -0500534 /* put_op? */
Mike Marshall5db11c22015-07-17 10:38:12 -0400535 dev_req_release(buffer);
536
537 return total_returned_size;
538}
539
Yi Liu8bb8aef2015-11-24 15:12:14 -0500540static ssize_t orangefs_devreq_write_iter(struct kiocb *iocb,
Mike Marshall5db11c22015-07-17 10:38:12 -0400541 struct iov_iter *iter)
542{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500543 return orangefs_devreq_writev(iocb->ki_filp,
Mike Marshall5db11c22015-07-17 10:38:12 -0400544 iter->iov,
545 iter->nr_segs,
546 &iocb->ki_pos);
547}
548
549/* Returns whether any FS are still pending remounted */
550static int mark_all_pending_mounts(void)
551{
552 int unmounted = 1;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500553 struct orangefs_sb_info_s *orangefs_sb = NULL;
Mike Marshall5db11c22015-07-17 10:38:12 -0400554
Yi Liu8bb8aef2015-11-24 15:12:14 -0500555 spin_lock(&orangefs_superblocks_lock);
556 list_for_each_entry(orangefs_sb, &orangefs_superblocks, list) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400557 /* All of these file system require a remount */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500558 orangefs_sb->mount_pending = 1;
Mike Marshall5db11c22015-07-17 10:38:12 -0400559 unmounted = 0;
560 }
Yi Liu8bb8aef2015-11-24 15:12:14 -0500561 spin_unlock(&orangefs_superblocks_lock);
Mike Marshall5db11c22015-07-17 10:38:12 -0400562 return unmounted;
563}
564
565/*
566 * Determine if a given file system needs to be remounted or not
567 * Returns -1 on error
568 * 0 if already mounted
569 * 1 if needs remount
570 */
571int fs_mount_pending(__s32 fsid)
572{
573 int mount_pending = -1;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500574 struct orangefs_sb_info_s *orangefs_sb = NULL;
Mike Marshall5db11c22015-07-17 10:38:12 -0400575
Yi Liu8bb8aef2015-11-24 15:12:14 -0500576 spin_lock(&orangefs_superblocks_lock);
577 list_for_each_entry(orangefs_sb, &orangefs_superblocks, list) {
578 if (orangefs_sb->fs_id == fsid) {
579 mount_pending = orangefs_sb->mount_pending;
Mike Marshall5db11c22015-07-17 10:38:12 -0400580 break;
581 }
582 }
Yi Liu8bb8aef2015-11-24 15:12:14 -0500583 spin_unlock(&orangefs_superblocks_lock);
Mike Marshall5db11c22015-07-17 10:38:12 -0400584 return mount_pending;
585}
586
587/*
588 * NOTE: gets called when the last reference to this device is dropped.
589 * Using the open_access_count variable, we enforce a reference count
590 * on this file so that it can be opened by only one process at a time.
591 * the devreq_mutex is used to make sure all i/o has completed
Yi Liu8bb8aef2015-11-24 15:12:14 -0500592 * before we call orangefs_bufmap_finalize, and similar such tricky
Mike Marshall5db11c22015-07-17 10:38:12 -0400593 * situations
594 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500595static int orangefs_devreq_release(struct inode *inode, struct file *file)
Mike Marshall5db11c22015-07-17 10:38:12 -0400596{
597 int unmounted = 0;
598
599 gossip_debug(GOSSIP_DEV_DEBUG,
600 "%s:pvfs2-client-core: exiting, closing device\n",
601 __func__);
602
603 mutex_lock(&devreq_mutex);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500604 orangefs_bufmap_finalize();
Mike Marshall5db11c22015-07-17 10:38:12 -0400605
606 open_access_count--;
607
608 unmounted = mark_all_pending_mounts();
Yi Liu8bb8aef2015-11-24 15:12:14 -0500609 gossip_debug(GOSSIP_DEV_DEBUG, "ORANGEFS Device Close: Filesystem(s) %s\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400610 (unmounted ? "UNMOUNTED" : "MOUNTED"));
611 mutex_unlock(&devreq_mutex);
612
613 /*
614 * Walk through the list of ops in the request list, mark them
615 * as purged and wake them up.
616 */
617 purge_waiting_ops();
618 /*
619 * Walk through the hash table of in progress operations; mark
620 * them as purged and wake them up
621 */
622 purge_inprogress_ops();
623 gossip_debug(GOSSIP_DEV_DEBUG,
624 "pvfs2-client-core: device close complete\n");
625 return 0;
626}
627
628int is_daemon_in_service(void)
629{
630 int in_service;
631
632 /*
633 * What this function does is checks if client-core is alive
634 * based on the access count we maintain on the device.
635 */
636 mutex_lock(&devreq_mutex);
637 in_service = open_access_count == 1 ? 0 : -EIO;
638 mutex_unlock(&devreq_mutex);
639 return in_service;
640}
641
642static inline long check_ioctl_command(unsigned int command)
643{
644 /* Check for valid ioctl codes */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500645 if (_IOC_TYPE(command) != ORANGEFS_DEV_MAGIC) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400646 gossip_err("device ioctl magic numbers don't match! Did you rebuild pvfs2-client-core/libpvfs2? [cmd %x, magic %x != %x]\n",
647 command,
648 _IOC_TYPE(command),
Yi Liu8bb8aef2015-11-24 15:12:14 -0500649 ORANGEFS_DEV_MAGIC);
Mike Marshall5db11c22015-07-17 10:38:12 -0400650 return -EINVAL;
651 }
652 /* and valid ioctl commands */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500653 if (_IOC_NR(command) >= ORANGEFS_DEV_MAXNR || _IOC_NR(command) <= 0) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400654 gossip_err("Invalid ioctl command number [%d >= %d]\n",
Yi Liu8bb8aef2015-11-24 15:12:14 -0500655 _IOC_NR(command), ORANGEFS_DEV_MAXNR);
Mike Marshall5db11c22015-07-17 10:38:12 -0400656 return -ENOIOCTLCMD;
657 }
658 return 0;
659}
660
661static long dispatch_ioctl_command(unsigned int command, unsigned long arg)
662{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500663 static __s32 magic = ORANGEFS_DEVREQ_MAGIC;
Mike Marshall5db11c22015-07-17 10:38:12 -0400664 static __s32 max_up_size = MAX_ALIGNED_DEV_REQ_UPSIZE;
665 static __s32 max_down_size = MAX_ALIGNED_DEV_REQ_DOWNSIZE;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500666 struct ORANGEFS_dev_map_desc user_desc;
Mike Marshall5db11c22015-07-17 10:38:12 -0400667 int ret = 0;
668 struct dev_mask_info_s mask_info = { 0 };
669 struct dev_mask2_info_s mask2_info = { 0, 0 };
670 int upstream_kmod = 1;
671 struct list_head *tmp = NULL;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500672 struct orangefs_sb_info_s *orangefs_sb = NULL;
Mike Marshall5db11c22015-07-17 10:38:12 -0400673
674 /* mtmoore: add locking here */
675
676 switch (command) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500677 case ORANGEFS_DEV_GET_MAGIC:
Mike Marshall5db11c22015-07-17 10:38:12 -0400678 return ((put_user(magic, (__s32 __user *) arg) == -EFAULT) ?
679 -EIO :
680 0);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500681 case ORANGEFS_DEV_GET_MAX_UPSIZE:
Mike Marshall5db11c22015-07-17 10:38:12 -0400682 return ((put_user(max_up_size,
683 (__s32 __user *) arg) == -EFAULT) ?
684 -EIO :
685 0);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500686 case ORANGEFS_DEV_GET_MAX_DOWNSIZE:
Mike Marshall5db11c22015-07-17 10:38:12 -0400687 return ((put_user(max_down_size,
688 (__s32 __user *) arg) == -EFAULT) ?
689 -EIO :
690 0);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500691 case ORANGEFS_DEV_MAP:
Mike Marshall5db11c22015-07-17 10:38:12 -0400692 ret = copy_from_user(&user_desc,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500693 (struct ORANGEFS_dev_map_desc __user *)
Mike Marshall5db11c22015-07-17 10:38:12 -0400694 arg,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500695 sizeof(struct ORANGEFS_dev_map_desc));
696 return ret ? -EIO : orangefs_bufmap_initialize(&user_desc);
697 case ORANGEFS_DEV_REMOUNT_ALL:
Mike Marshall5db11c22015-07-17 10:38:12 -0400698 gossip_debug(GOSSIP_DEV_DEBUG,
Mike Marshall97f10022015-12-11 16:45:03 -0500699 "%s: got ORANGEFS_DEV_REMOUNT_ALL\n",
700 __func__);
Mike Marshall5db11c22015-07-17 10:38:12 -0400701
702 /*
Yi Liu8bb8aef2015-11-24 15:12:14 -0500703 * remount all mounted orangefs volumes to regain the lost
Mike Marshall5db11c22015-07-17 10:38:12 -0400704 * dynamic mount tables (if any) -- NOTE: this is done
705 * without keeping the superblock list locked due to the
706 * upcall/downcall waiting. also, the request semaphore is
707 * used to ensure that no operations will be serviced until
708 * all of the remounts are serviced (to avoid ops between
709 * mounts to fail)
710 */
711 ret = mutex_lock_interruptible(&request_mutex);
712 if (ret < 0)
713 return ret;
714 gossip_debug(GOSSIP_DEV_DEBUG,
Mike Marshall97f10022015-12-11 16:45:03 -0500715 "%s: priority remount in progress\n",
716 __func__);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500717 list_for_each(tmp, &orangefs_superblocks) {
718 orangefs_sb =
Mike Marshall97f10022015-12-11 16:45:03 -0500719 list_entry(tmp,
720 struct orangefs_sb_info_s,
721 list);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500722 if (orangefs_sb && (orangefs_sb->sb)) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400723 gossip_debug(GOSSIP_DEV_DEBUG,
Mike Marshall97f10022015-12-11 16:45:03 -0500724 "%s: Remounting SB %p\n",
725 __func__,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500726 orangefs_sb);
Mike Marshall5db11c22015-07-17 10:38:12 -0400727
Yi Liu8bb8aef2015-11-24 15:12:14 -0500728 ret = orangefs_remount(orangefs_sb->sb);
Mike Marshall5db11c22015-07-17 10:38:12 -0400729 if (ret) {
730 gossip_debug(GOSSIP_DEV_DEBUG,
731 "SB %p remount failed\n",
Yi Liu8bb8aef2015-11-24 15:12:14 -0500732 orangefs_sb);
Mike Marshall97f10022015-12-11 16:45:03 -0500733 break;
Mike Marshall5db11c22015-07-17 10:38:12 -0400734 }
735 }
736 }
737 gossip_debug(GOSSIP_DEV_DEBUG,
Mike Marshall97f10022015-12-11 16:45:03 -0500738 "%s: priority remount complete\n",
739 __func__);
Mike Marshall5db11c22015-07-17 10:38:12 -0400740 mutex_unlock(&request_mutex);
741 return ret;
742
Yi Liu8bb8aef2015-11-24 15:12:14 -0500743 case ORANGEFS_DEV_UPSTREAM:
Mike Marshall5db11c22015-07-17 10:38:12 -0400744 ret = copy_to_user((void __user *)arg,
745 &upstream_kmod,
746 sizeof(upstream_kmod));
747
748 if (ret != 0)
749 return -EIO;
750 else
751 return ret;
752
Yi Liu8bb8aef2015-11-24 15:12:14 -0500753 case ORANGEFS_DEV_CLIENT_MASK:
Mike Marshall5db11c22015-07-17 10:38:12 -0400754 ret = copy_from_user(&mask2_info,
755 (void __user *)arg,
756 sizeof(struct dev_mask2_info_s));
757
758 if (ret != 0)
759 return -EIO;
760
761 client_debug_mask.mask1 = mask2_info.mask1_value;
762 client_debug_mask.mask2 = mask2_info.mask2_value;
763
764 pr_info("%s: client debug mask has been been received "
765 ":%llx: :%llx:\n",
766 __func__,
767 (unsigned long long)client_debug_mask.mask1,
768 (unsigned long long)client_debug_mask.mask2);
769
770 return ret;
771
Yi Liu8bb8aef2015-11-24 15:12:14 -0500772 case ORANGEFS_DEV_CLIENT_STRING:
Mike Marshall5db11c22015-07-17 10:38:12 -0400773 ret = copy_from_user(&client_debug_array_string,
774 (void __user *)arg,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500775 ORANGEFS_MAX_DEBUG_STRING_LEN);
Mike Marshall5db11c22015-07-17 10:38:12 -0400776 if (ret != 0) {
Mike Marshall97f10022015-12-11 16:45:03 -0500777 pr_info("%s: CLIENT_STRING: copy_from_user failed\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400778 __func__);
779 return -EIO;
780 }
781
Mike Marshall97f10022015-12-11 16:45:03 -0500782 pr_info("%s: client debug array string has been received.\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400783 __func__);
784
785 if (!help_string_initialized) {
786
787 /* Free the "we don't know yet" default string... */
788 kfree(debug_help_string);
789
790 /* build a proper debug help string */
791 if (orangefs_prepare_debugfs_help_string(0)) {
Mike Marshall97f10022015-12-11 16:45:03 -0500792 gossip_err("%s: no debug help string \n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400793 __func__);
794 return -EIO;
795 }
796
797 /* Replace the boilerplate boot-time debug-help file. */
798 debugfs_remove(help_file_dentry);
799
800 help_file_dentry =
801 debugfs_create_file(
802 ORANGEFS_KMOD_DEBUG_HELP_FILE,
803 0444,
804 debug_dir,
805 debug_help_string,
806 &debug_help_fops);
807
808 if (!help_file_dentry) {
809 gossip_err("%s: debugfs_create_file failed for"
810 " :%s:!\n",
811 __func__,
812 ORANGEFS_KMOD_DEBUG_HELP_FILE);
813 return -EIO;
814 }
815 }
816
817 debug_mask_to_string(&client_debug_mask, 1);
818
819 debugfs_remove(client_debug_dentry);
820
Yi Liu8bb8aef2015-11-24 15:12:14 -0500821 orangefs_client_debug_init();
Mike Marshall5db11c22015-07-17 10:38:12 -0400822
823 help_string_initialized++;
824
825 return ret;
826
Yi Liu8bb8aef2015-11-24 15:12:14 -0500827 case ORANGEFS_DEV_DEBUG:
Mike Marshall5db11c22015-07-17 10:38:12 -0400828 ret = copy_from_user(&mask_info,
829 (void __user *)arg,
830 sizeof(mask_info));
831
832 if (ret != 0)
833 return -EIO;
834
835 if (mask_info.mask_type == KERNEL_MASK) {
836 if ((mask_info.mask_value == 0)
837 && (kernel_mask_set_mod_init)) {
838 /*
839 * the kernel debug mask was set when the
840 * kernel module was loaded; don't override
841 * it if the client-core was started without
Yi Liu8bb8aef2015-11-24 15:12:14 -0500842 * a value for ORANGEFS_KMODMASK.
Mike Marshall5db11c22015-07-17 10:38:12 -0400843 */
844 return 0;
845 }
846 debug_mask_to_string(&mask_info.mask_value,
847 mask_info.mask_type);
848 gossip_debug_mask = mask_info.mask_value;
Mike Marshall97f10022015-12-11 16:45:03 -0500849 pr_info("%s: kernel debug mask has been modified to "
Mike Marshall5db11c22015-07-17 10:38:12 -0400850 ":%s: :%llx:\n",
Mike Marshall97f10022015-12-11 16:45:03 -0500851 __func__,
Mike Marshall5db11c22015-07-17 10:38:12 -0400852 kernel_debug_string,
853 (unsigned long long)gossip_debug_mask);
854 } else if (mask_info.mask_type == CLIENT_MASK) {
855 debug_mask_to_string(&mask_info.mask_value,
856 mask_info.mask_type);
Mike Marshall97f10022015-12-11 16:45:03 -0500857 pr_info("%s: client debug mask has been modified to"
Mike Marshall5db11c22015-07-17 10:38:12 -0400858 ":%s: :%llx:\n",
Mike Marshall97f10022015-12-11 16:45:03 -0500859 __func__,
Mike Marshall5db11c22015-07-17 10:38:12 -0400860 client_debug_string,
861 llu(mask_info.mask_value));
862 } else {
863 gossip_lerr("Invalid mask type....\n");
864 return -EINVAL;
865 }
866
867 return ret;
868
869 default:
870 return -ENOIOCTLCMD;
871 }
872 return -ENOIOCTLCMD;
873}
874
Yi Liu8bb8aef2015-11-24 15:12:14 -0500875static long orangefs_devreq_ioctl(struct file *file,
Mike Marshall5db11c22015-07-17 10:38:12 -0400876 unsigned int command, unsigned long arg)
877{
878 long ret;
879
880 /* Check for properly constructed commands */
881 ret = check_ioctl_command(command);
882 if (ret < 0)
883 return (int)ret;
884
885 return (int)dispatch_ioctl_command(command, arg);
886}
887
888#ifdef CONFIG_COMPAT /* CONFIG_COMPAT is in .config */
889
Yi Liu8bb8aef2015-11-24 15:12:14 -0500890/* Compat structure for the ORANGEFS_DEV_MAP ioctl */
891struct ORANGEFS_dev_map_desc32 {
Mike Marshall5db11c22015-07-17 10:38:12 -0400892 compat_uptr_t ptr;
893 __s32 total_size;
894 __s32 size;
895 __s32 count;
896};
897
898static unsigned long translate_dev_map26(unsigned long args, long *error)
899{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500900 struct ORANGEFS_dev_map_desc32 __user *p32 = (void __user *)args;
Mike Marshall5db11c22015-07-17 10:38:12 -0400901 /*
902 * Depending on the architecture, allocate some space on the
903 * user-call-stack based on our expected layout.
904 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500905 struct ORANGEFS_dev_map_desc __user *p =
Mike Marshall5db11c22015-07-17 10:38:12 -0400906 compat_alloc_user_space(sizeof(*p));
Mike Marshall84d02152015-07-28 13:27:51 -0400907 compat_uptr_t addr;
Mike Marshall5db11c22015-07-17 10:38:12 -0400908
909 *error = 0;
910 /* get the ptr from the 32 bit user-space */
911 if (get_user(addr, &p32->ptr))
912 goto err;
913 /* try to put that into a 64-bit layout */
914 if (put_user(compat_ptr(addr), &p->ptr))
915 goto err;
916 /* copy the remaining fields */
917 if (copy_in_user(&p->total_size, &p32->total_size, sizeof(__s32)))
918 goto err;
919 if (copy_in_user(&p->size, &p32->size, sizeof(__s32)))
920 goto err;
921 if (copy_in_user(&p->count, &p32->count, sizeof(__s32)))
922 goto err;
923 return (unsigned long)p;
924err:
925 *error = -EFAULT;
926 return 0;
927}
928
929/*
930 * 32 bit user-space apps' ioctl handlers when kernel modules
931 * is compiled as a 64 bit one
932 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500933static long orangefs_devreq_compat_ioctl(struct file *filp, unsigned int cmd,
Mike Marshall5db11c22015-07-17 10:38:12 -0400934 unsigned long args)
935{
936 long ret;
937 unsigned long arg = args;
938
939 /* Check for properly constructed commands */
940 ret = check_ioctl_command(cmd);
941 if (ret < 0)
942 return ret;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500943 if (cmd == ORANGEFS_DEV_MAP) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400944 /*
945 * convert the arguments to what we expect internally
946 * in kernel space
947 */
948 arg = translate_dev_map26(args, &ret);
949 if (ret < 0) {
950 gossip_err("Could not translate dev map\n");
951 return ret;
952 }
953 }
954 /* no other ioctl requires translation */
955 return dispatch_ioctl_command(cmd, arg);
956}
957
Mike Marshall2c590d52015-07-24 10:37:15 -0400958#endif /* CONFIG_COMPAT is in .config */
959
960/*
961 * The following two ioctl32 functions had been refactored into the above
962 * CONFIG_COMPAT ifdef, but that was an over simplification that was
963 * not noticed until we tried to compile on power pc...
964 */
965#if (defined(CONFIG_COMPAT) && !defined(HAVE_REGISTER_IOCTL32_CONVERSION)) || !defined(CONFIG_COMPAT)
Yi Liu8bb8aef2015-11-24 15:12:14 -0500966static int orangefs_ioctl32_init(void)
Mike Marshall5db11c22015-07-17 10:38:12 -0400967{
968 return 0;
969}
970
Yi Liu8bb8aef2015-11-24 15:12:14 -0500971static void orangefs_ioctl32_cleanup(void)
Mike Marshall5db11c22015-07-17 10:38:12 -0400972{
973 return;
974}
Mike Marshall2c590d52015-07-24 10:37:15 -0400975#endif
Mike Marshall5db11c22015-07-17 10:38:12 -0400976
977/* the assigned character device major number */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500978static int orangefs_dev_major;
Mike Marshall5db11c22015-07-17 10:38:12 -0400979
980/*
Yi Liu8bb8aef2015-11-24 15:12:14 -0500981 * Initialize orangefs device specific state:
Mike Marshall5db11c22015-07-17 10:38:12 -0400982 * Must be called at module load time only
983 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500984int orangefs_dev_init(void)
Mike Marshall5db11c22015-07-17 10:38:12 -0400985{
986 int ret;
987
988 /* register the ioctl32 sub-system */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500989 ret = orangefs_ioctl32_init();
Mike Marshall5db11c22015-07-17 10:38:12 -0400990 if (ret < 0)
991 return ret;
992
Yi Liu8bb8aef2015-11-24 15:12:14 -0500993 /* register orangefs-req device */
994 orangefs_dev_major = register_chrdev(0,
995 ORANGEFS_REQDEVICE_NAME,
996 &orangefs_devreq_file_operations);
997 if (orangefs_dev_major < 0) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400998 gossip_debug(GOSSIP_DEV_DEBUG,
999 "Failed to register /dev/%s (error %d)\n",
Yi Liu8bb8aef2015-11-24 15:12:14 -05001000 ORANGEFS_REQDEVICE_NAME, orangefs_dev_major);
1001 orangefs_ioctl32_cleanup();
1002 return orangefs_dev_major;
Mike Marshall5db11c22015-07-17 10:38:12 -04001003 }
1004
1005 gossip_debug(GOSSIP_DEV_DEBUG,
1006 "*** /dev/%s character device registered ***\n",
Yi Liu8bb8aef2015-11-24 15:12:14 -05001007 ORANGEFS_REQDEVICE_NAME);
Mike Marshall5db11c22015-07-17 10:38:12 -04001008 gossip_debug(GOSSIP_DEV_DEBUG, "'mknod /dev/%s c %d 0'.\n",
Yi Liu8bb8aef2015-11-24 15:12:14 -05001009 ORANGEFS_REQDEVICE_NAME, orangefs_dev_major);
Mike Marshall5db11c22015-07-17 10:38:12 -04001010 return 0;
1011}
1012
Yi Liu8bb8aef2015-11-24 15:12:14 -05001013void orangefs_dev_cleanup(void)
Mike Marshall5db11c22015-07-17 10:38:12 -04001014{
Yi Liu8bb8aef2015-11-24 15:12:14 -05001015 unregister_chrdev(orangefs_dev_major, ORANGEFS_REQDEVICE_NAME);
Mike Marshall5db11c22015-07-17 10:38:12 -04001016 gossip_debug(GOSSIP_DEV_DEBUG,
1017 "*** /dev/%s character device unregistered ***\n",
Yi Liu8bb8aef2015-11-24 15:12:14 -05001018 ORANGEFS_REQDEVICE_NAME);
Mike Marshall5db11c22015-07-17 10:38:12 -04001019 /* unregister the ioctl32 sub-system */
Yi Liu8bb8aef2015-11-24 15:12:14 -05001020 orangefs_ioctl32_cleanup();
Mike Marshall5db11c22015-07-17 10:38:12 -04001021}
1022
Yi Liu8bb8aef2015-11-24 15:12:14 -05001023static unsigned int orangefs_devreq_poll(struct file *file,
Mike Marshall5db11c22015-07-17 10:38:12 -04001024 struct poll_table_struct *poll_table)
1025{
1026 int poll_revent_mask = 0;
1027
1028 if (open_access_count == 1) {
Yi Liu8bb8aef2015-11-24 15:12:14 -05001029 poll_wait(file, &orangefs_request_list_waitq, poll_table);
Mike Marshall5db11c22015-07-17 10:38:12 -04001030
Yi Liu8bb8aef2015-11-24 15:12:14 -05001031 spin_lock(&orangefs_request_list_lock);
1032 if (!list_empty(&orangefs_request_list))
Mike Marshall5db11c22015-07-17 10:38:12 -04001033 poll_revent_mask |= POLL_IN;
Yi Liu8bb8aef2015-11-24 15:12:14 -05001034 spin_unlock(&orangefs_request_list_lock);
Mike Marshall5db11c22015-07-17 10:38:12 -04001035 }
1036 return poll_revent_mask;
1037}
1038
Yi Liu8bb8aef2015-11-24 15:12:14 -05001039const struct file_operations orangefs_devreq_file_operations = {
Mike Marshall5db11c22015-07-17 10:38:12 -04001040 .owner = THIS_MODULE,
Yi Liu8bb8aef2015-11-24 15:12:14 -05001041 .read = orangefs_devreq_read,
1042 .write_iter = orangefs_devreq_write_iter,
1043 .open = orangefs_devreq_open,
1044 .release = orangefs_devreq_release,
1045 .unlocked_ioctl = orangefs_devreq_ioctl,
Mike Marshall5db11c22015-07-17 10:38:12 -04001046
1047#ifdef CONFIG_COMPAT /* CONFIG_COMPAT is in .config */
Yi Liu8bb8aef2015-11-24 15:12:14 -05001048 .compat_ioctl = orangefs_devreq_compat_ioctl,
Mike Marshall5db11c22015-07-17 10:38:12 -04001049#endif
Yi Liu8bb8aef2015-11-24 15:12:14 -05001050 .poll = orangefs_devreq_poll
Mike Marshall5db11c22015-07-17 10:38:12 -04001051};