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 | |
Mike Marshall | ca9f518 | 2016-02-26 10:21:12 -0500 | [diff] [blame] | 49 | /* |
| 50 | * find the op with this tag and remove it from the in progress |
| 51 | * hash table. |
| 52 | */ |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 53 | static struct orangefs_kernel_op_s *orangefs_devreq_remove_op(__u64 tag) |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 54 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 55 | struct orangefs_kernel_op_s *op, *next; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 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) { |
Al Viro | 05a50a5 | 2016-02-18 18:59:44 -0500 | [diff] [blame] | 65 | if (op->tag == tag && !op_state_purged(op) && |
| 66 | !op_state_given_up(op)) { |
Al Viro | ed42fe0 | 2016-01-22 19:47:47 -0500 | [diff] [blame] | 67 | list_del_init(&op->list); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 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 | |
Martin Brandenburg | acfcbaf | 2016-03-05 13:17:39 -0500 | [diff] [blame] | 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 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 115 | static int orangefs_devreq_open(struct inode *inode, struct file *file) |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 116 | { |
| 117 | int ret = -EINVAL; |
| 118 | |
Jann Horn | 78fee0b | 2016-06-25 01:51:52 +0200 | [diff] [blame] | 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 | |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 126 | if (!(file->f_flags & O_NONBLOCK)) { |
Mike Marshall | 97f1002 | 2015-12-11 16:45:03 -0500 | [diff] [blame] | 127 | gossip_err("%s: device cannot be opened in blocking mode\n", |
| 128 | __func__); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 129 | goto out; |
| 130 | } |
| 131 | ret = -EACCES; |
Mike Marshall | 97f1002 | 2015-12-11 16:45:03 -0500 | [diff] [blame] | 132 | gossip_debug(GOSSIP_DEV_DEBUG, "client-core: opening device\n"); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 133 | mutex_lock(&devreq_mutex); |
| 134 | |
| 135 | if (open_access_count == 0) { |
Al Viro | fee25ce | 2016-01-22 19:46:08 -0500 | [diff] [blame] | 136 | open_access_count = 1; |
Al Viro | fb6d252 | 2016-01-19 12:00:26 -0500 | [diff] [blame] | 137 | ret = 0; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 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 | |
Mike Marshall | 97f1002 | 2015-12-11 16:45:03 -0500 | [diff] [blame] | 151 | /* Function for read() callers into the device */ |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 152 | static ssize_t orangefs_devreq_read(struct file *file, |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 153 | char __user *buf, |
| 154 | size_t count, loff_t *offset) |
| 155 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 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; |
Martin Brandenburg | 24c8d08 | 2015-11-13 14:26:10 -0500 | [diff] [blame] | 160 | unsigned long ret; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 161 | |
Martin Brandenburg | 24c8d08 | 2015-11-13 14:26:10 -0500 | [diff] [blame] | 162 | /* We do not support blocking IO. */ |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 163 | if (!(file->f_flags & O_NONBLOCK)) { |
Mike Marshall | 97f1002 | 2015-12-11 16:45:03 -0500 | [diff] [blame] | 164 | gossip_err("%s: blocking read from client-core.\n", |
| 165 | __func__); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 166 | return -EINVAL; |
Martin Brandenburg | 24c8d08 | 2015-11-13 14:26:10 -0500 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /* |
Martin Brandenburg | a762ae6 | 2015-12-15 14:22:06 -0500 | [diff] [blame] | 170 | * 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] | 171 | * always read with that size buffer. |
| 172 | */ |
Martin Brandenburg | a762ae6 | 2015-12-15 14:22:06 -0500 | [diff] [blame] | 173 | if (count != MAX_DEV_REQ_UPSIZE) { |
Martin Brandenburg | 24c8d08 | 2015-11-13 14:26:10 -0500 | [diff] [blame] | 174 | gossip_err("orangefs: client-core tried to read wrong size\n"); |
| 175 | return -EINVAL; |
| 176 | } |
| 177 | |
Al Viro | ed42fe0 | 2016-01-22 19:47:47 -0500 | [diff] [blame] | 178 | restart: |
Martin Brandenburg | 24c8d08 | 2015-11-13 14:26:10 -0500 | [diff] [blame] | 179 | /* Get next op (if any) from top of list. */ |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 180 | spin_lock(&orangefs_request_list_lock); |
| 181 | list_for_each_entry_safe(op, temp, &orangefs_request_list, list) { |
Martin Brandenburg | 24c8d08 | 2015-11-13 14:26:10 -0500 | [diff] [blame] | 182 | __s32 fsid; |
| 183 | /* This lock is held past the end of the loop when we break. */ |
| 184 | spin_lock(&op->lock); |
Al Viro | 05a50a5 | 2016-02-18 18:59:44 -0500 | [diff] [blame] | 185 | if (unlikely(op_state_purged(op) || op_state_given_up(op))) { |
Al Viro | ed42fe0 | 2016-01-22 19:47:47 -0500 | [diff] [blame] | 186 | spin_unlock(&op->lock); |
| 187 | continue; |
| 188 | } |
Martin Brandenburg | 24c8d08 | 2015-11-13 14:26:10 -0500 | [diff] [blame] | 189 | |
| 190 | fsid = fsid_of_op(op); |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 191 | if (fsid != ORANGEFS_FS_ID_NULL) { |
Martin Brandenburg | 24c8d08 | 2015-11-13 14:26:10 -0500 | [diff] [blame] | 192 | int ret; |
| 193 | /* Skip ops whose filesystem needs to be mounted. */ |
| 194 | ret = fs_mount_pending(fsid); |
| 195 | if (ret == 1) { |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 196 | gossip_debug(GOSSIP_DEV_DEBUG, |
Mike Marshall | 5090c96 | 2016-02-04 13:29:27 -0500 | [diff] [blame] | 197 | "%s: mount pending, skipping op tag " |
| 198 | "%llu %s\n", |
| 199 | __func__, |
| 200 | llu(op->tag), |
| 201 | get_opname_string(op)); |
Martin Brandenburg | 24c8d08 | 2015-11-13 14:26:10 -0500 | [diff] [blame] | 202 | spin_unlock(&op->lock); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 203 | continue; |
Mike Marshall | 97f1002 | 2015-12-11 16:45:03 -0500 | [diff] [blame] | 204 | /* |
| 205 | * Skip ops whose filesystem we don't know about unless |
| 206 | * it is being mounted. |
| 207 | */ |
Martin Brandenburg | 24c8d08 | 2015-11-13 14:26:10 -0500 | [diff] [blame] | 208 | /* XXX: is there a better way to detect this? */ |
| 209 | } else if (ret == -1 && |
Mike Marshall | 97f1002 | 2015-12-11 16:45:03 -0500 | [diff] [blame] | 210 | !(op->upcall.type == |
| 211 | ORANGEFS_VFS_OP_FS_MOUNT || |
| 212 | op->upcall.type == |
| 213 | ORANGEFS_VFS_OP_GETATTR)) { |
Martin Brandenburg | 24c8d08 | 2015-11-13 14:26:10 -0500 | [diff] [blame] | 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); |
| 221 | continue; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 222 | } |
| 223 | } |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 224 | /* |
Martin Brandenburg | 24c8d08 | 2015-11-13 14:26:10 -0500 | [diff] [blame] | 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. |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 228 | */ |
Martin Brandenburg | 24c8d08 | 2015-11-13 14:26:10 -0500 | [diff] [blame] | 229 | cur_op = op; |
| 230 | break; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 231 | } |
Martin Brandenburg | 24c8d08 | 2015-11-13 14:26:10 -0500 | [diff] [blame] | 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) { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 238 | spin_unlock(&orangefs_request_list_lock); |
Martin Brandenburg | 24c8d08 | 2015-11-13 14:26:10 -0500 | [diff] [blame] | 239 | return -EAGAIN; |
| 240 | } |
| 241 | |
Mike Marshall | ca9f518 | 2016-02-26 10:21:12 -0500 | [diff] [blame] | 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)); |
Martin Brandenburg | 24c8d08 | 2015-11-13 14:26:10 -0500 | [diff] [blame] | 246 | |
| 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"); |
Al Viro | 05a50a5 | 2016-02-18 18:59:44 -0500 | [diff] [blame] | 253 | list_del_init(&cur_op->list); |
Martin Brandenburg | 24c8d08 | 2015-11-13 14:26:10 -0500 | [diff] [blame] | 254 | spin_unlock(&cur_op->lock); |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 255 | spin_unlock(&orangefs_request_list_lock); |
Martin Brandenburg | 24c8d08 | 2015-11-13 14:26:10 -0500 | [diff] [blame] | 256 | return -EAGAIN; |
| 257 | } |
Mike Marshall | ca9f518 | 2016-02-26 10:21:12 -0500 | [diff] [blame] | 258 | |
Al Viro | ed42fe0 | 2016-01-22 19:47:47 -0500 | [diff] [blame] | 259 | list_del_init(&cur_op->list); |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 260 | spin_unlock(&orangefs_request_list_lock); |
Al Viro | ed42fe0 | 2016-01-22 19:47:47 -0500 | [diff] [blame] | 261 | |
Martin Brandenburg | 24c8d08 | 2015-11-13 14:26:10 -0500 | [diff] [blame] | 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, |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 275 | sizeof(struct orangefs_upcall_s)); |
Martin Brandenburg | 24c8d08 | 2015-11-13 14:26:10 -0500 | [diff] [blame] | 276 | if (ret != 0) |
| 277 | goto error; |
| 278 | |
Al Viro | ed42fe0 | 2016-01-22 19:47:47 -0500 | [diff] [blame] | 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); |
Al Viro | 05a50a5 | 2016-02-18 18:59:44 -0500 | [diff] [blame] | 284 | complete(&cur_op->waitq); |
Al Viro | ed42fe0 | 2016-01-22 19:47:47 -0500 | [diff] [blame] | 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); |
Mike Marshall | 9d9e7ba | 2016-03-03 13:46:48 -0500 | [diff] [blame] | 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); |
Al Viro | ed42fe0 | 2016-01-22 19:47:47 -0500 | [diff] [blame] | 299 | orangefs_devreq_add_op(cur_op); |
| 300 | spin_unlock(&cur_op->lock); |
| 301 | spin_unlock(&htable_ops_in_progress_lock); |
Al Viro | ed42fe0 | 2016-01-22 19:47:47 -0500 | [diff] [blame] | 302 | |
Martin Brandenburg | 24c8d08 | 2015-11-13 14:26:10 -0500 | [diff] [blame] | 303 | /* The client only asks to read one size buffer. */ |
Martin Brandenburg | a762ae6 | 2015-12-15 14:22:06 -0500 | [diff] [blame] | 304 | return MAX_DEV_REQ_UPSIZE; |
Martin Brandenburg | 24c8d08 | 2015-11-13 14:26:10 -0500 | [diff] [blame] | 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"); |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 312 | spin_lock(&orangefs_request_list_lock); |
Martin Brandenburg | 24c8d08 | 2015-11-13 14:26:10 -0500 | [diff] [blame] | 313 | spin_lock(&cur_op->lock); |
Al Viro | ed42fe0 | 2016-01-22 19:47:47 -0500 | [diff] [blame] | 314 | if (likely(!op_state_given_up(cur_op))) { |
| 315 | set_op_state_waiting(cur_op); |
Mike Marshall | 9d9e7ba | 2016-03-03 13:46:48 -0500 | [diff] [blame] | 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); |
Al Viro | ed42fe0 | 2016-01-22 19:47:47 -0500 | [diff] [blame] | 322 | list_add(&cur_op->list, &orangefs_request_list); |
Al Viro | 05a50a5 | 2016-02-18 18:59:44 -0500 | [diff] [blame] | 323 | spin_unlock(&cur_op->lock); |
| 324 | } else { |
| 325 | spin_unlock(&cur_op->lock); |
| 326 | complete(&cur_op->waitq); |
Al Viro | ed42fe0 | 2016-01-22 19:47:47 -0500 | [diff] [blame] | 327 | } |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 328 | spin_unlock(&orangefs_request_list_lock); |
Martin Brandenburg | 24c8d08 | 2015-11-13 14:26:10 -0500 | [diff] [blame] | 329 | return -EFAULT; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 330 | } |
| 331 | |
Mike Marshall | 97f1002 | 2015-12-11 16:45:03 -0500 | [diff] [blame] | 332 | /* |
Mike Marshall | b3ae475 | 2016-01-13 11:18:12 -0500 | [diff] [blame] | 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) |
Mike Marshall | 97f1002 | 2015-12-11 16:45:03 -0500 | [diff] [blame] | 341 | */ |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 342 | static ssize_t orangefs_devreq_write_iter(struct kiocb *iocb, |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 343 | struct iov_iter *iter) |
| 344 | { |
Mike Marshall | b3ae475 | 2016-01-13 11:18:12 -0500 | [diff] [blame] | 345 | ssize_t ret; |
| 346 | struct orangefs_kernel_op_s *op = NULL; |
| 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); |
| 361 | |
| 362 | if (total < MAX_DEV_REQ_DOWNSIZE) { |
Mike Marshall | cf0c277 | 2016-01-19 12:04:40 -0500 | [diff] [blame] | 363 | gossip_err("%s: total:%d: must be at least:%u:\n", |
Mike Marshall | b3ae475 | 2016-01-13 11:18:12 -0500 | [diff] [blame] | 364 | __func__, |
| 365 | total, |
Mike Marshall | cf0c277 | 2016-01-19 12:04:40 -0500 | [diff] [blame] | 366 | (unsigned int) MAX_DEV_REQ_DOWNSIZE); |
Al Viro | ed42fe0 | 2016-01-22 19:47:47 -0500 | [diff] [blame] | 367 | return -EFAULT; |
Mike Marshall | b3ae475 | 2016-01-13 11:18:12 -0500 | [diff] [blame] | 368 | } |
| 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__); |
Al Viro | ed42fe0 | 2016-01-22 19:47:47 -0500 | [diff] [blame] | 373 | return -EFAULT; |
Mike Marshall | b3ae475 | 2016-01-13 11:18:12 -0500 | [diff] [blame] | 374 | } |
| 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); |
Al Viro | ed42fe0 | 2016-01-22 19:47:47 -0500 | [diff] [blame] | 382 | return -EPROTO; |
Mike Marshall | b3ae475 | 2016-01-13 11:18:12 -0500 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | if (head.magic != ORANGEFS_DEVREQ_MAGIC) { |
| 386 | gossip_err("Error: Device magic number does not match.\n"); |
Al Viro | ed42fe0 | 2016-01-22 19:47:47 -0500 | [diff] [blame] | 387 | return -EPROTO; |
Mike Marshall | b3ae475 | 2016-01-13 11:18:12 -0500 | [diff] [blame] | 388 | } |
| 389 | |
Mike Marshall | ca9f518 | 2016-02-26 10:21:12 -0500 | [diff] [blame] | 390 | /* remove the op from the in progress hash table */ |
Mike Marshall | b3ae475 | 2016-01-13 11:18:12 -0500 | [diff] [blame] | 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)); |
Al Viro | ed42fe0 | 2016-01-22 19:47:47 -0500 | [diff] [blame] | 395 | return ret; |
Mike Marshall | b3ae475 | 2016-01-13 11:18:12 -0500 | [diff] [blame] | 396 | } |
| 397 | |
Mike Marshall | b3ae475 | 2016-01-13 11:18:12 -0500 | [diff] [blame] | 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__); |
Al Viro | 5964c1b | 2016-02-18 18:53:41 -0500 | [diff] [blame] | 401 | goto Efault; |
Mike Marshall | b3ae475 | 2016-01-13 11:18:12 -0500 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | if (op->downcall.status) |
| 405 | goto wakeup; |
| 406 | |
| 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); |
Al Viro | 5964c1b | 2016-02-18 18:53:41 -0500 | [diff] [blame] | 421 | goto Efault; |
Mike Marshall | b3ae475 | 2016-01-13 11:18:12 -0500 | [diff] [blame] | 422 | } |
| 423 | |
| 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); |
Al Viro | 5964c1b | 2016-02-18 18:53:41 -0500 | [diff] [blame] | 430 | goto Efault; |
Mike Marshall | b3ae475 | 2016-01-13 11:18:12 -0500 | [diff] [blame] | 431 | } |
| 432 | |
| 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); |
Al Viro | 5964c1b | 2016-02-18 18:53:41 -0500 | [diff] [blame] | 439 | goto Efault; |
Mike Marshall | b3ae475 | 2016-01-13 11:18:12 -0500 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | if (op->downcall.type != ORANGEFS_VFS_OP_READDIR) |
| 443 | goto wakeup; |
| 444 | |
| 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__); |
Al Viro | 5964c1b | 2016-02-18 18:53:41 -0500 | [diff] [blame] | 450 | goto Enomem; |
Mike Marshall | b3ae475 | 2016-01-13 11:18:12 -0500 | [diff] [blame] | 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); |
Al Viro | 5964c1b | 2016-02-18 18:53:41 -0500 | [diff] [blame] | 459 | goto Efault; |
Mike Marshall | b3ae475 | 2016-01-13 11:18:12 -0500 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | wakeup: |
Al Viro | 2a9e5c2 | 2016-01-23 13:45:46 -0500 | [diff] [blame] | 463 | /* |
Mike Marshall | 9f08cfe | 2016-02-26 14:39:08 -0500 | [diff] [blame] | 464 | * Return to vfs waitqueue, and back to service_operation |
| 465 | * through wait_for_matching_downcall. |
Al Viro | 2a9e5c2 | 2016-01-23 13:45:46 -0500 | [diff] [blame] | 466 | */ |
| 467 | spin_lock(&op->lock); |
Al Viro | 5964c1b | 2016-02-18 18:53:41 -0500 | [diff] [blame] | 468 | if (unlikely(op_is_cancel(op))) { |
Al Viro | 2a9e5c2 | 2016-01-23 13:45:46 -0500 | [diff] [blame] | 469 | spin_unlock(&op->lock); |
Al Viro | 78699e2 | 2016-02-11 23:07:19 -0500 | [diff] [blame] | 470 | put_cancel(op); |
Al Viro | 5964c1b | 2016-02-18 18:53:41 -0500 | [diff] [blame] | 471 | } else if (unlikely(op_state_given_up(op))) { |
| 472 | spin_unlock(&op->lock); |
Al Viro | 05a50a5 | 2016-02-18 18:59:44 -0500 | [diff] [blame] | 473 | complete(&op->waitq); |
Al Viro | 5964c1b | 2016-02-18 18:53:41 -0500 | [diff] [blame] | 474 | } else { |
| 475 | set_op_state_serviced(op); |
Mike Marshall | 9d9e7ba | 2016-03-03 13:46:48 -0500 | [diff] [blame] | 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); |
Al Viro | 5964c1b | 2016-02-18 18:53:41 -0500 | [diff] [blame] | 482 | spin_unlock(&op->lock); |
| 483 | } |
Mike Marshall | b3ae475 | 2016-01-13 11:18:12 -0500 | [diff] [blame] | 484 | return ret; |
Al Viro | ed42fe0 | 2016-01-22 19:47:47 -0500 | [diff] [blame] | 485 | |
Al Viro | 5964c1b | 2016-02-18 18:53:41 -0500 | [diff] [blame] | 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; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 495 | } |
| 496 | |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 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 |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 502 | * before we call orangefs_bufmap_finalize, and similar such tricky |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 503 | * situations |
| 504 | */ |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 505 | static int orangefs_devreq_release(struct inode *inode, struct file *file) |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 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); |
Al Viro | ea2c9c9 | 2016-02-13 21:01:21 -0500 | [diff] [blame] | 514 | orangefs_bufmap_finalize(); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 515 | |
Al Viro | fee25ce | 2016-01-22 19:46:08 -0500 | [diff] [blame] | 516 | open_access_count = -1; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 517 | |
| 518 | unmounted = mark_all_pending_mounts(); |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 519 | gossip_debug(GOSSIP_DEV_DEBUG, "ORANGEFS Device Close: Filesystem(s) %s\n", |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 520 | (unmounted ? "UNMOUNTED" : "MOUNTED")); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 521 | |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 522 | purge_waiting_ops(); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 523 | purge_inprogress_ops(); |
Al Viro | ea2c9c9 | 2016-02-13 21:01:21 -0500 | [diff] [blame] | 524 | |
| 525 | orangefs_bufmap_run_down(); |
| 526 | |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 527 | gossip_debug(GOSSIP_DEV_DEBUG, |
| 528 | "pvfs2-client-core: device close complete\n"); |
Al Viro | fee25ce | 2016-01-22 19:46:08 -0500 | [diff] [blame] | 529 | open_access_count = 0; |
| 530 | mutex_unlock(&devreq_mutex); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 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 | |
Al Viro | 78699e2 | 2016-02-11 23:07:19 -0500 | [diff] [blame] | 548 | bool __is_daemon_in_service(void) |
| 549 | { |
| 550 | return open_access_count == 1; |
| 551 | } |
| 552 | |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 553 | static inline long check_ioctl_command(unsigned int command) |
| 554 | { |
| 555 | /* Check for valid ioctl codes */ |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 556 | if (_IOC_TYPE(command) != ORANGEFS_DEV_MAGIC) { |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 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), |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 560 | ORANGEFS_DEV_MAGIC); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 561 | return -EINVAL; |
| 562 | } |
| 563 | /* and valid ioctl commands */ |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 564 | if (_IOC_NR(command) >= ORANGEFS_DEV_MAXNR || _IOC_NR(command) <= 0) { |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 565 | gossip_err("Invalid ioctl command number [%d >= %d]\n", |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 566 | _IOC_NR(command), ORANGEFS_DEV_MAXNR); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 567 | return -ENOIOCTLCMD; |
| 568 | } |
| 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | static long dispatch_ioctl_command(unsigned int command, unsigned long arg) |
| 573 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 574 | static __s32 magic = ORANGEFS_DEVREQ_MAGIC; |
Martin Brandenburg | a762ae6 | 2015-12-15 14:22:06 -0500 | [diff] [blame] | 575 | static __s32 max_up_size = MAX_DEV_REQ_UPSIZE; |
| 576 | static __s32 max_down_size = MAX_DEV_REQ_DOWNSIZE; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 577 | struct ORANGEFS_dev_map_desc user_desc; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 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; |
Al Viro | 4599649 | 2016-03-25 19:56:34 -0400 | [diff] [blame] | 582 | struct orangefs_sb_info_s *orangefs_sb; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 583 | |
| 584 | /* mtmoore: add locking here */ |
| 585 | |
| 586 | switch (command) { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 587 | case ORANGEFS_DEV_GET_MAGIC: |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 588 | return ((put_user(magic, (__s32 __user *) arg) == -EFAULT) ? |
| 589 | -EIO : |
| 590 | 0); |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 591 | case ORANGEFS_DEV_GET_MAX_UPSIZE: |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 592 | return ((put_user(max_up_size, |
| 593 | (__s32 __user *) arg) == -EFAULT) ? |
| 594 | -EIO : |
| 595 | 0); |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 596 | case ORANGEFS_DEV_GET_MAX_DOWNSIZE: |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 597 | return ((put_user(max_down_size, |
| 598 | (__s32 __user *) arg) == -EFAULT) ? |
| 599 | -EIO : |
| 600 | 0); |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 601 | case ORANGEFS_DEV_MAP: |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 602 | ret = copy_from_user(&user_desc, |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 603 | (struct ORANGEFS_dev_map_desc __user *) |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 604 | arg, |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 605 | sizeof(struct ORANGEFS_dev_map_desc)); |
Al Viro | ea2c9c9 | 2016-02-13 21:01:21 -0500 | [diff] [blame] | 606 | /* WTF -EIO and not -EFAULT? */ |
| 607 | return ret ? -EIO : orangefs_bufmap_initialize(&user_desc); |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 608 | case ORANGEFS_DEV_REMOUNT_ALL: |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 609 | gossip_debug(GOSSIP_DEV_DEBUG, |
Mike Marshall | 97f1002 | 2015-12-11 16:45:03 -0500 | [diff] [blame] | 610 | "%s: got ORANGEFS_DEV_REMOUNT_ALL\n", |
| 611 | __func__); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 612 | |
| 613 | /* |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 614 | * remount all mounted orangefs volumes to regain the lost |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 615 | * dynamic mount tables (if any) -- NOTE: this is done |
| 616 | * without keeping the superblock list locked due to the |
Mike Marshall | adcf34a | 2016-02-24 16:54:27 -0500 | [diff] [blame] | 617 | * upcall/downcall waiting. also, the request mutex is |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 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, |
Mike Marshall | 97f1002 | 2015-12-11 16:45:03 -0500 | [diff] [blame] | 626 | "%s: priority remount in progress\n", |
| 627 | __func__); |
Al Viro | 4599649 | 2016-03-25 19:56:34 -0400 | [diff] [blame] | 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); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 642 | |
Al Viro | 4599649 | 2016-03-25 19:56:34 -0400 | [diff] [blame] | 643 | spin_unlock(&orangefs_superblocks_lock); |
| 644 | ret = orangefs_remount(orangefs_sb); |
| 645 | spin_lock(&orangefs_superblocks_lock); |
| 646 | if (ret) { |
| 647 | gossip_debug(GOSSIP_DEV_DEBUG, |
| 648 | "SB %p remount failed\n", |
| 649 | orangefs_sb); |
| 650 | break; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 651 | } |
| 652 | } |
Al Viro | 4599649 | 2016-03-25 19:56:34 -0400 | [diff] [blame] | 653 | spin_unlock(&orangefs_superblocks_lock); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 654 | gossip_debug(GOSSIP_DEV_DEBUG, |
Mike Marshall | 97f1002 | 2015-12-11 16:45:03 -0500 | [diff] [blame] | 655 | "%s: priority remount complete\n", |
| 656 | __func__); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 657 | mutex_unlock(&request_mutex); |
| 658 | return ret; |
| 659 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 660 | case ORANGEFS_DEV_UPSTREAM: |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 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 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 670 | case ORANGEFS_DEV_CLIENT_MASK: |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 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 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 689 | case ORANGEFS_DEV_CLIENT_STRING: |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 690 | ret = copy_from_user(&client_debug_array_string, |
| 691 | (void __user *)arg, |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 692 | ORANGEFS_MAX_DEBUG_STRING_LEN); |
Mike Marshall | 53f57fe | 2016-03-14 15:28:34 -0400 | [diff] [blame] | 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 | |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 706 | if (ret != 0) { |
Mike Marshall | 97f1002 | 2015-12-11 16:45:03 -0500 | [diff] [blame] | 707 | pr_info("%s: CLIENT_STRING: copy_from_user failed\n", |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 708 | __func__); |
| 709 | return -EIO; |
| 710 | } |
| 711 | |
Mike Marshall | 97f1002 | 2015-12-11 16:45:03 -0500 | [diff] [blame] | 712 | pr_info("%s: client debug array string has been received.\n", |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 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)) { |
Mike Marshall | 97f1002 | 2015-12-11 16:45:03 -0500 | [diff] [blame] | 722 | gossip_err("%s: no debug help string \n", |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 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 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 751 | orangefs_client_debug_init(); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 752 | |
| 753 | help_string_initialized++; |
| 754 | |
| 755 | return ret; |
| 756 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 757 | case ORANGEFS_DEV_DEBUG: |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 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 |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 772 | * a value for ORANGEFS_KMODMASK. |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 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; |
Mike Marshall | 97f1002 | 2015-12-11 16:45:03 -0500 | [diff] [blame] | 779 | pr_info("%s: kernel debug mask has been modified to " |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 780 | ":%s: :%llx:\n", |
Mike Marshall | 97f1002 | 2015-12-11 16:45:03 -0500 | [diff] [blame] | 781 | __func__, |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 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); |
Mike Marshall | 97f1002 | 2015-12-11 16:45:03 -0500 | [diff] [blame] | 787 | pr_info("%s: client debug mask has been modified to" |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 788 | ":%s: :%llx:\n", |
Mike Marshall | 97f1002 | 2015-12-11 16:45:03 -0500 | [diff] [blame] | 789 | __func__, |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 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 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 805 | static long orangefs_devreq_ioctl(struct file *file, |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 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 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 820 | /* Compat structure for the ORANGEFS_DEV_MAP ioctl */ |
| 821 | struct ORANGEFS_dev_map_desc32 { |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 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 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 830 | struct ORANGEFS_dev_map_desc32 __user *p32 = (void __user *)args; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 831 | /* |
| 832 | * Depending on the architecture, allocate some space on the |
| 833 | * user-call-stack based on our expected layout. |
| 834 | */ |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 835 | struct ORANGEFS_dev_map_desc __user *p = |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 836 | compat_alloc_user_space(sizeof(*p)); |
Mike Marshall | 84d0215 | 2015-07-28 13:27:51 -0400 | [diff] [blame] | 837 | compat_uptr_t addr; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 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 | */ |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 863 | static long orangefs_devreq_compat_ioctl(struct file *filp, unsigned int cmd, |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 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; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 873 | if (cmd == ORANGEFS_DEV_MAP) { |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 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 | |
Mike Marshall | 2c590d5 | 2015-07-24 10:37:15 -0400 | [diff] [blame] | 888 | #endif /* CONFIG_COMPAT is in .config */ |
| 889 | |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 890 | /* the assigned character device major number */ |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 891 | static int orangefs_dev_major; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 892 | |
| 893 | /* |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 894 | * Initialize orangefs device specific state: |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 895 | * Must be called at module load time only |
| 896 | */ |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 897 | int orangefs_dev_init(void) |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 898 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 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) { |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 904 | gossip_debug(GOSSIP_DEV_DEBUG, |
| 905 | "Failed to register /dev/%s (error %d)\n", |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 906 | ORANGEFS_REQDEVICE_NAME, orangefs_dev_major); |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 907 | return orangefs_dev_major; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | gossip_debug(GOSSIP_DEV_DEBUG, |
| 911 | "*** /dev/%s character device registered ***\n", |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 912 | ORANGEFS_REQDEVICE_NAME); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 913 | gossip_debug(GOSSIP_DEV_DEBUG, "'mknod /dev/%s c %d 0'.\n", |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 914 | ORANGEFS_REQDEVICE_NAME, orangefs_dev_major); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 915 | return 0; |
| 916 | } |
| 917 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 918 | void orangefs_dev_cleanup(void) |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 919 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 920 | unregister_chrdev(orangefs_dev_major, ORANGEFS_REQDEVICE_NAME); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 921 | gossip_debug(GOSSIP_DEV_DEBUG, |
| 922 | "*** /dev/%s character device unregistered ***\n", |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 923 | ORANGEFS_REQDEVICE_NAME); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 924 | } |
| 925 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 926 | static unsigned int orangefs_devreq_poll(struct file *file, |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 927 | struct poll_table_struct *poll_table) |
| 928 | { |
| 929 | int poll_revent_mask = 0; |
| 930 | |
Al Viro | 83595db | 2016-01-19 12:03:05 -0500 | [diff] [blame] | 931 | poll_wait(file, &orangefs_request_list_waitq, poll_table); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 932 | |
Al Viro | 83595db | 2016-01-19 12:03:05 -0500 | [diff] [blame] | 933 | if (!list_empty(&orangefs_request_list)) |
| 934 | poll_revent_mask |= POLL_IN; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 935 | return poll_revent_mask; |
| 936 | } |
| 937 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 938 | const struct file_operations orangefs_devreq_file_operations = { |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 939 | .owner = THIS_MODULE, |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 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, |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 945 | |
| 946 | #ifdef CONFIG_COMPAT /* CONFIG_COMPAT is in .config */ |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 947 | .compat_ioctl = orangefs_devreq_compat_ioctl, |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 948 | #endif |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 949 | .poll = orangefs_devreq_poll |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 950 | }; |