blob: 641e27152d7ea8951234c61b2ae74e93c58a561f [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
Martin Brandenburga0fe0512016-08-15 15:21:16 -040023static DEFINE_MUTEX(devreq_mutex);
24
Mike Marshall5db11c22015-07-17 10:38:12 -040025#define DUMP_DEVICE_ERROR() \
26do { \
27 gossip_err("*****************************************************\n");\
Yi Liu8bb8aef2015-11-24 15:12:14 -050028 gossip_err("ORANGEFS Device Error: You cannot open the device file "); \
Mike Marshall5db11c22015-07-17 10:38:12 -040029 gossip_err("\n/dev/%s more than once. Please make sure that\nthere " \
Yi Liu8bb8aef2015-11-24 15:12:14 -050030 "are no ", ORANGEFS_REQDEVICE_NAME); \
Mike Marshall5db11c22015-07-17 10:38:12 -040031 gossip_err("instances of a program using this device\ncurrently " \
32 "running. (You must verify this!)\n"); \
33 gossip_err("For example, you can use the lsof program as follows:\n");\
34 gossip_err("'lsof | grep %s' (run this as root)\n", \
Yi Liu8bb8aef2015-11-24 15:12:14 -050035 ORANGEFS_REQDEVICE_NAME); \
Mike Marshall5db11c22015-07-17 10:38:12 -040036 gossip_err(" open_access_count = %d\n", open_access_count); \
37 gossip_err("*****************************************************\n");\
38} while (0)
39
40static int hash_func(__u64 tag, int table_size)
41{
Mike Marshall2c590d52015-07-24 10:37:15 -040042 return do_div(tag, (unsigned int)table_size);
Mike Marshall5db11c22015-07-17 10:38:12 -040043}
44
Yi Liu8bb8aef2015-11-24 15:12:14 -050045static void orangefs_devreq_add_op(struct orangefs_kernel_op_s *op)
Mike Marshall5db11c22015-07-17 10:38:12 -040046{
47 int index = hash_func(op->tag, hash_table_size);
48
Martin Brandenburg1d503612016-08-16 11:38:14 -040049 list_add_tail(&op->list, &orangefs_htable_ops_in_progress[index]);
Mike Marshall5db11c22015-07-17 10:38:12 -040050}
51
Mike Marshallca9f5182016-02-26 10:21:12 -050052/*
53 * find the op with this tag and remove it from the in progress
54 * hash table.
55 */
Yi Liu8bb8aef2015-11-24 15:12:14 -050056static struct orangefs_kernel_op_s *orangefs_devreq_remove_op(__u64 tag)
Mike Marshall5db11c22015-07-17 10:38:12 -040057{
Yi Liu8bb8aef2015-11-24 15:12:14 -050058 struct orangefs_kernel_op_s *op, *next;
Mike Marshall5db11c22015-07-17 10:38:12 -040059 int index;
60
61 index = hash_func(tag, hash_table_size);
62
Martin Brandenburg1d503612016-08-16 11:38:14 -040063 spin_lock(&orangefs_htable_ops_in_progress_lock);
Mike Marshall5db11c22015-07-17 10:38:12 -040064 list_for_each_entry_safe(op,
65 next,
Martin Brandenburg1d503612016-08-16 11:38:14 -040066 &orangefs_htable_ops_in_progress[index],
Mike Marshall5db11c22015-07-17 10:38:12 -040067 list) {
Al Viro05a50a52016-02-18 18:59:44 -050068 if (op->tag == tag && !op_state_purged(op) &&
69 !op_state_given_up(op)) {
Al Viroed42fe02016-01-22 19:47:47 -050070 list_del_init(&op->list);
Martin Brandenburg1d503612016-08-16 11:38:14 -040071 spin_unlock(&orangefs_htable_ops_in_progress_lock);
Mike Marshall5db11c22015-07-17 10:38:12 -040072 return op;
73 }
74 }
75
Martin Brandenburg1d503612016-08-16 11:38:14 -040076 spin_unlock(&orangefs_htable_ops_in_progress_lock);
Mike Marshall5db11c22015-07-17 10:38:12 -040077 return NULL;
78}
79
Martin Brandenburgacfcbaf2016-03-05 13:17:39 -050080/* Returns whether any FS are still pending remounted */
81static int mark_all_pending_mounts(void)
82{
83 int unmounted = 1;
84 struct orangefs_sb_info_s *orangefs_sb = NULL;
85
86 spin_lock(&orangefs_superblocks_lock);
87 list_for_each_entry(orangefs_sb, &orangefs_superblocks, list) {
88 /* All of these file system require a remount */
89 orangefs_sb->mount_pending = 1;
90 unmounted = 0;
91 }
92 spin_unlock(&orangefs_superblocks_lock);
93 return unmounted;
94}
95
96/*
97 * Determine if a given file system needs to be remounted or not
98 * Returns -1 on error
99 * 0 if already mounted
100 * 1 if needs remount
101 */
102static int fs_mount_pending(__s32 fsid)
103{
104 int mount_pending = -1;
105 struct orangefs_sb_info_s *orangefs_sb = NULL;
106
107 spin_lock(&orangefs_superblocks_lock);
108 list_for_each_entry(orangefs_sb, &orangefs_superblocks, list) {
109 if (orangefs_sb->fs_id == fsid) {
110 mount_pending = orangefs_sb->mount_pending;
111 break;
112 }
113 }
114 spin_unlock(&orangefs_superblocks_lock);
115 return mount_pending;
116}
117
Yi Liu8bb8aef2015-11-24 15:12:14 -0500118static int orangefs_devreq_open(struct inode *inode, struct file *file)
Mike Marshall5db11c22015-07-17 10:38:12 -0400119{
120 int ret = -EINVAL;
121
Jann Horn78fee0b2016-06-25 01:51:52 +0200122 /* in order to ensure that the filesystem driver sees correct UIDs */
123 if (file->f_cred->user_ns != &init_user_ns) {
124 gossip_err("%s: device cannot be opened outside init_user_ns\n",
125 __func__);
126 goto out;
127 }
128
Mike Marshall5db11c22015-07-17 10:38:12 -0400129 if (!(file->f_flags & O_NONBLOCK)) {
Mike Marshall97f10022015-12-11 16:45:03 -0500130 gossip_err("%s: device cannot be opened in blocking mode\n",
131 __func__);
Mike Marshall5db11c22015-07-17 10:38:12 -0400132 goto out;
133 }
134 ret = -EACCES;
Mike Marshall97f10022015-12-11 16:45:03 -0500135 gossip_debug(GOSSIP_DEV_DEBUG, "client-core: opening device\n");
Mike Marshall5db11c22015-07-17 10:38:12 -0400136 mutex_lock(&devreq_mutex);
137
138 if (open_access_count == 0) {
Al Virofee25ce2016-01-22 19:46:08 -0500139 open_access_count = 1;
Al Virofb6d2522016-01-19 12:00:26 -0500140 ret = 0;
Mike Marshall5db11c22015-07-17 10:38:12 -0400141 } else {
142 DUMP_DEVICE_ERROR();
143 }
144 mutex_unlock(&devreq_mutex);
145
146out:
147
148 gossip_debug(GOSSIP_DEV_DEBUG,
149 "pvfs2-client-core: open device complete (ret = %d)\n",
150 ret);
151 return ret;
152}
153
Mike Marshall97f10022015-12-11 16:45:03 -0500154/* Function for read() callers into the device */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500155static ssize_t orangefs_devreq_read(struct file *file,
Mike Marshall5db11c22015-07-17 10:38:12 -0400156 char __user *buf,
157 size_t count, loff_t *offset)
158{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500159 struct orangefs_kernel_op_s *op, *temp;
160 __s32 proto_ver = ORANGEFS_KERNEL_PROTO_VERSION;
161 static __s32 magic = ORANGEFS_DEVREQ_MAGIC;
162 struct orangefs_kernel_op_s *cur_op = NULL;
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500163 unsigned long ret;
Mike Marshall5db11c22015-07-17 10:38:12 -0400164
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500165 /* We do not support blocking IO. */
Mike Marshall5db11c22015-07-17 10:38:12 -0400166 if (!(file->f_flags & O_NONBLOCK)) {
Mike Marshall97f10022015-12-11 16:45:03 -0500167 gossip_err("%s: blocking read from client-core.\n",
168 __func__);
Mike Marshall5db11c22015-07-17 10:38:12 -0400169 return -EINVAL;
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500170 }
171
172 /*
Martin Brandenburga762ae62015-12-15 14:22:06 -0500173 * The client will do an ioctl to find MAX_DEV_REQ_UPSIZE, then
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500174 * always read with that size buffer.
175 */
Martin Brandenburga762ae62015-12-15 14:22:06 -0500176 if (count != MAX_DEV_REQ_UPSIZE) {
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500177 gossip_err("orangefs: client-core tried to read wrong size\n");
178 return -EINVAL;
179 }
180
Al Viroed42fe02016-01-22 19:47:47 -0500181restart:
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500182 /* Get next op (if any) from top of list. */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500183 spin_lock(&orangefs_request_list_lock);
184 list_for_each_entry_safe(op, temp, &orangefs_request_list, list) {
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500185 __s32 fsid;
186 /* This lock is held past the end of the loop when we break. */
187 spin_lock(&op->lock);
Al Viro05a50a52016-02-18 18:59:44 -0500188 if (unlikely(op_state_purged(op) || op_state_given_up(op))) {
Al Viroed42fe02016-01-22 19:47:47 -0500189 spin_unlock(&op->lock);
190 continue;
191 }
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500192
193 fsid = fsid_of_op(op);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500194 if (fsid != ORANGEFS_FS_ID_NULL) {
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500195 int ret;
196 /* Skip ops whose filesystem needs to be mounted. */
197 ret = fs_mount_pending(fsid);
198 if (ret == 1) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400199 gossip_debug(GOSSIP_DEV_DEBUG,
Mike Marshall5090c962016-02-04 13:29:27 -0500200 "%s: mount pending, skipping op tag "
201 "%llu %s\n",
202 __func__,
203 llu(op->tag),
204 get_opname_string(op));
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500205 spin_unlock(&op->lock);
Mike Marshall5db11c22015-07-17 10:38:12 -0400206 continue;
Mike Marshall97f10022015-12-11 16:45:03 -0500207 /*
208 * Skip ops whose filesystem we don't know about unless
209 * it is being mounted.
210 */
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500211 /* XXX: is there a better way to detect this? */
212 } else if (ret == -1 &&
Mike Marshall97f10022015-12-11 16:45:03 -0500213 !(op->upcall.type ==
214 ORANGEFS_VFS_OP_FS_MOUNT ||
215 op->upcall.type ==
216 ORANGEFS_VFS_OP_GETATTR)) {
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500217 gossip_debug(GOSSIP_DEV_DEBUG,
218 "orangefs: skipping op tag %llu %s\n",
219 llu(op->tag), get_opname_string(op));
220 gossip_err(
221 "orangefs: ERROR: fs_mount_pending %d\n",
222 fsid);
223 spin_unlock(&op->lock);
224 continue;
Mike Marshall5db11c22015-07-17 10:38:12 -0400225 }
226 }
Mike Marshall5db11c22015-07-17 10:38:12 -0400227 /*
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500228 * Either this op does not pertain to a filesystem, is mounting
229 * a filesystem, or pertains to a mounted filesystem. Let it
230 * through.
Mike Marshall5db11c22015-07-17 10:38:12 -0400231 */
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500232 cur_op = op;
233 break;
Mike Marshall5db11c22015-07-17 10:38:12 -0400234 }
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500235
236 /*
237 * At this point we either have a valid op and can continue or have not
238 * found an op and must ask the client to try again later.
239 */
240 if (!cur_op) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500241 spin_unlock(&orangefs_request_list_lock);
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500242 return -EAGAIN;
243 }
244
Mike Marshallca9f5182016-02-26 10:21:12 -0500245 gossip_debug(GOSSIP_DEV_DEBUG, "%s: reading op tag %llu %s\n",
246 __func__,
247 llu(cur_op->tag),
248 get_opname_string(cur_op));
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500249
250 /*
251 * Such an op should never be on the list in the first place. If so, we
252 * will abort.
253 */
254 if (op_state_in_progress(cur_op) || op_state_serviced(cur_op)) {
255 gossip_err("orangefs: ERROR: Current op already queued.\n");
Al Viro05a50a52016-02-18 18:59:44 -0500256 list_del_init(&cur_op->list);
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500257 spin_unlock(&cur_op->lock);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500258 spin_unlock(&orangefs_request_list_lock);
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500259 return -EAGAIN;
260 }
Mike Marshallca9f5182016-02-26 10:21:12 -0500261
Al Viroed42fe02016-01-22 19:47:47 -0500262 list_del_init(&cur_op->list);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500263 spin_unlock(&orangefs_request_list_lock);
Al Viroed42fe02016-01-22 19:47:47 -0500264
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500265 spin_unlock(&cur_op->lock);
266
267 /* Push the upcall out. */
268 ret = copy_to_user(buf, &proto_ver, sizeof(__s32));
269 if (ret != 0)
270 goto error;
271 ret = copy_to_user(buf+sizeof(__s32), &magic, sizeof(__s32));
272 if (ret != 0)
273 goto error;
274 ret = copy_to_user(buf+2 * sizeof(__s32), &cur_op->tag, sizeof(__u64));
275 if (ret != 0)
276 goto error;
277 ret = copy_to_user(buf+2*sizeof(__s32)+sizeof(__u64), &cur_op->upcall,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500278 sizeof(struct orangefs_upcall_s));
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500279 if (ret != 0)
280 goto error;
281
Martin Brandenburg1d503612016-08-16 11:38:14 -0400282 spin_lock(&orangefs_htable_ops_in_progress_lock);
Al Viroed42fe02016-01-22 19:47:47 -0500283 spin_lock(&cur_op->lock);
284 if (unlikely(op_state_given_up(cur_op))) {
285 spin_unlock(&cur_op->lock);
Martin Brandenburg1d503612016-08-16 11:38:14 -0400286 spin_unlock(&orangefs_htable_ops_in_progress_lock);
Al Viro05a50a52016-02-18 18:59:44 -0500287 complete(&cur_op->waitq);
Al Viroed42fe02016-01-22 19:47:47 -0500288 goto restart;
289 }
290
291 /*
292 * Set the operation to be in progress and move it between lists since
293 * it has been sent to the client.
294 */
295 set_op_state_inprogress(cur_op);
Mike Marshall9d9e7ba2016-03-03 13:46:48 -0500296 gossip_debug(GOSSIP_DEV_DEBUG,
297 "%s: 1 op:%s: op_state:%d: process:%s:\n",
298 __func__,
299 get_opname_string(cur_op),
300 cur_op->op_state,
301 current->comm);
Al Viroed42fe02016-01-22 19:47:47 -0500302 orangefs_devreq_add_op(cur_op);
303 spin_unlock(&cur_op->lock);
Martin Brandenburg1d503612016-08-16 11:38:14 -0400304 spin_unlock(&orangefs_htable_ops_in_progress_lock);
Al Viroed42fe02016-01-22 19:47:47 -0500305
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500306 /* The client only asks to read one size buffer. */
Martin Brandenburga762ae62015-12-15 14:22:06 -0500307 return MAX_DEV_REQ_UPSIZE;
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500308error:
309 /*
310 * We were unable to copy the op data to the client. Put the op back in
311 * list. If client has crashed, the op will be purged later when the
312 * device is released.
313 */
314 gossip_err("orangefs: Failed to copy data to user space\n");
Yi Liu8bb8aef2015-11-24 15:12:14 -0500315 spin_lock(&orangefs_request_list_lock);
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500316 spin_lock(&cur_op->lock);
Al Viroed42fe02016-01-22 19:47:47 -0500317 if (likely(!op_state_given_up(cur_op))) {
318 set_op_state_waiting(cur_op);
Mike Marshall9d9e7ba2016-03-03 13:46:48 -0500319 gossip_debug(GOSSIP_DEV_DEBUG,
320 "%s: 2 op:%s: op_state:%d: process:%s:\n",
321 __func__,
322 get_opname_string(cur_op),
323 cur_op->op_state,
324 current->comm);
Al Viroed42fe02016-01-22 19:47:47 -0500325 list_add(&cur_op->list, &orangefs_request_list);
Al Viro05a50a52016-02-18 18:59:44 -0500326 spin_unlock(&cur_op->lock);
327 } else {
328 spin_unlock(&cur_op->lock);
329 complete(&cur_op->waitq);
Al Viroed42fe02016-01-22 19:47:47 -0500330 }
Yi Liu8bb8aef2015-11-24 15:12:14 -0500331 spin_unlock(&orangefs_request_list_lock);
Martin Brandenburg24c8d082015-11-13 14:26:10 -0500332 return -EFAULT;
Mike Marshall5db11c22015-07-17 10:38:12 -0400333}
334
Mike Marshall97f10022015-12-11 16:45:03 -0500335/*
Mike Marshallb3ae4752016-01-13 11:18:12 -0500336 * Function for writev() callers into the device.
337 *
338 * Userspace should have written:
339 * - __u32 version
340 * - __u32 magic
341 * - __u64 tag
342 * - struct orangefs_downcall_s
343 * - trailer buffer (in the case of READDIR operations)
Mike Marshall97f10022015-12-11 16:45:03 -0500344 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500345static ssize_t orangefs_devreq_write_iter(struct kiocb *iocb,
Mike Marshall5db11c22015-07-17 10:38:12 -0400346 struct iov_iter *iter)
347{
Mike Marshallb3ae4752016-01-13 11:18:12 -0500348 ssize_t ret;
349 struct orangefs_kernel_op_s *op = NULL;
350 struct {
351 __u32 version;
352 __u32 magic;
353 __u64 tag;
354 } head;
355 int total = ret = iov_iter_count(iter);
356 int n;
357 int downcall_size = sizeof(struct orangefs_downcall_s);
358 int head_size = sizeof(head);
359
360 gossip_debug(GOSSIP_DEV_DEBUG, "%s: total:%d: ret:%zd:\n",
361 __func__,
362 total,
363 ret);
364
365 if (total < MAX_DEV_REQ_DOWNSIZE) {
Mike Marshallcf0c2772016-01-19 12:04:40 -0500366 gossip_err("%s: total:%d: must be at least:%u:\n",
Mike Marshallb3ae4752016-01-13 11:18:12 -0500367 __func__,
368 total,
Mike Marshallcf0c2772016-01-19 12:04:40 -0500369 (unsigned int) MAX_DEV_REQ_DOWNSIZE);
Al Viroed42fe02016-01-22 19:47:47 -0500370 return -EFAULT;
Mike Marshallb3ae4752016-01-13 11:18:12 -0500371 }
372
373 n = copy_from_iter(&head, head_size, iter);
374 if (n < head_size) {
375 gossip_err("%s: failed to copy head.\n", __func__);
Al Viroed42fe02016-01-22 19:47:47 -0500376 return -EFAULT;
Mike Marshallb3ae4752016-01-13 11:18:12 -0500377 }
378
379 if (head.version < ORANGEFS_MINIMUM_USERSPACE_VERSION) {
380 gossip_err("%s: userspace claims version"
381 "%d, minimum version required: %d.\n",
382 __func__,
383 head.version,
384 ORANGEFS_MINIMUM_USERSPACE_VERSION);
Al Viroed42fe02016-01-22 19:47:47 -0500385 return -EPROTO;
Mike Marshallb3ae4752016-01-13 11:18:12 -0500386 }
387
388 if (head.magic != ORANGEFS_DEVREQ_MAGIC) {
389 gossip_err("Error: Device magic number does not match.\n");
Al Viroed42fe02016-01-22 19:47:47 -0500390 return -EPROTO;
Mike Marshallb3ae4752016-01-13 11:18:12 -0500391 }
392
Mike Marshallca9f5182016-02-26 10:21:12 -0500393 /* remove the op from the in progress hash table */
Mike Marshallb3ae4752016-01-13 11:18:12 -0500394 op = orangefs_devreq_remove_op(head.tag);
395 if (!op) {
396 gossip_err("WARNING: No one's waiting for tag %llu\n",
397 llu(head.tag));
Al Viroed42fe02016-01-22 19:47:47 -0500398 return ret;
Mike Marshallb3ae4752016-01-13 11:18:12 -0500399 }
400
Mike Marshallb3ae4752016-01-13 11:18:12 -0500401 n = copy_from_iter(&op->downcall, downcall_size, iter);
402 if (n != downcall_size) {
403 gossip_err("%s: failed to copy downcall.\n", __func__);
Al Viro5964c1b2016-02-18 18:53:41 -0500404 goto Efault;
Mike Marshallb3ae4752016-01-13 11:18:12 -0500405 }
406
407 if (op->downcall.status)
408 goto wakeup;
409
410 /*
411 * We've successfully peeled off the head and the downcall.
412 * Something has gone awry if total doesn't equal the
413 * sum of head_size, downcall_size and trailer_size.
414 */
415 if ((head_size + downcall_size + op->downcall.trailer_size) != total) {
416 gossip_err("%s: funky write, head_size:%d"
417 ": downcall_size:%d: trailer_size:%lld"
418 ": total size:%d:\n",
419 __func__,
420 head_size,
421 downcall_size,
422 op->downcall.trailer_size,
423 total);
Al Viro5964c1b2016-02-18 18:53:41 -0500424 goto Efault;
Mike Marshallb3ae4752016-01-13 11:18:12 -0500425 }
426
427 /* Only READDIR operations should have trailers. */
428 if ((op->downcall.type != ORANGEFS_VFS_OP_READDIR) &&
429 (op->downcall.trailer_size != 0)) {
430 gossip_err("%s: %x operation with trailer.",
431 __func__,
432 op->downcall.type);
Al Viro5964c1b2016-02-18 18:53:41 -0500433 goto Efault;
Mike Marshallb3ae4752016-01-13 11:18:12 -0500434 }
435
436 /* READDIR operations should always have trailers. */
437 if ((op->downcall.type == ORANGEFS_VFS_OP_READDIR) &&
438 (op->downcall.trailer_size == 0)) {
439 gossip_err("%s: %x operation with no trailer.",
440 __func__,
441 op->downcall.type);
Al Viro5964c1b2016-02-18 18:53:41 -0500442 goto Efault;
Mike Marshallb3ae4752016-01-13 11:18:12 -0500443 }
444
445 if (op->downcall.type != ORANGEFS_VFS_OP_READDIR)
446 goto wakeup;
447
448 op->downcall.trailer_buf =
449 vmalloc(op->downcall.trailer_size);
450 if (op->downcall.trailer_buf == NULL) {
451 gossip_err("%s: failed trailer vmalloc.\n",
452 __func__);
Al Viro5964c1b2016-02-18 18:53:41 -0500453 goto Enomem;
Mike Marshallb3ae4752016-01-13 11:18:12 -0500454 }
455 memset(op->downcall.trailer_buf, 0, op->downcall.trailer_size);
456 n = copy_from_iter(op->downcall.trailer_buf,
457 op->downcall.trailer_size,
458 iter);
459 if (n != op->downcall.trailer_size) {
460 gossip_err("%s: failed to copy trailer.\n", __func__);
461 vfree(op->downcall.trailer_buf);
Al Viro5964c1b2016-02-18 18:53:41 -0500462 goto Efault;
Mike Marshallb3ae4752016-01-13 11:18:12 -0500463 }
464
465wakeup:
Al Viro2a9e5c22016-01-23 13:45:46 -0500466 /*
Mike Marshall9f08cfe2016-02-26 14:39:08 -0500467 * Return to vfs waitqueue, and back to service_operation
468 * through wait_for_matching_downcall.
Al Viro2a9e5c22016-01-23 13:45:46 -0500469 */
470 spin_lock(&op->lock);
Al Viro5964c1b2016-02-18 18:53:41 -0500471 if (unlikely(op_is_cancel(op))) {
Al Viro2a9e5c22016-01-23 13:45:46 -0500472 spin_unlock(&op->lock);
Al Viro78699e22016-02-11 23:07:19 -0500473 put_cancel(op);
Al Viro5964c1b2016-02-18 18:53:41 -0500474 } else if (unlikely(op_state_given_up(op))) {
475 spin_unlock(&op->lock);
Al Viro05a50a52016-02-18 18:59:44 -0500476 complete(&op->waitq);
Al Viro5964c1b2016-02-18 18:53:41 -0500477 } else {
478 set_op_state_serviced(op);
Mike Marshall9d9e7ba2016-03-03 13:46:48 -0500479 gossip_debug(GOSSIP_DEV_DEBUG,
480 "%s: op:%s: op_state:%d: process:%s:\n",
481 __func__,
482 get_opname_string(op),
483 op->op_state,
484 current->comm);
Al Viro5964c1b2016-02-18 18:53:41 -0500485 spin_unlock(&op->lock);
486 }
Mike Marshallb3ae4752016-01-13 11:18:12 -0500487 return ret;
Al Viroed42fe02016-01-22 19:47:47 -0500488
Al Viro5964c1b2016-02-18 18:53:41 -0500489Efault:
490 op->downcall.status = -(ORANGEFS_ERROR_BIT | 9);
491 ret = -EFAULT;
492 goto wakeup;
493
494Enomem:
495 op->downcall.status = -(ORANGEFS_ERROR_BIT | 8);
496 ret = -ENOMEM;
497 goto wakeup;
Mike Marshall5db11c22015-07-17 10:38:12 -0400498}
499
Mike Marshall5db11c22015-07-17 10:38:12 -0400500/*
501 * NOTE: gets called when the last reference to this device is dropped.
502 * Using the open_access_count variable, we enforce a reference count
503 * on this file so that it can be opened by only one process at a time.
504 * the devreq_mutex is used to make sure all i/o has completed
Yi Liu8bb8aef2015-11-24 15:12:14 -0500505 * before we call orangefs_bufmap_finalize, and similar such tricky
Mike Marshall5db11c22015-07-17 10:38:12 -0400506 * situations
507 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500508static int orangefs_devreq_release(struct inode *inode, struct file *file)
Mike Marshall5db11c22015-07-17 10:38:12 -0400509{
510 int unmounted = 0;
511
512 gossip_debug(GOSSIP_DEV_DEBUG,
513 "%s:pvfs2-client-core: exiting, closing device\n",
514 __func__);
515
516 mutex_lock(&devreq_mutex);
Al Viroea2c9c92016-02-13 21:01:21 -0500517 orangefs_bufmap_finalize();
Mike Marshall5db11c22015-07-17 10:38:12 -0400518
Al Virofee25ce2016-01-22 19:46:08 -0500519 open_access_count = -1;
Mike Marshall5db11c22015-07-17 10:38:12 -0400520
521 unmounted = mark_all_pending_mounts();
Yi Liu8bb8aef2015-11-24 15:12:14 -0500522 gossip_debug(GOSSIP_DEV_DEBUG, "ORANGEFS Device Close: Filesystem(s) %s\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400523 (unmounted ? "UNMOUNTED" : "MOUNTED"));
Mike Marshall5db11c22015-07-17 10:38:12 -0400524
Mike Marshall5db11c22015-07-17 10:38:12 -0400525 purge_waiting_ops();
Mike Marshall5db11c22015-07-17 10:38:12 -0400526 purge_inprogress_ops();
Al Viroea2c9c92016-02-13 21:01:21 -0500527
528 orangefs_bufmap_run_down();
529
Mike Marshall5db11c22015-07-17 10:38:12 -0400530 gossip_debug(GOSSIP_DEV_DEBUG,
531 "pvfs2-client-core: device close complete\n");
Al Virofee25ce2016-01-22 19:46:08 -0500532 open_access_count = 0;
533 mutex_unlock(&devreq_mutex);
Mike Marshall5db11c22015-07-17 10:38:12 -0400534 return 0;
535}
536
537int is_daemon_in_service(void)
538{
539 int in_service;
540
541 /*
542 * What this function does is checks if client-core is alive
543 * based on the access count we maintain on the device.
544 */
545 mutex_lock(&devreq_mutex);
546 in_service = open_access_count == 1 ? 0 : -EIO;
547 mutex_unlock(&devreq_mutex);
548 return in_service;
549}
550
Al Viro78699e22016-02-11 23:07:19 -0500551bool __is_daemon_in_service(void)
552{
553 return open_access_count == 1;
554}
555
Mike Marshall5db11c22015-07-17 10:38:12 -0400556static inline long check_ioctl_command(unsigned int command)
557{
558 /* Check for valid ioctl codes */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500559 if (_IOC_TYPE(command) != ORANGEFS_DEV_MAGIC) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400560 gossip_err("device ioctl magic numbers don't match! Did you rebuild pvfs2-client-core/libpvfs2? [cmd %x, magic %x != %x]\n",
561 command,
562 _IOC_TYPE(command),
Yi Liu8bb8aef2015-11-24 15:12:14 -0500563 ORANGEFS_DEV_MAGIC);
Mike Marshall5db11c22015-07-17 10:38:12 -0400564 return -EINVAL;
565 }
566 /* and valid ioctl commands */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500567 if (_IOC_NR(command) >= ORANGEFS_DEV_MAXNR || _IOC_NR(command) <= 0) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400568 gossip_err("Invalid ioctl command number [%d >= %d]\n",
Yi Liu8bb8aef2015-11-24 15:12:14 -0500569 _IOC_NR(command), ORANGEFS_DEV_MAXNR);
Mike Marshall5db11c22015-07-17 10:38:12 -0400570 return -ENOIOCTLCMD;
571 }
572 return 0;
573}
574
575static long dispatch_ioctl_command(unsigned int command, unsigned long arg)
576{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500577 static __s32 magic = ORANGEFS_DEVREQ_MAGIC;
Martin Brandenburga762ae62015-12-15 14:22:06 -0500578 static __s32 max_up_size = MAX_DEV_REQ_UPSIZE;
579 static __s32 max_down_size = MAX_DEV_REQ_DOWNSIZE;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500580 struct ORANGEFS_dev_map_desc user_desc;
Mike Marshall5db11c22015-07-17 10:38:12 -0400581 int ret = 0;
Mike Marshall5db11c22015-07-17 10:38:12 -0400582 int upstream_kmod = 1;
Al Viro45996492016-03-25 19:56:34 -0400583 struct orangefs_sb_info_s *orangefs_sb;
Mike Marshall5db11c22015-07-17 10:38:12 -0400584
585 /* mtmoore: add locking here */
586
587 switch (command) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500588 case ORANGEFS_DEV_GET_MAGIC:
Mike Marshall5db11c22015-07-17 10:38:12 -0400589 return ((put_user(magic, (__s32 __user *) arg) == -EFAULT) ?
590 -EIO :
591 0);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500592 case ORANGEFS_DEV_GET_MAX_UPSIZE:
Mike Marshall5db11c22015-07-17 10:38:12 -0400593 return ((put_user(max_up_size,
594 (__s32 __user *) arg) == -EFAULT) ?
595 -EIO :
596 0);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500597 case ORANGEFS_DEV_GET_MAX_DOWNSIZE:
Mike Marshall5db11c22015-07-17 10:38:12 -0400598 return ((put_user(max_down_size,
599 (__s32 __user *) arg) == -EFAULT) ?
600 -EIO :
601 0);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500602 case ORANGEFS_DEV_MAP:
Mike Marshall5db11c22015-07-17 10:38:12 -0400603 ret = copy_from_user(&user_desc,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500604 (struct ORANGEFS_dev_map_desc __user *)
Mike Marshall5db11c22015-07-17 10:38:12 -0400605 arg,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500606 sizeof(struct ORANGEFS_dev_map_desc));
Al Viroea2c9c92016-02-13 21:01:21 -0500607 /* WTF -EIO and not -EFAULT? */
608 return ret ? -EIO : orangefs_bufmap_initialize(&user_desc);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500609 case ORANGEFS_DEV_REMOUNT_ALL:
Mike Marshall5db11c22015-07-17 10:38:12 -0400610 gossip_debug(GOSSIP_DEV_DEBUG,
Mike Marshall97f10022015-12-11 16:45:03 -0500611 "%s: got ORANGEFS_DEV_REMOUNT_ALL\n",
612 __func__);
Mike Marshall5db11c22015-07-17 10:38:12 -0400613
614 /*
Yi Liu8bb8aef2015-11-24 15:12:14 -0500615 * remount all mounted orangefs volumes to regain the lost
Mike Marshall5db11c22015-07-17 10:38:12 -0400616 * dynamic mount tables (if any) -- NOTE: this is done
617 * without keeping the superblock list locked due to the
Mike Marshalladcf34a2016-02-24 16:54:27 -0500618 * upcall/downcall waiting. also, the request mutex is
Mike Marshall5db11c22015-07-17 10:38:12 -0400619 * used to ensure that no operations will be serviced until
620 * all of the remounts are serviced (to avoid ops between
621 * mounts to fail)
622 */
Martin Brandenburg1d503612016-08-16 11:38:14 -0400623 ret = mutex_lock_interruptible(&orangefs_request_mutex);
Mike Marshall5db11c22015-07-17 10:38:12 -0400624 if (ret < 0)
625 return ret;
626 gossip_debug(GOSSIP_DEV_DEBUG,
Mike Marshall97f10022015-12-11 16:45:03 -0500627 "%s: priority remount in progress\n",
628 __func__);
Al Viro45996492016-03-25 19:56:34 -0400629 spin_lock(&orangefs_superblocks_lock);
630 list_for_each_entry(orangefs_sb, &orangefs_superblocks, list) {
631 /*
632 * We have to drop the spinlock, so entries can be
633 * removed. They can't be freed, though, so we just
634 * keep the forward pointers and zero the back ones -
635 * that way we can get to the rest of the list.
636 */
637 if (!orangefs_sb->list.prev)
638 continue;
639 gossip_debug(GOSSIP_DEV_DEBUG,
640 "%s: Remounting SB %p\n",
641 __func__,
642 orangefs_sb);
Mike Marshall5db11c22015-07-17 10:38:12 -0400643
Al Viro45996492016-03-25 19:56:34 -0400644 spin_unlock(&orangefs_superblocks_lock);
645 ret = orangefs_remount(orangefs_sb);
646 spin_lock(&orangefs_superblocks_lock);
647 if (ret) {
648 gossip_debug(GOSSIP_DEV_DEBUG,
649 "SB %p remount failed\n",
650 orangefs_sb);
651 break;
Mike Marshall5db11c22015-07-17 10:38:12 -0400652 }
653 }
Al Viro45996492016-03-25 19:56:34 -0400654 spin_unlock(&orangefs_superblocks_lock);
Mike Marshall5db11c22015-07-17 10:38:12 -0400655 gossip_debug(GOSSIP_DEV_DEBUG,
Mike Marshall97f10022015-12-11 16:45:03 -0500656 "%s: priority remount complete\n",
657 __func__);
Martin Brandenburg1d503612016-08-16 11:38:14 -0400658 mutex_unlock(&orangefs_request_mutex);
Mike Marshall5db11c22015-07-17 10:38:12 -0400659 return ret;
660
Yi Liu8bb8aef2015-11-24 15:12:14 -0500661 case ORANGEFS_DEV_UPSTREAM:
Mike Marshall5db11c22015-07-17 10:38:12 -0400662 ret = copy_to_user((void __user *)arg,
663 &upstream_kmod,
664 sizeof(upstream_kmod));
665
666 if (ret != 0)
667 return -EIO;
668 else
669 return ret;
670
Yi Liu8bb8aef2015-11-24 15:12:14 -0500671 case ORANGEFS_DEV_CLIENT_MASK:
Martin Brandenburg44f46412016-08-15 11:38:36 -0400672 return orangefs_debugfs_new_client_mask((void __user *)arg);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500673 case ORANGEFS_DEV_CLIENT_STRING:
Martin Brandenburg44f46412016-08-15 11:38:36 -0400674 return orangefs_debugfs_new_client_string((void __user *)arg);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500675 case ORANGEFS_DEV_DEBUG:
Martin Brandenburg44f46412016-08-15 11:38:36 -0400676 return orangefs_debugfs_new_debug((void __user *)arg);
Mike Marshall5db11c22015-07-17 10:38:12 -0400677 default:
678 return -ENOIOCTLCMD;
679 }
680 return -ENOIOCTLCMD;
681}
682
Yi Liu8bb8aef2015-11-24 15:12:14 -0500683static long orangefs_devreq_ioctl(struct file *file,
Mike Marshall5db11c22015-07-17 10:38:12 -0400684 unsigned int command, unsigned long arg)
685{
686 long ret;
687
688 /* Check for properly constructed commands */
689 ret = check_ioctl_command(command);
690 if (ret < 0)
691 return (int)ret;
692
693 return (int)dispatch_ioctl_command(command, arg);
694}
695
696#ifdef CONFIG_COMPAT /* CONFIG_COMPAT is in .config */
697
Yi Liu8bb8aef2015-11-24 15:12:14 -0500698/* Compat structure for the ORANGEFS_DEV_MAP ioctl */
699struct ORANGEFS_dev_map_desc32 {
Mike Marshall5db11c22015-07-17 10:38:12 -0400700 compat_uptr_t ptr;
701 __s32 total_size;
702 __s32 size;
703 __s32 count;
704};
705
706static unsigned long translate_dev_map26(unsigned long args, long *error)
707{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500708 struct ORANGEFS_dev_map_desc32 __user *p32 = (void __user *)args;
Mike Marshall5db11c22015-07-17 10:38:12 -0400709 /*
710 * Depending on the architecture, allocate some space on the
711 * user-call-stack based on our expected layout.
712 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500713 struct ORANGEFS_dev_map_desc __user *p =
Mike Marshall5db11c22015-07-17 10:38:12 -0400714 compat_alloc_user_space(sizeof(*p));
Mike Marshall84d02152015-07-28 13:27:51 -0400715 compat_uptr_t addr;
Mike Marshall5db11c22015-07-17 10:38:12 -0400716
717 *error = 0;
718 /* get the ptr from the 32 bit user-space */
719 if (get_user(addr, &p32->ptr))
720 goto err;
721 /* try to put that into a 64-bit layout */
722 if (put_user(compat_ptr(addr), &p->ptr))
723 goto err;
724 /* copy the remaining fields */
725 if (copy_in_user(&p->total_size, &p32->total_size, sizeof(__s32)))
726 goto err;
727 if (copy_in_user(&p->size, &p32->size, sizeof(__s32)))
728 goto err;
729 if (copy_in_user(&p->count, &p32->count, sizeof(__s32)))
730 goto err;
731 return (unsigned long)p;
732err:
733 *error = -EFAULT;
734 return 0;
735}
736
737/*
738 * 32 bit user-space apps' ioctl handlers when kernel modules
739 * is compiled as a 64 bit one
740 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500741static long orangefs_devreq_compat_ioctl(struct file *filp, unsigned int cmd,
Mike Marshall5db11c22015-07-17 10:38:12 -0400742 unsigned long args)
743{
744 long ret;
745 unsigned long arg = args;
746
747 /* Check for properly constructed commands */
748 ret = check_ioctl_command(cmd);
749 if (ret < 0)
750 return ret;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500751 if (cmd == ORANGEFS_DEV_MAP) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400752 /*
753 * convert the arguments to what we expect internally
754 * in kernel space
755 */
756 arg = translate_dev_map26(args, &ret);
757 if (ret < 0) {
758 gossip_err("Could not translate dev map\n");
759 return ret;
760 }
761 }
762 /* no other ioctl requires translation */
763 return dispatch_ioctl_command(cmd, arg);
764}
765
Mike Marshall2c590d52015-07-24 10:37:15 -0400766#endif /* CONFIG_COMPAT is in .config */
767
Mike Marshall5db11c22015-07-17 10:38:12 -0400768/* the assigned character device major number */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500769static int orangefs_dev_major;
Mike Marshall5db11c22015-07-17 10:38:12 -0400770
771/*
Yi Liu8bb8aef2015-11-24 15:12:14 -0500772 * Initialize orangefs device specific state:
Mike Marshall5db11c22015-07-17 10:38:12 -0400773 * Must be called at module load time only
774 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500775int orangefs_dev_init(void)
Mike Marshall5db11c22015-07-17 10:38:12 -0400776{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500777 /* register orangefs-req device */
778 orangefs_dev_major = register_chrdev(0,
779 ORANGEFS_REQDEVICE_NAME,
780 &orangefs_devreq_file_operations);
781 if (orangefs_dev_major < 0) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400782 gossip_debug(GOSSIP_DEV_DEBUG,
783 "Failed to register /dev/%s (error %d)\n",
Yi Liu8bb8aef2015-11-24 15:12:14 -0500784 ORANGEFS_REQDEVICE_NAME, orangefs_dev_major);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500785 return orangefs_dev_major;
Mike Marshall5db11c22015-07-17 10:38:12 -0400786 }
787
788 gossip_debug(GOSSIP_DEV_DEBUG,
789 "*** /dev/%s character device registered ***\n",
Yi Liu8bb8aef2015-11-24 15:12:14 -0500790 ORANGEFS_REQDEVICE_NAME);
Mike Marshall5db11c22015-07-17 10:38:12 -0400791 gossip_debug(GOSSIP_DEV_DEBUG, "'mknod /dev/%s c %d 0'.\n",
Yi Liu8bb8aef2015-11-24 15:12:14 -0500792 ORANGEFS_REQDEVICE_NAME, orangefs_dev_major);
Mike Marshall5db11c22015-07-17 10:38:12 -0400793 return 0;
794}
795
Yi Liu8bb8aef2015-11-24 15:12:14 -0500796void orangefs_dev_cleanup(void)
Mike Marshall5db11c22015-07-17 10:38:12 -0400797{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500798 unregister_chrdev(orangefs_dev_major, ORANGEFS_REQDEVICE_NAME);
Mike Marshall5db11c22015-07-17 10:38:12 -0400799 gossip_debug(GOSSIP_DEV_DEBUG,
800 "*** /dev/%s character device unregistered ***\n",
Yi Liu8bb8aef2015-11-24 15:12:14 -0500801 ORANGEFS_REQDEVICE_NAME);
Mike Marshall5db11c22015-07-17 10:38:12 -0400802}
803
Yi Liu8bb8aef2015-11-24 15:12:14 -0500804static unsigned int orangefs_devreq_poll(struct file *file,
Mike Marshall5db11c22015-07-17 10:38:12 -0400805 struct poll_table_struct *poll_table)
806{
807 int poll_revent_mask = 0;
808
Al Viro83595db2016-01-19 12:03:05 -0500809 poll_wait(file, &orangefs_request_list_waitq, poll_table);
Mike Marshall5db11c22015-07-17 10:38:12 -0400810
Al Viro83595db2016-01-19 12:03:05 -0500811 if (!list_empty(&orangefs_request_list))
812 poll_revent_mask |= POLL_IN;
Mike Marshall5db11c22015-07-17 10:38:12 -0400813 return poll_revent_mask;
814}
815
Yi Liu8bb8aef2015-11-24 15:12:14 -0500816const struct file_operations orangefs_devreq_file_operations = {
Mike Marshall5db11c22015-07-17 10:38:12 -0400817 .owner = THIS_MODULE,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500818 .read = orangefs_devreq_read,
819 .write_iter = orangefs_devreq_write_iter,
820 .open = orangefs_devreq_open,
821 .release = orangefs_devreq_release,
822 .unlocked_ioctl = orangefs_devreq_ioctl,
Mike Marshall5db11c22015-07-17 10:38:12 -0400823
824#ifdef CONFIG_COMPAT /* CONFIG_COMPAT is in .config */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500825 .compat_ioctl = orangefs_devreq_compat_ioctl,
Mike Marshall5db11c22015-07-17 10:38:12 -0400826#endif
Yi Liu8bb8aef2015-11-24 15:12:14 -0500827 .poll = orangefs_devreq_poll
Mike Marshall5db11c22015-07-17 10:38:12 -0400828};