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