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