blob: 6779cb7b747e3f700d4437667a9b8a6fe3534189 [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"
Martin Brandenburg44f46412016-08-15 11:38:36 -040014#include "orangefs-debugfs.h"
Mike Marshall5db11c22015-07-17 10:38:12 -040015
16#include <linux/debugfs.h>
17#include <linux/slab.h>
18
19/* this file implements the /dev/pvfs2-req device node */
20
21static int open_access_count;
22
23#define DUMP_DEVICE_ERROR() \
24do { \
25 gossip_err("*****************************************************\n");\
Yi Liu8bb8aef2015-11-24 15:12:14 -050026 gossip_err("ORANGEFS Device Error: You cannot open the device file "); \
Mike Marshall5db11c22015-07-17 10:38:12 -040027 gossip_err("\n/dev/%s more than once. Please make sure that\nthere " \
Yi Liu8bb8aef2015-11-24 15:12:14 -050028 "are no ", ORANGEFS_REQDEVICE_NAME); \
Mike Marshall5db11c22015-07-17 10:38:12 -040029 gossip_err("instances of a program using this device\ncurrently " \
30 "running. (You must verify this!)\n"); \
31 gossip_err("For example, you can use the lsof program as follows:\n");\
32 gossip_err("'lsof | grep %s' (run this as root)\n", \
Yi Liu8bb8aef2015-11-24 15:12:14 -050033 ORANGEFS_REQDEVICE_NAME); \
Mike Marshall5db11c22015-07-17 10:38:12 -040034 gossip_err(" open_access_count = %d\n", open_access_count); \
35 gossip_err("*****************************************************\n");\
36} while (0)
37
38static int hash_func(__u64 tag, int table_size)
39{
Mike Marshall2c590d52015-07-24 10:37:15 -040040 return do_div(tag, (unsigned int)table_size);
Mike Marshall5db11c22015-07-17 10:38:12 -040041}
42
Yi Liu8bb8aef2015-11-24 15:12:14 -050043static void orangefs_devreq_add_op(struct orangefs_kernel_op_s *op)
Mike Marshall5db11c22015-07-17 10:38:12 -040044{
45 int index = hash_func(op->tag, hash_table_size);
46
Mike Marshall5db11c22015-07-17 10:38:12 -040047 list_add_tail(&op->list, &htable_ops_in_progress[index]);
Mike Marshall5db11c22015-07-17 10:38:12 -040048}
49
Mike Marshallca9f5182016-02-26 10:21:12 -050050/*
51 * find the op with this tag and remove it from the in progress
52 * hash table.
53 */
Yi Liu8bb8aef2015-11-24 15:12:14 -050054static struct orangefs_kernel_op_s *orangefs_devreq_remove_op(__u64 tag)
Mike Marshall5db11c22015-07-17 10:38:12 -040055{
Yi Liu8bb8aef2015-11-24 15:12:14 -050056 struct orangefs_kernel_op_s *op, *next;
Mike Marshall5db11c22015-07-17 10:38:12 -040057 int index;
58
59 index = hash_func(tag, hash_table_size);
60
61 spin_lock(&htable_ops_in_progress_lock);
62 list_for_each_entry_safe(op,
63 next,
64 &htable_ops_in_progress[index],
65 list) {
Al Viro05a50a52016-02-18 18:59:44 -050066 if (op->tag == tag && !op_state_purged(op) &&
67 !op_state_given_up(op)) {
Al Viroed42fe02016-01-22 19:47:47 -050068 list_del_init(&op->list);
Mike Marshall5db11c22015-07-17 10:38:12 -040069 spin_unlock(&htable_ops_in_progress_lock);
70 return op;
71 }
72 }
73
74 spin_unlock(&htable_ops_in_progress_lock);
75 return NULL;
76}
77
Martin Brandenburgacfcbaf2016-03-05 13:17:39 -050078/* Returns whether any FS are still pending remounted */
79static int mark_all_pending_mounts(void)
80{
81 int unmounted = 1;
82 struct orangefs_sb_info_s *orangefs_sb = NULL;
83
84 spin_lock(&orangefs_superblocks_lock);
85 list_for_each_entry(orangefs_sb, &orangefs_superblocks, list) {
86 /* All of these file system require a remount */
87 orangefs_sb->mount_pending = 1;
88 unmounted = 0;
89 }
90 spin_unlock(&orangefs_superblocks_lock);
91 return unmounted;
92}
93
94/*
95 * Determine if a given file system needs to be remounted or not
96 * Returns -1 on error
97 * 0 if already mounted
98 * 1 if needs remount
99 */
100static int fs_mount_pending(__s32 fsid)
101{
102 int mount_pending = -1;
103 struct orangefs_sb_info_s *orangefs_sb = NULL;
104
105 spin_lock(&orangefs_superblocks_lock);
106 list_for_each_entry(orangefs_sb, &orangefs_superblocks, list) {
107 if (orangefs_sb->fs_id == fsid) {
108 mount_pending = orangefs_sb->mount_pending;
109 break;
110 }
111 }
112 spin_unlock(&orangefs_superblocks_lock);
113 return mount_pending;
114}
115
Yi Liu8bb8aef2015-11-24 15:12:14 -0500116static int orangefs_devreq_open(struct inode *inode, struct file *file)
Mike Marshall5db11c22015-07-17 10:38:12 -0400117{
118 int ret = -EINVAL;
119
Jann Horn78fee0b2016-06-25 01:51:52 +0200120 /* in order to ensure that the filesystem driver sees correct UIDs */
121 if (file->f_cred->user_ns != &init_user_ns) {
122 gossip_err("%s: device cannot be opened outside init_user_ns\n",
123 __func__);
124 goto out;
125 }
126
Mike Marshall5db11c22015-07-17 10:38:12 -0400127 if (!(file->f_flags & O_NONBLOCK)) {
Mike Marshall97f10022015-12-11 16:45:03 -0500128 gossip_err("%s: device cannot be opened in blocking mode\n",
129 __func__);
Mike Marshall5db11c22015-07-17 10:38:12 -0400130 goto out;
131 }
132 ret = -EACCES;
Mike Marshall97f10022015-12-11 16:45:03 -0500133 gossip_debug(GOSSIP_DEV_DEBUG, "client-core: opening device\n");
Mike Marshall5db11c22015-07-17 10:38:12 -0400134 mutex_lock(&devreq_mutex);
135
136 if (open_access_count == 0) {
Al Virofee25ce2016-01-22 19:46:08 -0500137 open_access_count = 1;
Al Virofb6d2522016-01-19 12:00:26 -0500138 ret = 0;
Mike Marshall5db11c22015-07-17 10:38:12 -0400139 } else {
140 DUMP_DEVICE_ERROR();
141 }
142 mutex_unlock(&devreq_mutex);
143
144out:
145
146 gossip_debug(GOSSIP_DEV_DEBUG,
147 "pvfs2-client-core: open device complete (ret = %d)\n",
148 ret);
149 return ret;
150}
151
Mike Marshall97f10022015-12-11 16:45:03 -0500152/* Function for read() callers into the device */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500153static ssize_t orangefs_devreq_read(struct file *file,
Mike Marshall5db11c22015-07-17 10:38:12 -0400154 char __user *buf,
155 size_t count, loff_t *offset)
156{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500157 struct orangefs_kernel_op_s *op, *temp;
158 __s32 proto_ver = ORANGEFS_KERNEL_PROTO_VERSION;
159 static __s32 magic = ORANGEFS_DEVREQ_MAGIC;
160 struct orangefs_kernel_op_s *cur_op = NULL;
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500161 unsigned long ret;
Mike Marshall5db11c22015-07-17 10:38:12 -0400162
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500163 /* We do not support blocking IO. */
Mike Marshall5db11c22015-07-17 10:38:12 -0400164 if (!(file->f_flags & O_NONBLOCK)) {
Mike Marshall97f10022015-12-11 16:45:03 -0500165 gossip_err("%s: blocking read from client-core.\n",
166 __func__);
Mike Marshall5db11c22015-07-17 10:38:12 -0400167 return -EINVAL;
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500168 }
169
170 /*
Martin Brandenburga762ae62015-12-15 14:22:06 -0500171 * The client will do an ioctl to find MAX_DEV_REQ_UPSIZE, then
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500172 * always read with that size buffer.
173 */
Martin Brandenburga762ae62015-12-15 14:22:06 -0500174 if (count != MAX_DEV_REQ_UPSIZE) {
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500175 gossip_err("orangefs: client-core tried to read wrong size\n");
176 return -EINVAL;
177 }
178
Al Viroed42fe02016-01-22 19:47:47 -0500179restart:
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500180 /* Get next op (if any) from top of list. */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500181 spin_lock(&orangefs_request_list_lock);
182 list_for_each_entry_safe(op, temp, &orangefs_request_list, list) {
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500183 __s32 fsid;
184 /* This lock is held past the end of the loop when we break. */
185 spin_lock(&op->lock);
Al Viro05a50a52016-02-18 18:59:44 -0500186 if (unlikely(op_state_purged(op) || op_state_given_up(op))) {
Al Viroed42fe02016-01-22 19:47:47 -0500187 spin_unlock(&op->lock);
188 continue;
189 }
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500190
191 fsid = fsid_of_op(op);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500192 if (fsid != ORANGEFS_FS_ID_NULL) {
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500193 int ret;
194 /* Skip ops whose filesystem needs to be mounted. */
195 ret = fs_mount_pending(fsid);
196 if (ret == 1) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400197 gossip_debug(GOSSIP_DEV_DEBUG,
Mike Marshall5090c962016-02-04 13:29:27 -0500198 "%s: mount pending, skipping op tag "
199 "%llu %s\n",
200 __func__,
201 llu(op->tag),
202 get_opname_string(op));
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500203 spin_unlock(&op->lock);
Mike Marshall5db11c22015-07-17 10:38:12 -0400204 continue;
Mike Marshall97f10022015-12-11 16:45:03 -0500205 /*
206 * Skip ops whose filesystem we don't know about unless
207 * it is being mounted.
208 */
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500209 /* XXX: is there a better way to detect this? */
210 } else if (ret == -1 &&
Mike Marshall97f10022015-12-11 16:45:03 -0500211 !(op->upcall.type ==
212 ORANGEFS_VFS_OP_FS_MOUNT ||
213 op->upcall.type ==
214 ORANGEFS_VFS_OP_GETATTR)) {
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500215 gossip_debug(GOSSIP_DEV_DEBUG,
216 "orangefs: skipping op tag %llu %s\n",
217 llu(op->tag), get_opname_string(op));
218 gossip_err(
219 "orangefs: ERROR: fs_mount_pending %d\n",
220 fsid);
221 spin_unlock(&op->lock);
222 continue;
Mike Marshall5db11c22015-07-17 10:38:12 -0400223 }
224 }
Mike Marshall5db11c22015-07-17 10:38:12 -0400225 /*
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500226 * Either this op does not pertain to a filesystem, is mounting
227 * a filesystem, or pertains to a mounted filesystem. Let it
228 * through.
Mike Marshall5db11c22015-07-17 10:38:12 -0400229 */
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500230 cur_op = op;
231 break;
Mike Marshall5db11c22015-07-17 10:38:12 -0400232 }
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500233
234 /*
235 * At this point we either have a valid op and can continue or have not
236 * found an op and must ask the client to try again later.
237 */
238 if (!cur_op) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500239 spin_unlock(&orangefs_request_list_lock);
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500240 return -EAGAIN;
241 }
242
Mike Marshallca9f5182016-02-26 10:21:12 -0500243 gossip_debug(GOSSIP_DEV_DEBUG, "%s: reading op tag %llu %s\n",
244 __func__,
245 llu(cur_op->tag),
246 get_opname_string(cur_op));
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500247
248 /*
249 * Such an op should never be on the list in the first place. If so, we
250 * will abort.
251 */
252 if (op_state_in_progress(cur_op) || op_state_serviced(cur_op)) {
253 gossip_err("orangefs: ERROR: Current op already queued.\n");
Al Viro05a50a52016-02-18 18:59:44 -0500254 list_del_init(&cur_op->list);
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500255 spin_unlock(&cur_op->lock);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500256 spin_unlock(&orangefs_request_list_lock);
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500257 return -EAGAIN;
258 }
Mike Marshallca9f5182016-02-26 10:21:12 -0500259
Al Viroed42fe02016-01-22 19:47:47 -0500260 list_del_init(&cur_op->list);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500261 spin_unlock(&orangefs_request_list_lock);
Al Viroed42fe02016-01-22 19:47:47 -0500262
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500263 spin_unlock(&cur_op->lock);
264
265 /* Push the upcall out. */
266 ret = copy_to_user(buf, &proto_ver, sizeof(__s32));
267 if (ret != 0)
268 goto error;
269 ret = copy_to_user(buf+sizeof(__s32), &magic, sizeof(__s32));
270 if (ret != 0)
271 goto error;
272 ret = copy_to_user(buf+2 * sizeof(__s32), &cur_op->tag, sizeof(__u64));
273 if (ret != 0)
274 goto error;
275 ret = copy_to_user(buf+2*sizeof(__s32)+sizeof(__u64), &cur_op->upcall,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500276 sizeof(struct orangefs_upcall_s));
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500277 if (ret != 0)
278 goto error;
279
Al Viroed42fe02016-01-22 19:47:47 -0500280 spin_lock(&htable_ops_in_progress_lock);
281 spin_lock(&cur_op->lock);
282 if (unlikely(op_state_given_up(cur_op))) {
283 spin_unlock(&cur_op->lock);
284 spin_unlock(&htable_ops_in_progress_lock);
Al Viro05a50a52016-02-18 18:59:44 -0500285 complete(&cur_op->waitq);
Al Viroed42fe02016-01-22 19:47:47 -0500286 goto restart;
287 }
288
289 /*
290 * Set the operation to be in progress and move it between lists since
291 * it has been sent to the client.
292 */
293 set_op_state_inprogress(cur_op);
Mike Marshall9d9e7ba2016-03-03 13:46:48 -0500294 gossip_debug(GOSSIP_DEV_DEBUG,
295 "%s: 1 op:%s: op_state:%d: process:%s:\n",
296 __func__,
297 get_opname_string(cur_op),
298 cur_op->op_state,
299 current->comm);
Al Viroed42fe02016-01-22 19:47:47 -0500300 orangefs_devreq_add_op(cur_op);
301 spin_unlock(&cur_op->lock);
302 spin_unlock(&htable_ops_in_progress_lock);
Al Viroed42fe02016-01-22 19:47:47 -0500303
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500304 /* The client only asks to read one size buffer. */
Martin Brandenburga762ae62015-12-15 14:22:06 -0500305 return MAX_DEV_REQ_UPSIZE;
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500306error:
307 /*
308 * We were unable to copy the op data to the client. Put the op back in
309 * list. If client has crashed, the op will be purged later when the
310 * device is released.
311 */
312 gossip_err("orangefs: Failed to copy data to user space\n");
Yi Liu8bb8aef2015-11-24 15:12:14 -0500313 spin_lock(&orangefs_request_list_lock);
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500314 spin_lock(&cur_op->lock);
Al Viroed42fe02016-01-22 19:47:47 -0500315 if (likely(!op_state_given_up(cur_op))) {
316 set_op_state_waiting(cur_op);
Mike Marshall9d9e7ba2016-03-03 13:46:48 -0500317 gossip_debug(GOSSIP_DEV_DEBUG,
318 "%s: 2 op:%s: op_state:%d: process:%s:\n",
319 __func__,
320 get_opname_string(cur_op),
321 cur_op->op_state,
322 current->comm);
Al Viroed42fe02016-01-22 19:47:47 -0500323 list_add(&cur_op->list, &orangefs_request_list);
Al Viro05a50a52016-02-18 18:59:44 -0500324 spin_unlock(&cur_op->lock);
325 } else {
326 spin_unlock(&cur_op->lock);
327 complete(&cur_op->waitq);
Al Viroed42fe02016-01-22 19:47:47 -0500328 }
Yi Liu8bb8aef2015-11-24 15:12:14 -0500329 spin_unlock(&orangefs_request_list_lock);
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500330 return -EFAULT;
Mike Marshall5db11c22015-07-17 10:38:12 -0400331}
332
Mike Marshall97f10022015-12-11 16:45:03 -0500333/*
Mike Marshallb3ae4752016-01-13 11:18:12 -0500334 * Function for writev() callers into the device.
335 *
336 * Userspace should have written:
337 * - __u32 version
338 * - __u32 magic
339 * - __u64 tag
340 * - struct orangefs_downcall_s
341 * - trailer buffer (in the case of READDIR operations)
Mike Marshall97f10022015-12-11 16:45:03 -0500342 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500343static ssize_t orangefs_devreq_write_iter(struct kiocb *iocb,
Mike Marshall5db11c22015-07-17 10:38:12 -0400344 struct iov_iter *iter)
345{
Mike Marshallb3ae4752016-01-13 11:18:12 -0500346 ssize_t ret;
347 struct orangefs_kernel_op_s *op = NULL;
348 struct {
349 __u32 version;
350 __u32 magic;
351 __u64 tag;
352 } head;
353 int total = ret = iov_iter_count(iter);
354 int n;
355 int downcall_size = sizeof(struct orangefs_downcall_s);
356 int head_size = sizeof(head);
357
358 gossip_debug(GOSSIP_DEV_DEBUG, "%s: total:%d: ret:%zd:\n",
359 __func__,
360 total,
361 ret);
362
363 if (total < MAX_DEV_REQ_DOWNSIZE) {
Mike Marshallcf0c2772016-01-19 12:04:40 -0500364 gossip_err("%s: total:%d: must be at least:%u:\n",
Mike Marshallb3ae4752016-01-13 11:18:12 -0500365 __func__,
366 total,
Mike Marshallcf0c2772016-01-19 12:04:40 -0500367 (unsigned int) MAX_DEV_REQ_DOWNSIZE);
Al Viroed42fe02016-01-22 19:47:47 -0500368 return -EFAULT;
Mike Marshallb3ae4752016-01-13 11:18:12 -0500369 }
370
371 n = copy_from_iter(&head, head_size, iter);
372 if (n < head_size) {
373 gossip_err("%s: failed to copy head.\n", __func__);
Al Viroed42fe02016-01-22 19:47:47 -0500374 return -EFAULT;
Mike Marshallb3ae4752016-01-13 11:18:12 -0500375 }
376
377 if (head.version < ORANGEFS_MINIMUM_USERSPACE_VERSION) {
378 gossip_err("%s: userspace claims version"
379 "%d, minimum version required: %d.\n",
380 __func__,
381 head.version,
382 ORANGEFS_MINIMUM_USERSPACE_VERSION);
Al Viroed42fe02016-01-22 19:47:47 -0500383 return -EPROTO;
Mike Marshallb3ae4752016-01-13 11:18:12 -0500384 }
385
386 if (head.magic != ORANGEFS_DEVREQ_MAGIC) {
387 gossip_err("Error: Device magic number does not match.\n");
Al Viroed42fe02016-01-22 19:47:47 -0500388 return -EPROTO;
Mike Marshallb3ae4752016-01-13 11:18:12 -0500389 }
390
Mike Marshallca9f5182016-02-26 10:21:12 -0500391 /* remove the op from the in progress hash table */
Mike Marshallb3ae4752016-01-13 11:18:12 -0500392 op = orangefs_devreq_remove_op(head.tag);
393 if (!op) {
394 gossip_err("WARNING: No one's waiting for tag %llu\n",
395 llu(head.tag));
Al Viroed42fe02016-01-22 19:47:47 -0500396 return ret;
Mike Marshallb3ae4752016-01-13 11:18:12 -0500397 }
398
Mike Marshallb3ae4752016-01-13 11:18:12 -0500399 n = copy_from_iter(&op->downcall, downcall_size, iter);
400 if (n != downcall_size) {
401 gossip_err("%s: failed to copy downcall.\n", __func__);
Al Viro5964c1b2016-02-18 18:53:41 -0500402 goto Efault;
Mike Marshallb3ae4752016-01-13 11:18:12 -0500403 }
404
405 if (op->downcall.status)
406 goto wakeup;
407
408 /*
409 * We've successfully peeled off the head and the downcall.
410 * Something has gone awry if total doesn't equal the
411 * sum of head_size, downcall_size and trailer_size.
412 */
413 if ((head_size + downcall_size + op->downcall.trailer_size) != total) {
414 gossip_err("%s: funky write, head_size:%d"
415 ": downcall_size:%d: trailer_size:%lld"
416 ": total size:%d:\n",
417 __func__,
418 head_size,
419 downcall_size,
420 op->downcall.trailer_size,
421 total);
Al Viro5964c1b2016-02-18 18:53:41 -0500422 goto Efault;
Mike Marshallb3ae4752016-01-13 11:18:12 -0500423 }
424
425 /* Only READDIR operations should have trailers. */
426 if ((op->downcall.type != ORANGEFS_VFS_OP_READDIR) &&
427 (op->downcall.trailer_size != 0)) {
428 gossip_err("%s: %x operation with trailer.",
429 __func__,
430 op->downcall.type);
Al Viro5964c1b2016-02-18 18:53:41 -0500431 goto Efault;
Mike Marshallb3ae4752016-01-13 11:18:12 -0500432 }
433
434 /* READDIR operations should always have trailers. */
435 if ((op->downcall.type == ORANGEFS_VFS_OP_READDIR) &&
436 (op->downcall.trailer_size == 0)) {
437 gossip_err("%s: %x operation with no trailer.",
438 __func__,
439 op->downcall.type);
Al Viro5964c1b2016-02-18 18:53:41 -0500440 goto Efault;
Mike Marshallb3ae4752016-01-13 11:18:12 -0500441 }
442
443 if (op->downcall.type != ORANGEFS_VFS_OP_READDIR)
444 goto wakeup;
445
446 op->downcall.trailer_buf =
447 vmalloc(op->downcall.trailer_size);
448 if (op->downcall.trailer_buf == NULL) {
449 gossip_err("%s: failed trailer vmalloc.\n",
450 __func__);
Al Viro5964c1b2016-02-18 18:53:41 -0500451 goto Enomem;
Mike Marshallb3ae4752016-01-13 11:18:12 -0500452 }
453 memset(op->downcall.trailer_buf, 0, op->downcall.trailer_size);
454 n = copy_from_iter(op->downcall.trailer_buf,
455 op->downcall.trailer_size,
456 iter);
457 if (n != op->downcall.trailer_size) {
458 gossip_err("%s: failed to copy trailer.\n", __func__);
459 vfree(op->downcall.trailer_buf);
Al Viro5964c1b2016-02-18 18:53:41 -0500460 goto Efault;
Mike Marshallb3ae4752016-01-13 11:18:12 -0500461 }
462
463wakeup:
Al Viro2a9e5c22016-01-23 13:45:46 -0500464 /*
Mike Marshall9f08cfe92016-02-26 14:39:08 -0500465 * Return to vfs waitqueue, and back to service_operation
466 * through wait_for_matching_downcall.
Al Viro2a9e5c22016-01-23 13:45:46 -0500467 */
468 spin_lock(&op->lock);
Al Viro5964c1b2016-02-18 18:53:41 -0500469 if (unlikely(op_is_cancel(op))) {
Al Viro2a9e5c22016-01-23 13:45:46 -0500470 spin_unlock(&op->lock);
Al Viro78699e22016-02-11 23:07:19 -0500471 put_cancel(op);
Al Viro5964c1b2016-02-18 18:53:41 -0500472 } else if (unlikely(op_state_given_up(op))) {
473 spin_unlock(&op->lock);
Al Viro05a50a52016-02-18 18:59:44 -0500474 complete(&op->waitq);
Al Viro5964c1b2016-02-18 18:53:41 -0500475 } else {
476 set_op_state_serviced(op);
Mike Marshall9d9e7ba2016-03-03 13:46:48 -0500477 gossip_debug(GOSSIP_DEV_DEBUG,
478 "%s: op:%s: op_state:%d: process:%s:\n",
479 __func__,
480 get_opname_string(op),
481 op->op_state,
482 current->comm);
Al Viro5964c1b2016-02-18 18:53:41 -0500483 spin_unlock(&op->lock);
484 }
Mike Marshallb3ae4752016-01-13 11:18:12 -0500485 return ret;
Al Viroed42fe02016-01-22 19:47:47 -0500486
Al Viro5964c1b2016-02-18 18:53:41 -0500487Efault:
488 op->downcall.status = -(ORANGEFS_ERROR_BIT | 9);
489 ret = -EFAULT;
490 goto wakeup;
491
492Enomem:
493 op->downcall.status = -(ORANGEFS_ERROR_BIT | 8);
494 ret = -ENOMEM;
495 goto wakeup;
Mike Marshall5db11c22015-07-17 10:38:12 -0400496}
497
Mike Marshall5db11c22015-07-17 10:38:12 -0400498/*
499 * NOTE: gets called when the last reference to this device is dropped.
500 * Using the open_access_count variable, we enforce a reference count
501 * on this file so that it can be opened by only one process at a time.
502 * the devreq_mutex is used to make sure all i/o has completed
Yi Liu8bb8aef2015-11-24 15:12:14 -0500503 * before we call orangefs_bufmap_finalize, and similar such tricky
Mike Marshall5db11c22015-07-17 10:38:12 -0400504 * situations
505 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500506static int orangefs_devreq_release(struct inode *inode, struct file *file)
Mike Marshall5db11c22015-07-17 10:38:12 -0400507{
508 int unmounted = 0;
509
510 gossip_debug(GOSSIP_DEV_DEBUG,
511 "%s:pvfs2-client-core: exiting, closing device\n",
512 __func__);
513
514 mutex_lock(&devreq_mutex);
Al Viroea2c9c92016-02-13 21:01:21 -0500515 orangefs_bufmap_finalize();
Mike Marshall5db11c22015-07-17 10:38:12 -0400516
Al Virofee25ce2016-01-22 19:46:08 -0500517 open_access_count = -1;
Mike Marshall5db11c22015-07-17 10:38:12 -0400518
519 unmounted = mark_all_pending_mounts();
Yi Liu8bb8aef2015-11-24 15:12:14 -0500520 gossip_debug(GOSSIP_DEV_DEBUG, "ORANGEFS Device Close: Filesystem(s) %s\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400521 (unmounted ? "UNMOUNTED" : "MOUNTED"));
Mike Marshall5db11c22015-07-17 10:38:12 -0400522
Mike Marshall5db11c22015-07-17 10:38:12 -0400523 purge_waiting_ops();
Mike Marshall5db11c22015-07-17 10:38:12 -0400524 purge_inprogress_ops();
Al Viroea2c9c92016-02-13 21:01:21 -0500525
526 orangefs_bufmap_run_down();
527
Mike Marshall5db11c22015-07-17 10:38:12 -0400528 gossip_debug(GOSSIP_DEV_DEBUG,
529 "pvfs2-client-core: device close complete\n");
Al Virofee25ce2016-01-22 19:46:08 -0500530 open_access_count = 0;
531 mutex_unlock(&devreq_mutex);
Mike Marshall5db11c22015-07-17 10:38:12 -0400532 return 0;
533}
534
535int 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
Al Viro78699e22016-02-11 23:07:19 -0500549bool __is_daemon_in_service(void)
550{
551 return open_access_count == 1;
552}
553
Mike Marshall5db11c22015-07-17 10:38:12 -0400554static inline long check_ioctl_command(unsigned int command)
555{
556 /* Check for valid ioctl codes */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500557 if (_IOC_TYPE(command) != ORANGEFS_DEV_MAGIC) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400558 gossip_err("device ioctl magic numbers don't match! Did you rebuild pvfs2-client-core/libpvfs2? [cmd %x, magic %x != %x]\n",
559 command,
560 _IOC_TYPE(command),
Yi Liu8bb8aef2015-11-24 15:12:14 -0500561 ORANGEFS_DEV_MAGIC);
Mike Marshall5db11c22015-07-17 10:38:12 -0400562 return -EINVAL;
563 }
564 /* and valid ioctl commands */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500565 if (_IOC_NR(command) >= ORANGEFS_DEV_MAXNR || _IOC_NR(command) <= 0) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400566 gossip_err("Invalid ioctl command number [%d >= %d]\n",
Yi Liu8bb8aef2015-11-24 15:12:14 -0500567 _IOC_NR(command), ORANGEFS_DEV_MAXNR);
Mike Marshall5db11c22015-07-17 10:38:12 -0400568 return -ENOIOCTLCMD;
569 }
570 return 0;
571}
572
573static long dispatch_ioctl_command(unsigned int command, unsigned long arg)
574{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500575 static __s32 magic = ORANGEFS_DEVREQ_MAGIC;
Martin Brandenburga762ae62015-12-15 14:22:06 -0500576 static __s32 max_up_size = MAX_DEV_REQ_UPSIZE;
577 static __s32 max_down_size = MAX_DEV_REQ_DOWNSIZE;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500578 struct ORANGEFS_dev_map_desc user_desc;
Mike Marshall5db11c22015-07-17 10:38:12 -0400579 int ret = 0;
Mike Marshall5db11c22015-07-17 10:38:12 -0400580 int upstream_kmod = 1;
Al Viro45996492016-03-25 19:56:34 -0400581 struct orangefs_sb_info_s *orangefs_sb;
Mike Marshall5db11c22015-07-17 10:38:12 -0400582
583 /* mtmoore: add locking here */
584
585 switch (command) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500586 case ORANGEFS_DEV_GET_MAGIC:
Mike Marshall5db11c22015-07-17 10:38:12 -0400587 return ((put_user(magic, (__s32 __user *) arg) == -EFAULT) ?
588 -EIO :
589 0);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500590 case ORANGEFS_DEV_GET_MAX_UPSIZE:
Mike Marshall5db11c22015-07-17 10:38:12 -0400591 return ((put_user(max_up_size,
592 (__s32 __user *) arg) == -EFAULT) ?
593 -EIO :
594 0);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500595 case ORANGEFS_DEV_GET_MAX_DOWNSIZE:
Mike Marshall5db11c22015-07-17 10:38:12 -0400596 return ((put_user(max_down_size,
597 (__s32 __user *) arg) == -EFAULT) ?
598 -EIO :
599 0);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500600 case ORANGEFS_DEV_MAP:
Mike Marshall5db11c22015-07-17 10:38:12 -0400601 ret = copy_from_user(&user_desc,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500602 (struct ORANGEFS_dev_map_desc __user *)
Mike Marshall5db11c22015-07-17 10:38:12 -0400603 arg,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500604 sizeof(struct ORANGEFS_dev_map_desc));
Al Viroea2c9c92016-02-13 21:01:21 -0500605 /* WTF -EIO and not -EFAULT? */
606 return ret ? -EIO : orangefs_bufmap_initialize(&user_desc);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500607 case ORANGEFS_DEV_REMOUNT_ALL:
Mike Marshall5db11c22015-07-17 10:38:12 -0400608 gossip_debug(GOSSIP_DEV_DEBUG,
Mike Marshall97f10022015-12-11 16:45:03 -0500609 "%s: got ORANGEFS_DEV_REMOUNT_ALL\n",
610 __func__);
Mike Marshall5db11c22015-07-17 10:38:12 -0400611
612 /*
Yi Liu8bb8aef2015-11-24 15:12:14 -0500613 * remount all mounted orangefs volumes to regain the lost
Mike Marshall5db11c22015-07-17 10:38:12 -0400614 * dynamic mount tables (if any) -- NOTE: this is done
615 * without keeping the superblock list locked due to the
Mike Marshalladcf34a2016-02-24 16:54:27 -0500616 * upcall/downcall waiting. also, the request mutex is
Mike Marshall5db11c22015-07-17 10:38:12 -0400617 * used to ensure that no operations will be serviced until
618 * all of the remounts are serviced (to avoid ops between
619 * mounts to fail)
620 */
621 ret = mutex_lock_interruptible(&request_mutex);
622 if (ret < 0)
623 return ret;
624 gossip_debug(GOSSIP_DEV_DEBUG,
Mike Marshall97f10022015-12-11 16:45:03 -0500625 "%s: priority remount in progress\n",
626 __func__);
Al Viro45996492016-03-25 19:56:34 -0400627 spin_lock(&orangefs_superblocks_lock);
628 list_for_each_entry(orangefs_sb, &orangefs_superblocks, list) {
629 /*
630 * We have to drop the spinlock, so entries can be
631 * removed. They can't be freed, though, so we just
632 * keep the forward pointers and zero the back ones -
633 * that way we can get to the rest of the list.
634 */
635 if (!orangefs_sb->list.prev)
636 continue;
637 gossip_debug(GOSSIP_DEV_DEBUG,
638 "%s: Remounting SB %p\n",
639 __func__,
640 orangefs_sb);
Mike Marshall5db11c22015-07-17 10:38:12 -0400641
Al Viro45996492016-03-25 19:56:34 -0400642 spin_unlock(&orangefs_superblocks_lock);
643 ret = orangefs_remount(orangefs_sb);
644 spin_lock(&orangefs_superblocks_lock);
645 if (ret) {
646 gossip_debug(GOSSIP_DEV_DEBUG,
647 "SB %p remount failed\n",
648 orangefs_sb);
649 break;
Mike Marshall5db11c22015-07-17 10:38:12 -0400650 }
651 }
Al Viro45996492016-03-25 19:56:34 -0400652 spin_unlock(&orangefs_superblocks_lock);
Mike Marshall5db11c22015-07-17 10:38:12 -0400653 gossip_debug(GOSSIP_DEV_DEBUG,
Mike Marshall97f10022015-12-11 16:45:03 -0500654 "%s: priority remount complete\n",
655 __func__);
Mike Marshall5db11c22015-07-17 10:38:12 -0400656 mutex_unlock(&request_mutex);
657 return ret;
658
Yi Liu8bb8aef2015-11-24 15:12:14 -0500659 case ORANGEFS_DEV_UPSTREAM:
Mike Marshall5db11c22015-07-17 10:38:12 -0400660 ret = copy_to_user((void __user *)arg,
661 &upstream_kmod,
662 sizeof(upstream_kmod));
663
664 if (ret != 0)
665 return -EIO;
666 else
667 return ret;
668
Yi Liu8bb8aef2015-11-24 15:12:14 -0500669 case ORANGEFS_DEV_CLIENT_MASK:
Martin Brandenburg44f46412016-08-15 11:38:36 -0400670 return orangefs_debugfs_new_client_mask((void __user *)arg);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500671 case ORANGEFS_DEV_CLIENT_STRING:
Martin Brandenburg44f46412016-08-15 11:38:36 -0400672 return orangefs_debugfs_new_client_string((void __user *)arg);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500673 case ORANGEFS_DEV_DEBUG:
Martin Brandenburg44f46412016-08-15 11:38:36 -0400674 return orangefs_debugfs_new_debug((void __user *)arg);
Mike Marshall5db11c22015-07-17 10:38:12 -0400675 default:
676 return -ENOIOCTLCMD;
677 }
678 return -ENOIOCTLCMD;
679}
680
Yi Liu8bb8aef2015-11-24 15:12:14 -0500681static long orangefs_devreq_ioctl(struct file *file,
Mike Marshall5db11c22015-07-17 10:38:12 -0400682 unsigned int command, unsigned long arg)
683{
684 long ret;
685
686 /* Check for properly constructed commands */
687 ret = check_ioctl_command(command);
688 if (ret < 0)
689 return (int)ret;
690
691 return (int)dispatch_ioctl_command(command, arg);
692}
693
694#ifdef CONFIG_COMPAT /* CONFIG_COMPAT is in .config */
695
Yi Liu8bb8aef2015-11-24 15:12:14 -0500696/* Compat structure for the ORANGEFS_DEV_MAP ioctl */
697struct ORANGEFS_dev_map_desc32 {
Mike Marshall5db11c22015-07-17 10:38:12 -0400698 compat_uptr_t ptr;
699 __s32 total_size;
700 __s32 size;
701 __s32 count;
702};
703
704static unsigned long translate_dev_map26(unsigned long args, long *error)
705{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500706 struct ORANGEFS_dev_map_desc32 __user *p32 = (void __user *)args;
Mike Marshall5db11c22015-07-17 10:38:12 -0400707 /*
708 * Depending on the architecture, allocate some space on the
709 * user-call-stack based on our expected layout.
710 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500711 struct ORANGEFS_dev_map_desc __user *p =
Mike Marshall5db11c22015-07-17 10:38:12 -0400712 compat_alloc_user_space(sizeof(*p));
Mike Marshall84d02152015-07-28 13:27:51 -0400713 compat_uptr_t addr;
Mike Marshall5db11c22015-07-17 10:38:12 -0400714
715 *error = 0;
716 /* get the ptr from the 32 bit user-space */
717 if (get_user(addr, &p32->ptr))
718 goto err;
719 /* try to put that into a 64-bit layout */
720 if (put_user(compat_ptr(addr), &p->ptr))
721 goto err;
722 /* copy the remaining fields */
723 if (copy_in_user(&p->total_size, &p32->total_size, sizeof(__s32)))
724 goto err;
725 if (copy_in_user(&p->size, &p32->size, sizeof(__s32)))
726 goto err;
727 if (copy_in_user(&p->count, &p32->count, sizeof(__s32)))
728 goto err;
729 return (unsigned long)p;
730err:
731 *error = -EFAULT;
732 return 0;
733}
734
735/*
736 * 32 bit user-space apps' ioctl handlers when kernel modules
737 * is compiled as a 64 bit one
738 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500739static long orangefs_devreq_compat_ioctl(struct file *filp, unsigned int cmd,
Mike Marshall5db11c22015-07-17 10:38:12 -0400740 unsigned long args)
741{
742 long ret;
743 unsigned long arg = args;
744
745 /* Check for properly constructed commands */
746 ret = check_ioctl_command(cmd);
747 if (ret < 0)
748 return ret;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500749 if (cmd == ORANGEFS_DEV_MAP) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400750 /*
751 * convert the arguments to what we expect internally
752 * in kernel space
753 */
754 arg = translate_dev_map26(args, &ret);
755 if (ret < 0) {
756 gossip_err("Could not translate dev map\n");
757 return ret;
758 }
759 }
760 /* no other ioctl requires translation */
761 return dispatch_ioctl_command(cmd, arg);
762}
763
Mike Marshall2c590d52015-07-24 10:37:15 -0400764#endif /* CONFIG_COMPAT is in .config */
765
Mike Marshall5db11c22015-07-17 10:38:12 -0400766/* the assigned character device major number */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500767static int orangefs_dev_major;
Mike Marshall5db11c22015-07-17 10:38:12 -0400768
769/*
Yi Liu8bb8aef2015-11-24 15:12:14 -0500770 * Initialize orangefs device specific state:
Mike Marshall5db11c22015-07-17 10:38:12 -0400771 * Must be called at module load time only
772 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500773int orangefs_dev_init(void)
Mike Marshall5db11c22015-07-17 10:38:12 -0400774{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500775 /* register orangefs-req device */
776 orangefs_dev_major = register_chrdev(0,
777 ORANGEFS_REQDEVICE_NAME,
778 &orangefs_devreq_file_operations);
779 if (orangefs_dev_major < 0) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400780 gossip_debug(GOSSIP_DEV_DEBUG,
781 "Failed to register /dev/%s (error %d)\n",
Yi Liu8bb8aef2015-11-24 15:12:14 -0500782 ORANGEFS_REQDEVICE_NAME, orangefs_dev_major);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500783 return orangefs_dev_major;
Mike Marshall5db11c22015-07-17 10:38:12 -0400784 }
785
786 gossip_debug(GOSSIP_DEV_DEBUG,
787 "*** /dev/%s character device registered ***\n",
Yi Liu8bb8aef2015-11-24 15:12:14 -0500788 ORANGEFS_REQDEVICE_NAME);
Mike Marshall5db11c22015-07-17 10:38:12 -0400789 gossip_debug(GOSSIP_DEV_DEBUG, "'mknod /dev/%s c %d 0'.\n",
Yi Liu8bb8aef2015-11-24 15:12:14 -0500790 ORANGEFS_REQDEVICE_NAME, orangefs_dev_major);
Mike Marshall5db11c22015-07-17 10:38:12 -0400791 return 0;
792}
793
Yi Liu8bb8aef2015-11-24 15:12:14 -0500794void orangefs_dev_cleanup(void)
Mike Marshall5db11c22015-07-17 10:38:12 -0400795{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500796 unregister_chrdev(orangefs_dev_major, ORANGEFS_REQDEVICE_NAME);
Mike Marshall5db11c22015-07-17 10:38:12 -0400797 gossip_debug(GOSSIP_DEV_DEBUG,
798 "*** /dev/%s character device unregistered ***\n",
Yi Liu8bb8aef2015-11-24 15:12:14 -0500799 ORANGEFS_REQDEVICE_NAME);
Mike Marshall5db11c22015-07-17 10:38:12 -0400800}
801
Yi Liu8bb8aef2015-11-24 15:12:14 -0500802static unsigned int orangefs_devreq_poll(struct file *file,
Mike Marshall5db11c22015-07-17 10:38:12 -0400803 struct poll_table_struct *poll_table)
804{
805 int poll_revent_mask = 0;
806
Al Viro83595db2016-01-19 12:03:05 -0500807 poll_wait(file, &orangefs_request_list_waitq, poll_table);
Mike Marshall5db11c22015-07-17 10:38:12 -0400808
Al Viro83595db2016-01-19 12:03:05 -0500809 if (!list_empty(&orangefs_request_list))
810 poll_revent_mask |= POLL_IN;
Mike Marshall5db11c22015-07-17 10:38:12 -0400811 return poll_revent_mask;
812}
813
Yi Liu8bb8aef2015-11-24 15:12:14 -0500814const struct file_operations orangefs_devreq_file_operations = {
Mike Marshall5db11c22015-07-17 10:38:12 -0400815 .owner = THIS_MODULE,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500816 .read = orangefs_devreq_read,
817 .write_iter = orangefs_devreq_write_iter,
818 .open = orangefs_devreq_open,
819 .release = orangefs_devreq_release,
820 .unlocked_ioctl = orangefs_devreq_ioctl,
Mike Marshall5db11c22015-07-17 10:38:12 -0400821
822#ifdef CONFIG_COMPAT /* CONFIG_COMPAT is in .config */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500823 .compat_ioctl = orangefs_devreq_compat_ioctl,
Mike Marshall5db11c22015-07-17 10:38:12 -0400824#endif
Yi Liu8bb8aef2015-11-24 15:12:14 -0500825 .poll = orangefs_devreq_poll
Mike Marshall5db11c22015-07-17 10:38:12 -0400826};