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