Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2 | #include <linux/ceph/ceph_debug.h> |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3 | |
Sage Weil | 496e595 | 2010-09-22 19:57:10 -0700 | [diff] [blame] | 4 | #include <linux/fs.h> |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 5 | #include <linux/wait.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 6 | #include <linux/slab.h> |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 7 | #include <linux/gfp.h> |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 8 | #include <linux/sched.h> |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 9 | #include <linux/debugfs.h> |
| 10 | #include <linux/seq_file.h> |
Yan, Zheng | 3e0708b | 2015-05-22 16:38:02 +0800 | [diff] [blame] | 11 | #include <linux/ratelimit.h> |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 12 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 13 | #include "super.h" |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 14 | #include "mds_client.h" |
| 15 | |
Sage Weil | 1fe60e5 | 2012-07-30 16:23:22 -0700 | [diff] [blame] | 16 | #include <linux/ceph/ceph_features.h> |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 17 | #include <linux/ceph/messenger.h> |
| 18 | #include <linux/ceph/decode.h> |
| 19 | #include <linux/ceph/pagelist.h> |
| 20 | #include <linux/ceph/auth.h> |
| 21 | #include <linux/ceph/debugfs.h> |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 22 | |
| 23 | /* |
| 24 | * A cluster of MDS (metadata server) daemons is responsible for |
| 25 | * managing the file system namespace (the directory hierarchy and |
| 26 | * inodes) and for coordinating shared access to storage. Metadata is |
| 27 | * partitioning hierarchically across a number of servers, and that |
| 28 | * partition varies over time as the cluster adjusts the distribution |
| 29 | * in order to balance load. |
| 30 | * |
| 31 | * The MDS client is primarily responsible to managing synchronous |
| 32 | * metadata requests for operations like open, unlink, and so forth. |
| 33 | * If there is a MDS failure, we find out about it when we (possibly |
| 34 | * request and) receive a new MDS map, and can resubmit affected |
| 35 | * requests. |
| 36 | * |
| 37 | * For the most part, though, we take advantage of a lossless |
| 38 | * communications channel to the MDS, and do not need to worry about |
| 39 | * timing out or resubmitting requests. |
| 40 | * |
| 41 | * We maintain a stateful "session" with each MDS we interact with. |
| 42 | * Within each session, we sent periodic heartbeat messages to ensure |
| 43 | * any capabilities or leases we have been issues remain valid. If |
| 44 | * the session times out and goes stale, our leases and capabilities |
| 45 | * are no longer valid. |
| 46 | */ |
| 47 | |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 48 | struct ceph_reconnect_state { |
Yan, Zheng | 44c9975 | 2013-09-22 10:28:10 +0800 | [diff] [blame] | 49 | int nr_caps; |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 50 | struct ceph_pagelist *pagelist; |
Yan, Zheng | 121f22a | 2016-07-04 22:05:18 +0800 | [diff] [blame] | 51 | unsigned msg_version; |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 54 | static void __wake_requests(struct ceph_mds_client *mdsc, |
| 55 | struct list_head *head); |
| 56 | |
Tobias Klauser | 9e32789 | 2010-05-20 10:40:19 +0200 | [diff] [blame] | 57 | static const struct ceph_connection_operations mds_con_ops; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 58 | |
| 59 | |
| 60 | /* |
| 61 | * mds reply parsing |
| 62 | */ |
| 63 | |
| 64 | /* |
| 65 | * parse individual inode info |
| 66 | */ |
| 67 | static int parse_reply_info_in(void **p, void *end, |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 68 | struct ceph_mds_reply_info_in *info, |
Ilya Dryomov | 12b4629 | 2013-12-24 21:19:23 +0200 | [diff] [blame] | 69 | u64 features) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 70 | { |
| 71 | int err = -EIO; |
| 72 | |
| 73 | info->in = *p; |
| 74 | *p += sizeof(struct ceph_mds_reply_inode) + |
| 75 | sizeof(*info->in->fragtree.splits) * |
| 76 | le32_to_cpu(info->in->fragtree.nsplits); |
| 77 | |
| 78 | ceph_decode_32_safe(p, end, info->symlink_len, bad); |
| 79 | ceph_decode_need(p, end, info->symlink_len, bad); |
| 80 | info->symlink = *p; |
| 81 | *p += info->symlink_len; |
| 82 | |
Ilya Dryomov | 23c625c | 2018-11-08 14:55:21 +0100 | [diff] [blame] | 83 | ceph_decode_copy_safe(p, end, &info->dir_layout, |
| 84 | sizeof(info->dir_layout), bad); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 85 | ceph_decode_32_safe(p, end, info->xattr_len, bad); |
| 86 | ceph_decode_need(p, end, info->xattr_len, bad); |
| 87 | info->xattr_data = *p; |
| 88 | *p += info->xattr_len; |
Yan, Zheng | fb01d1f | 2014-11-14 21:29:55 +0800 | [diff] [blame] | 89 | |
| 90 | if (features & CEPH_FEATURE_MDS_INLINE_DATA) { |
| 91 | ceph_decode_64_safe(p, end, info->inline_version, bad); |
| 92 | ceph_decode_32_safe(p, end, info->inline_len, bad); |
| 93 | ceph_decode_need(p, end, info->inline_len, bad); |
| 94 | info->inline_data = *p; |
| 95 | *p += info->inline_len; |
| 96 | } else |
| 97 | info->inline_version = CEPH_INLINE_NONE; |
| 98 | |
Luis Henriques | fb18a57 | 2018-01-05 10:47:18 +0000 | [diff] [blame] | 99 | if (features & CEPH_FEATURE_MDS_QUOTA) { |
| 100 | u8 struct_v, struct_compat; |
| 101 | u32 struct_len; |
| 102 | |
| 103 | /* |
| 104 | * both struct_v and struct_compat are expected to be >= 1 |
| 105 | */ |
| 106 | ceph_decode_8_safe(p, end, struct_v, bad); |
| 107 | ceph_decode_8_safe(p, end, struct_compat, bad); |
| 108 | if (!struct_v || !struct_compat) |
| 109 | goto bad; |
| 110 | ceph_decode_32_safe(p, end, struct_len, bad); |
| 111 | ceph_decode_need(p, end, struct_len, bad); |
| 112 | ceph_decode_64_safe(p, end, info->max_bytes, bad); |
| 113 | ceph_decode_64_safe(p, end, info->max_files, bad); |
| 114 | } else { |
| 115 | info->max_bytes = 0; |
| 116 | info->max_files = 0; |
| 117 | } |
| 118 | |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 119 | info->pool_ns_len = 0; |
| 120 | info->pool_ns_data = NULL; |
Yan, Zheng | 5ea5c5e | 2016-02-14 18:06:41 +0800 | [diff] [blame] | 121 | if (features & CEPH_FEATURE_FS_FILE_LAYOUT_V2) { |
| 122 | ceph_decode_32_safe(p, end, info->pool_ns_len, bad); |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 123 | if (info->pool_ns_len > 0) { |
| 124 | ceph_decode_need(p, end, info->pool_ns_len, bad); |
| 125 | info->pool_ns_data = *p; |
| 126 | *p += info->pool_ns_len; |
| 127 | } |
Yan, Zheng | 5ea5c5e | 2016-02-14 18:06:41 +0800 | [diff] [blame] | 128 | } |
| 129 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 130 | return 0; |
| 131 | bad: |
| 132 | return err; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * parse a normal reply, which may contain a (dir+)dentry and/or a |
| 137 | * target inode. |
| 138 | */ |
| 139 | static int parse_reply_info_trace(void **p, void *end, |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 140 | struct ceph_mds_reply_info_parsed *info, |
Ilya Dryomov | 12b4629 | 2013-12-24 21:19:23 +0200 | [diff] [blame] | 141 | u64 features) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 142 | { |
| 143 | int err; |
| 144 | |
| 145 | if (info->head->is_dentry) { |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 146 | err = parse_reply_info_in(p, end, &info->diri, features); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 147 | if (err < 0) |
| 148 | goto out_bad; |
| 149 | |
| 150 | if (unlikely(*p + sizeof(*info->dirfrag) > end)) |
| 151 | goto bad; |
| 152 | info->dirfrag = *p; |
| 153 | *p += sizeof(*info->dirfrag) + |
| 154 | sizeof(u32)*le32_to_cpu(info->dirfrag->ndist); |
| 155 | if (unlikely(*p > end)) |
| 156 | goto bad; |
| 157 | |
| 158 | ceph_decode_32_safe(p, end, info->dname_len, bad); |
| 159 | ceph_decode_need(p, end, info->dname_len, bad); |
| 160 | info->dname = *p; |
| 161 | *p += info->dname_len; |
| 162 | info->dlease = *p; |
| 163 | *p += sizeof(*info->dlease); |
| 164 | } |
| 165 | |
| 166 | if (info->head->is_target) { |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 167 | err = parse_reply_info_in(p, end, &info->targeti, features); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 168 | if (err < 0) |
| 169 | goto out_bad; |
| 170 | } |
| 171 | |
| 172 | if (unlikely(*p != end)) |
| 173 | goto bad; |
| 174 | return 0; |
| 175 | |
| 176 | bad: |
| 177 | err = -EIO; |
| 178 | out_bad: |
| 179 | pr_err("problem parsing mds trace %d\n", err); |
| 180 | return err; |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * parse readdir results |
| 185 | */ |
| 186 | static int parse_reply_info_dir(void **p, void *end, |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 187 | struct ceph_mds_reply_info_parsed *info, |
Ilya Dryomov | 12b4629 | 2013-12-24 21:19:23 +0200 | [diff] [blame] | 188 | u64 features) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 189 | { |
| 190 | u32 num, i = 0; |
| 191 | int err; |
| 192 | |
| 193 | info->dir_dir = *p; |
| 194 | if (*p + sizeof(*info->dir_dir) > end) |
| 195 | goto bad; |
| 196 | *p += sizeof(*info->dir_dir) + |
| 197 | sizeof(u32)*le32_to_cpu(info->dir_dir->ndist); |
| 198 | if (*p > end) |
| 199 | goto bad; |
| 200 | |
| 201 | ceph_decode_need(p, end, sizeof(num) + 2, bad); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 202 | num = ceph_decode_32(p); |
Yan, Zheng | 956d39d | 2016-04-27 17:48:30 +0800 | [diff] [blame] | 203 | { |
| 204 | u16 flags = ceph_decode_16(p); |
| 205 | info->dir_end = !!(flags & CEPH_READDIR_FRAG_END); |
| 206 | info->dir_complete = !!(flags & CEPH_READDIR_FRAG_COMPLETE); |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 207 | info->hash_order = !!(flags & CEPH_READDIR_HASH_ORDER); |
Yan, Zheng | 7916254 | 2017-04-05 12:54:05 -0400 | [diff] [blame] | 208 | info->offset_hash = !!(flags & CEPH_READDIR_OFFSET_HASH); |
Yan, Zheng | 956d39d | 2016-04-27 17:48:30 +0800 | [diff] [blame] | 209 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 210 | if (num == 0) |
| 211 | goto done; |
| 212 | |
Yan, Zheng | 2a5beea | 2016-04-28 09:37:39 +0800 | [diff] [blame] | 213 | BUG_ON(!info->dir_entries); |
| 214 | if ((unsigned long)(info->dir_entries + num) > |
| 215 | (unsigned long)info->dir_entries + info->dir_buf_size) { |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 216 | pr_err("dir contents are larger than expected\n"); |
| 217 | WARN_ON(1); |
| 218 | goto bad; |
| 219 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 220 | |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 221 | info->dir_nr = num; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 222 | while (num) { |
Yan, Zheng | 2a5beea | 2016-04-28 09:37:39 +0800 | [diff] [blame] | 223 | struct ceph_mds_reply_dir_entry *rde = info->dir_entries + i; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 224 | /* dentry */ |
| 225 | ceph_decode_need(p, end, sizeof(u32)*2, bad); |
Yan, Zheng | 2a5beea | 2016-04-28 09:37:39 +0800 | [diff] [blame] | 226 | rde->name_len = ceph_decode_32(p); |
| 227 | ceph_decode_need(p, end, rde->name_len, bad); |
| 228 | rde->name = *p; |
| 229 | *p += rde->name_len; |
| 230 | dout("parsed dir dname '%.*s'\n", rde->name_len, rde->name); |
| 231 | rde->lease = *p; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 232 | *p += sizeof(struct ceph_mds_reply_lease); |
| 233 | |
| 234 | /* inode */ |
Yan, Zheng | 2a5beea | 2016-04-28 09:37:39 +0800 | [diff] [blame] | 235 | err = parse_reply_info_in(p, end, &rde->inode, features); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 236 | if (err < 0) |
| 237 | goto out_bad; |
Yan, Zheng | 8974eeb | 2016-04-28 15:17:40 +0800 | [diff] [blame] | 238 | /* ceph_readdir_prepopulate() will update it */ |
| 239 | rde->offset = 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 240 | i++; |
| 241 | num--; |
| 242 | } |
| 243 | |
| 244 | done: |
| 245 | if (*p != end) |
| 246 | goto bad; |
| 247 | return 0; |
| 248 | |
| 249 | bad: |
| 250 | err = -EIO; |
| 251 | out_bad: |
| 252 | pr_err("problem parsing dir contents %d\n", err); |
| 253 | return err; |
| 254 | } |
| 255 | |
| 256 | /* |
Herb Shiu | 25933ab | 2010-12-01 14:14:38 -0800 | [diff] [blame] | 257 | * parse fcntl F_GETLK results |
| 258 | */ |
| 259 | static int parse_reply_info_filelock(void **p, void *end, |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 260 | struct ceph_mds_reply_info_parsed *info, |
Ilya Dryomov | 12b4629 | 2013-12-24 21:19:23 +0200 | [diff] [blame] | 261 | u64 features) |
Herb Shiu | 25933ab | 2010-12-01 14:14:38 -0800 | [diff] [blame] | 262 | { |
| 263 | if (*p + sizeof(*info->filelock_reply) > end) |
| 264 | goto bad; |
| 265 | |
| 266 | info->filelock_reply = *p; |
| 267 | *p += sizeof(*info->filelock_reply); |
| 268 | |
| 269 | if (unlikely(*p != end)) |
| 270 | goto bad; |
| 271 | return 0; |
| 272 | |
| 273 | bad: |
| 274 | return -EIO; |
| 275 | } |
| 276 | |
| 277 | /* |
Sam Lang | 6e8575f | 2012-12-28 09:56:46 -0800 | [diff] [blame] | 278 | * parse create results |
| 279 | */ |
| 280 | static int parse_reply_info_create(void **p, void *end, |
| 281 | struct ceph_mds_reply_info_parsed *info, |
Ilya Dryomov | 12b4629 | 2013-12-24 21:19:23 +0200 | [diff] [blame] | 282 | u64 features) |
Sam Lang | 6e8575f | 2012-12-28 09:56:46 -0800 | [diff] [blame] | 283 | { |
| 284 | if (features & CEPH_FEATURE_REPLY_CREATE_INODE) { |
| 285 | if (*p == end) { |
| 286 | info->has_create_ino = false; |
| 287 | } else { |
| 288 | info->has_create_ino = true; |
| 289 | info->ino = ceph_decode_64(p); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | if (unlikely(*p != end)) |
| 294 | goto bad; |
| 295 | return 0; |
| 296 | |
| 297 | bad: |
| 298 | return -EIO; |
| 299 | } |
| 300 | |
| 301 | /* |
Herb Shiu | 25933ab | 2010-12-01 14:14:38 -0800 | [diff] [blame] | 302 | * parse extra results |
| 303 | */ |
| 304 | static int parse_reply_info_extra(void **p, void *end, |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 305 | struct ceph_mds_reply_info_parsed *info, |
Ilya Dryomov | 12b4629 | 2013-12-24 21:19:23 +0200 | [diff] [blame] | 306 | u64 features) |
Herb Shiu | 25933ab | 2010-12-01 14:14:38 -0800 | [diff] [blame] | 307 | { |
Jeff Layton | 6df8c9d | 2017-01-12 14:42:41 -0500 | [diff] [blame] | 308 | u32 op = le32_to_cpu(info->head->op); |
| 309 | |
| 310 | if (op == CEPH_MDS_OP_GETFILELOCK) |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 311 | return parse_reply_info_filelock(p, end, info, features); |
Jeff Layton | 6df8c9d | 2017-01-12 14:42:41 -0500 | [diff] [blame] | 312 | else if (op == CEPH_MDS_OP_READDIR || op == CEPH_MDS_OP_LSSNAP) |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 313 | return parse_reply_info_dir(p, end, info, features); |
Jeff Layton | 6df8c9d | 2017-01-12 14:42:41 -0500 | [diff] [blame] | 314 | else if (op == CEPH_MDS_OP_CREATE) |
Sam Lang | 6e8575f | 2012-12-28 09:56:46 -0800 | [diff] [blame] | 315 | return parse_reply_info_create(p, end, info, features); |
| 316 | else |
| 317 | return -EIO; |
Herb Shiu | 25933ab | 2010-12-01 14:14:38 -0800 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /* |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 321 | * parse entire mds reply |
| 322 | */ |
| 323 | static int parse_reply_info(struct ceph_msg *msg, |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 324 | struct ceph_mds_reply_info_parsed *info, |
Ilya Dryomov | 12b4629 | 2013-12-24 21:19:23 +0200 | [diff] [blame] | 325 | u64 features) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 326 | { |
| 327 | void *p, *end; |
| 328 | u32 len; |
| 329 | int err; |
| 330 | |
| 331 | info->head = msg->front.iov_base; |
| 332 | p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head); |
| 333 | end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head); |
| 334 | |
| 335 | /* trace */ |
| 336 | ceph_decode_32_safe(&p, end, len, bad); |
| 337 | if (len > 0) { |
Xi Wang | 32852a8 | 2012-01-14 22:20:59 -0500 | [diff] [blame] | 338 | ceph_decode_need(&p, end, len, bad); |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 339 | err = parse_reply_info_trace(&p, p+len, info, features); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 340 | if (err < 0) |
| 341 | goto out_bad; |
| 342 | } |
| 343 | |
Herb Shiu | 25933ab | 2010-12-01 14:14:38 -0800 | [diff] [blame] | 344 | /* extra */ |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 345 | ceph_decode_32_safe(&p, end, len, bad); |
| 346 | if (len > 0) { |
Xi Wang | 32852a8 | 2012-01-14 22:20:59 -0500 | [diff] [blame] | 347 | ceph_decode_need(&p, end, len, bad); |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 348 | err = parse_reply_info_extra(&p, p+len, info, features); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 349 | if (err < 0) |
| 350 | goto out_bad; |
| 351 | } |
| 352 | |
| 353 | /* snap blob */ |
| 354 | ceph_decode_32_safe(&p, end, len, bad); |
| 355 | info->snapblob_len = len; |
| 356 | info->snapblob = p; |
| 357 | p += len; |
| 358 | |
| 359 | if (p != end) |
| 360 | goto bad; |
| 361 | return 0; |
| 362 | |
| 363 | bad: |
| 364 | err = -EIO; |
| 365 | out_bad: |
| 366 | pr_err("mds parse_reply err %d\n", err); |
| 367 | return err; |
| 368 | } |
| 369 | |
| 370 | static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info) |
| 371 | { |
Yan, Zheng | 2a5beea | 2016-04-28 09:37:39 +0800 | [diff] [blame] | 372 | if (!info->dir_entries) |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 373 | return; |
Yan, Zheng | 2a5beea | 2016-04-28 09:37:39 +0800 | [diff] [blame] | 374 | free_pages((unsigned long)info->dir_entries, get_order(info->dir_buf_size)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | |
| 378 | /* |
| 379 | * sessions |
| 380 | */ |
John Spray | a687eca | 2014-09-19 13:51:08 +0100 | [diff] [blame] | 381 | const char *ceph_session_state_name(int s) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 382 | { |
| 383 | switch (s) { |
| 384 | case CEPH_MDS_SESSION_NEW: return "new"; |
| 385 | case CEPH_MDS_SESSION_OPENING: return "opening"; |
| 386 | case CEPH_MDS_SESSION_OPEN: return "open"; |
| 387 | case CEPH_MDS_SESSION_HUNG: return "hung"; |
| 388 | case CEPH_MDS_SESSION_CLOSING: return "closing"; |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 389 | case CEPH_MDS_SESSION_RESTARTING: return "restarting"; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 390 | case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting"; |
Yan, Zheng | fcff415 | 2016-09-14 16:39:51 +0800 | [diff] [blame] | 391 | case CEPH_MDS_SESSION_REJECTED: return "rejected"; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 392 | default: return "???"; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | static struct ceph_mds_session *get_session(struct ceph_mds_session *s) |
| 397 | { |
Elena Reshetova | 3997c01 | 2017-03-03 11:15:06 +0200 | [diff] [blame] | 398 | if (refcount_inc_not_zero(&s->s_ref)) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 399 | dout("mdsc get_session %p %d -> %d\n", s, |
Elena Reshetova | 3997c01 | 2017-03-03 11:15:06 +0200 | [diff] [blame] | 400 | refcount_read(&s->s_ref)-1, refcount_read(&s->s_ref)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 401 | return s; |
| 402 | } else { |
Chengguang Xu | 4c069a5 | 2018-01-30 16:29:17 +0800 | [diff] [blame] | 403 | dout("mdsc get_session %p 0 -- FAIL\n", s); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 404 | return NULL; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | void ceph_put_mds_session(struct ceph_mds_session *s) |
| 409 | { |
| 410 | dout("mdsc put_session %p %d -> %d\n", s, |
Elena Reshetova | 3997c01 | 2017-03-03 11:15:06 +0200 | [diff] [blame] | 411 | refcount_read(&s->s_ref), refcount_read(&s->s_ref)-1); |
| 412 | if (refcount_dec_and_test(&s->s_ref)) { |
Alex Elder | 6c4a191 | 2012-05-16 15:16:38 -0500 | [diff] [blame] | 413 | if (s->s_auth.authorizer) |
Ilya Dryomov | 6c1ea26 | 2016-04-11 19:34:49 +0200 | [diff] [blame] | 414 | ceph_auth_destroy_authorizer(s->s_auth.authorizer); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 415 | kfree(s); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 416 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /* |
| 420 | * called under mdsc->mutex |
| 421 | */ |
| 422 | struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc, |
| 423 | int mds) |
| 424 | { |
| 425 | struct ceph_mds_session *session; |
| 426 | |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 427 | if (mds >= mdsc->max_sessions || !mdsc->sessions[mds]) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 428 | return NULL; |
| 429 | session = mdsc->sessions[mds]; |
| 430 | dout("lookup_mds_session %p %d\n", session, |
Elena Reshetova | 3997c01 | 2017-03-03 11:15:06 +0200 | [diff] [blame] | 431 | refcount_read(&session->s_ref)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 432 | get_session(session); |
| 433 | return session; |
| 434 | } |
| 435 | |
| 436 | static bool __have_session(struct ceph_mds_client *mdsc, int mds) |
| 437 | { |
Chengguang Xu | 98cfda8 | 2018-03-13 10:43:45 +0800 | [diff] [blame] | 438 | if (mds >= mdsc->max_sessions || !mdsc->sessions[mds]) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 439 | return false; |
Chengguang Xu | 98cfda8 | 2018-03-13 10:43:45 +0800 | [diff] [blame] | 440 | else |
| 441 | return true; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 444 | static int __verify_registered_session(struct ceph_mds_client *mdsc, |
| 445 | struct ceph_mds_session *s) |
| 446 | { |
| 447 | if (s->s_mds >= mdsc->max_sessions || |
| 448 | mdsc->sessions[s->s_mds] != s) |
| 449 | return -ENOENT; |
| 450 | return 0; |
| 451 | } |
| 452 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 453 | /* |
| 454 | * create+register a new session for given mds. |
| 455 | * called under mdsc->mutex. |
| 456 | */ |
| 457 | static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc, |
| 458 | int mds) |
| 459 | { |
| 460 | struct ceph_mds_session *s; |
| 461 | |
Yan, Zheng | 76201b6 | 2017-03-28 17:04:13 +0800 | [diff] [blame] | 462 | if (mds >= mdsc->mdsmap->m_num_mds) |
Nathaniel Yazdani | c338c07 | 2013-08-04 21:04:30 -0700 | [diff] [blame] | 463 | return ERR_PTR(-EINVAL); |
| 464 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 465 | s = kzalloc(sizeof(*s), GFP_NOFS); |
Dan Carpenter | 4736b00 | 2010-03-20 15:30:16 +0300 | [diff] [blame] | 466 | if (!s) |
| 467 | return ERR_PTR(-ENOMEM); |
Chengguang Xu | 47474d0 | 2018-03-13 23:01:07 +0800 | [diff] [blame] | 468 | |
| 469 | if (mds >= mdsc->max_sessions) { |
| 470 | int newmax = 1 << get_count_order(mds + 1); |
| 471 | struct ceph_mds_session **sa; |
| 472 | |
| 473 | dout("%s: realloc to %d\n", __func__, newmax); |
| 474 | sa = kcalloc(newmax, sizeof(void *), GFP_NOFS); |
| 475 | if (!sa) |
| 476 | goto fail_realloc; |
| 477 | if (mdsc->sessions) { |
| 478 | memcpy(sa, mdsc->sessions, |
| 479 | mdsc->max_sessions * sizeof(void *)); |
| 480 | kfree(mdsc->sessions); |
| 481 | } |
| 482 | mdsc->sessions = sa; |
| 483 | mdsc->max_sessions = newmax; |
| 484 | } |
| 485 | |
| 486 | dout("%s: mds%d\n", __func__, mds); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 487 | s->s_mdsc = mdsc; |
| 488 | s->s_mds = mds; |
| 489 | s->s_state = CEPH_MDS_SESSION_NEW; |
| 490 | s->s_ttl = 0; |
| 491 | s->s_seq = 0; |
| 492 | mutex_init(&s->s_mutex); |
| 493 | |
Sage Weil | b7a9e5d | 2012-06-27 12:24:08 -0700 | [diff] [blame] | 494 | ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 495 | |
Alex Elder | d8fb02a | 2012-01-12 17:48:10 -0800 | [diff] [blame] | 496 | spin_lock_init(&s->s_gen_ttl_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 497 | s->s_cap_gen = 0; |
Alex Elder | 1ce208a | 2012-01-12 17:48:11 -0800 | [diff] [blame] | 498 | s->s_cap_ttl = jiffies - 1; |
Alex Elder | d8fb02a | 2012-01-12 17:48:10 -0800 | [diff] [blame] | 499 | |
| 500 | spin_lock_init(&s->s_cap_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 501 | s->s_renew_requested = 0; |
| 502 | s->s_renew_seq = 0; |
| 503 | INIT_LIST_HEAD(&s->s_caps); |
| 504 | s->s_nr_caps = 0; |
Sage Weil | 5dacf09 | 2009-12-21 20:40:34 -0800 | [diff] [blame] | 505 | s->s_trim_caps = 0; |
Elena Reshetova | 3997c01 | 2017-03-03 11:15:06 +0200 | [diff] [blame] | 506 | refcount_set(&s->s_ref, 1); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 507 | INIT_LIST_HEAD(&s->s_waiting); |
| 508 | INIT_LIST_HEAD(&s->s_unsafe); |
| 509 | s->s_num_cap_releases = 0; |
Yan, Zheng | 99a9c27 | 2013-09-22 11:08:14 +0800 | [diff] [blame] | 510 | s->s_cap_reconnect = 0; |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 511 | s->s_cap_iterator = NULL; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 512 | INIT_LIST_HEAD(&s->s_cap_releases); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 513 | INIT_LIST_HEAD(&s->s_cap_flushing); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 514 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 515 | mdsc->sessions[mds] = s; |
Yan, Zheng | 86d8f67 | 2015-01-09 17:00:42 +0800 | [diff] [blame] | 516 | atomic_inc(&mdsc->num_sessions); |
Elena Reshetova | 3997c01 | 2017-03-03 11:15:06 +0200 | [diff] [blame] | 517 | refcount_inc(&s->s_ref); /* one ref to sessions[], one to caller */ |
Sage Weil | 42ce56e | 2009-11-18 11:22:36 -0800 | [diff] [blame] | 518 | |
Sage Weil | b7a9e5d | 2012-06-27 12:24:08 -0700 | [diff] [blame] | 519 | ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds, |
| 520 | ceph_mdsmap_get_addr(mdsc->mdsmap, mds)); |
Sage Weil | 42ce56e | 2009-11-18 11:22:36 -0800 | [diff] [blame] | 521 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 522 | return s; |
Sage Weil | 42ce56e | 2009-11-18 11:22:36 -0800 | [diff] [blame] | 523 | |
| 524 | fail_realloc: |
| 525 | kfree(s); |
| 526 | return ERR_PTR(-ENOMEM); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | /* |
| 530 | * called under mdsc->mutex |
| 531 | */ |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 532 | static void __unregister_session(struct ceph_mds_client *mdsc, |
Sage Weil | 42ce56e | 2009-11-18 11:22:36 -0800 | [diff] [blame] | 533 | struct ceph_mds_session *s) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 534 | { |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 535 | dout("__unregister_session mds%d %p\n", s->s_mds, s); |
| 536 | BUG_ON(mdsc->sessions[s->s_mds] != s); |
Sage Weil | 42ce56e | 2009-11-18 11:22:36 -0800 | [diff] [blame] | 537 | mdsc->sessions[s->s_mds] = NULL; |
| 538 | ceph_con_close(&s->s_con); |
| 539 | ceph_put_mds_session(s); |
Yan, Zheng | 86d8f67 | 2015-01-09 17:00:42 +0800 | [diff] [blame] | 540 | atomic_dec(&mdsc->num_sessions); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /* |
| 544 | * drop session refs in request. |
| 545 | * |
| 546 | * should be last request ref, or hold mdsc->mutex |
| 547 | */ |
| 548 | static void put_request_session(struct ceph_mds_request *req) |
| 549 | { |
| 550 | if (req->r_session) { |
| 551 | ceph_put_mds_session(req->r_session); |
| 552 | req->r_session = NULL; |
| 553 | } |
| 554 | } |
| 555 | |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 556 | void ceph_mdsc_release_request(struct kref *kref) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 557 | { |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 558 | struct ceph_mds_request *req = container_of(kref, |
| 559 | struct ceph_mds_request, |
| 560 | r_kref); |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 561 | destroy_reply_info(&req->r_reply_info); |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 562 | if (req->r_request) |
| 563 | ceph_msg_put(req->r_request); |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 564 | if (req->r_reply) |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 565 | ceph_msg_put(req->r_reply); |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 566 | if (req->r_inode) { |
Sage Weil | 41b02e1 | 2011-07-26 11:31:14 -0700 | [diff] [blame] | 567 | ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN); |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 568 | iput(req->r_inode); |
| 569 | } |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 570 | if (req->r_parent) |
| 571 | ceph_put_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN); |
SF Markus Elfring | e96a650 | 2014-11-02 15:20:59 +0100 | [diff] [blame] | 572 | iput(req->r_target_inode); |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 573 | if (req->r_dentry) |
| 574 | dput(req->r_dentry); |
Sage Weil | 844d87c | 2013-02-05 13:40:09 -0800 | [diff] [blame] | 575 | if (req->r_old_dentry) |
| 576 | dput(req->r_old_dentry); |
| 577 | if (req->r_old_dentry_dir) { |
Sage Weil | 41b02e1 | 2011-07-26 11:31:14 -0700 | [diff] [blame] | 578 | /* |
| 579 | * track (and drop pins for) r_old_dentry_dir |
| 580 | * separately, since r_old_dentry's d_parent may have |
| 581 | * changed between the dir mutex being dropped and |
| 582 | * this request being freed. |
| 583 | */ |
| 584 | ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir), |
| 585 | CEPH_CAP_PIN); |
Sage Weil | 41b02e1 | 2011-07-26 11:31:14 -0700 | [diff] [blame] | 586 | iput(req->r_old_dentry_dir); |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 587 | } |
| 588 | kfree(req->r_path1); |
| 589 | kfree(req->r_path2); |
Yan, Zheng | 25e6bae | 2014-09-16 19:15:28 +0800 | [diff] [blame] | 590 | if (req->r_pagelist) |
| 591 | ceph_pagelist_release(req->r_pagelist); |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 592 | put_request_session(req); |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 593 | ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation); |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 594 | kfree(req); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Ilya Dryomov | fcd00b6 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 597 | DEFINE_RB_FUNCS(request, struct ceph_mds_request, r_tid, r_node) |
| 598 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 599 | /* |
| 600 | * lookup session, bump ref if found. |
| 601 | * |
| 602 | * called under mdsc->mutex. |
| 603 | */ |
Ilya Dryomov | fcd00b6 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 604 | static struct ceph_mds_request * |
| 605 | lookup_get_request(struct ceph_mds_client *mdsc, u64 tid) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 606 | { |
| 607 | struct ceph_mds_request *req; |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 608 | |
Ilya Dryomov | fcd00b6 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 609 | req = lookup_request(&mdsc->request_tree, tid); |
| 610 | if (req) |
| 611 | ceph_mdsc_get_request(req); |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 612 | |
Ilya Dryomov | fcd00b6 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 613 | return req; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | /* |
| 617 | * Register an in-flight request, and assign a tid. Link to directory |
| 618 | * are modifying (if any). |
| 619 | * |
| 620 | * Called under mdsc->mutex. |
| 621 | */ |
| 622 | static void __register_request(struct ceph_mds_client *mdsc, |
| 623 | struct ceph_mds_request *req, |
| 624 | struct inode *dir) |
| 625 | { |
Zhi Zhang | e30ee58 | 2018-01-24 21:24:33 +0800 | [diff] [blame] | 626 | int ret = 0; |
| 627 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 628 | req->r_tid = ++mdsc->last_tid; |
Zhi Zhang | e30ee58 | 2018-01-24 21:24:33 +0800 | [diff] [blame] | 629 | if (req->r_num_caps) { |
| 630 | ret = ceph_reserve_caps(mdsc, &req->r_caps_reservation, |
| 631 | req->r_num_caps); |
| 632 | if (ret < 0) { |
| 633 | pr_err("__register_request %p " |
| 634 | "failed to reserve caps: %d\n", req, ret); |
| 635 | /* set req->r_err to fail early from __do_request */ |
| 636 | req->r_err = ret; |
| 637 | return; |
| 638 | } |
| 639 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 640 | dout("__register_request %p tid %lld\n", req, req->r_tid); |
| 641 | ceph_mdsc_get_request(req); |
Ilya Dryomov | fcd00b6 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 642 | insert_request(&mdsc->request_tree, req); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 643 | |
Sage Weil | cb4276c | 2010-11-08 07:28:52 -0800 | [diff] [blame] | 644 | req->r_uid = current_fsuid(); |
| 645 | req->r_gid = current_fsgid(); |
| 646 | |
Yan, Zheng | e8a7b8b | 2015-05-19 18:54:40 +0800 | [diff] [blame] | 647 | if (mdsc->oldest_tid == 0 && req->r_op != CEPH_MDS_OP_SETFILELOCK) |
| 648 | mdsc->oldest_tid = req->r_tid; |
| 649 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 650 | if (dir) { |
Sage Weil | 3b66378 | 2011-05-18 16:12:12 -0700 | [diff] [blame] | 651 | ihold(dir); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 652 | req->r_unsafe_dir = dir; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
| 656 | static void __unregister_request(struct ceph_mds_client *mdsc, |
| 657 | struct ceph_mds_request *req) |
| 658 | { |
| 659 | dout("__unregister_request %p tid %lld\n", req, req->r_tid); |
Yan, Zheng | e8a7b8b | 2015-05-19 18:54:40 +0800 | [diff] [blame] | 660 | |
Jeff Layton | df963ea | 2017-02-14 10:09:40 -0500 | [diff] [blame] | 661 | /* Never leave an unregistered request on an unsafe list! */ |
| 662 | list_del_init(&req->r_unsafe_item); |
| 663 | |
Yan, Zheng | e8a7b8b | 2015-05-19 18:54:40 +0800 | [diff] [blame] | 664 | if (req->r_tid == mdsc->oldest_tid) { |
| 665 | struct rb_node *p = rb_next(&req->r_node); |
| 666 | mdsc->oldest_tid = 0; |
| 667 | while (p) { |
| 668 | struct ceph_mds_request *next_req = |
| 669 | rb_entry(p, struct ceph_mds_request, r_node); |
| 670 | if (next_req->r_op != CEPH_MDS_OP_SETFILELOCK) { |
| 671 | mdsc->oldest_tid = next_req->r_tid; |
| 672 | break; |
| 673 | } |
| 674 | p = rb_next(p); |
| 675 | } |
| 676 | } |
| 677 | |
Ilya Dryomov | fcd00b6 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 678 | erase_request(&mdsc->request_tree, req); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 679 | |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 680 | if (req->r_unsafe_dir && |
| 681 | test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 682 | struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 683 | spin_lock(&ci->i_unsafe_lock); |
| 684 | list_del_init(&req->r_unsafe_dir_item); |
| 685 | spin_unlock(&ci->i_unsafe_lock); |
Yan, Zheng | 4c06ace | 2015-10-27 17:18:00 +0800 | [diff] [blame] | 686 | } |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 687 | if (req->r_target_inode && |
| 688 | test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) { |
Yan, Zheng | 68cd5b4 | 2015-10-27 18:36:06 +0800 | [diff] [blame] | 689 | struct ceph_inode_info *ci = ceph_inode(req->r_target_inode); |
| 690 | spin_lock(&ci->i_unsafe_lock); |
| 691 | list_del_init(&req->r_unsafe_target_item); |
| 692 | spin_unlock(&ci->i_unsafe_lock); |
| 693 | } |
Sage Weil | 3b66378 | 2011-05-18 16:12:12 -0700 | [diff] [blame] | 694 | |
Yan, Zheng | 4c06ace | 2015-10-27 17:18:00 +0800 | [diff] [blame] | 695 | if (req->r_unsafe_dir) { |
Sage Weil | 3b66378 | 2011-05-18 16:12:12 -0700 | [diff] [blame] | 696 | iput(req->r_unsafe_dir); |
| 697 | req->r_unsafe_dir = NULL; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 698 | } |
Sage Weil | 94aa8ae | 2010-03-28 21:22:50 -0700 | [diff] [blame] | 699 | |
Yan, Zheng | fc55d2c | 2013-10-31 09:10:47 +0800 | [diff] [blame] | 700 | complete_all(&req->r_safe_completion); |
| 701 | |
Sage Weil | 94aa8ae | 2010-03-28 21:22:50 -0700 | [diff] [blame] | 702 | ceph_mdsc_put_request(req); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | /* |
Jeff Layton | 30c7123 | 2016-12-15 08:37:56 -0500 | [diff] [blame] | 706 | * Walk back up the dentry tree until we hit a dentry representing a |
| 707 | * non-snapshot inode. We do this using the rcu_read_lock (which must be held |
| 708 | * when calling this) to ensure that the objects won't disappear while we're |
| 709 | * working with them. Once we hit a candidate dentry, we attempt to take a |
| 710 | * reference to it, and return that as the result. |
| 711 | */ |
Dan Carpenter | f107548 | 2017-02-23 13:39:59 +0300 | [diff] [blame] | 712 | static struct inode *get_nonsnap_parent(struct dentry *dentry) |
| 713 | { |
| 714 | struct inode *inode = NULL; |
Jeff Layton | 30c7123 | 2016-12-15 08:37:56 -0500 | [diff] [blame] | 715 | |
| 716 | while (dentry && !IS_ROOT(dentry)) { |
| 717 | inode = d_inode_rcu(dentry); |
| 718 | if (!inode || ceph_snap(inode) == CEPH_NOSNAP) |
| 719 | break; |
| 720 | dentry = dentry->d_parent; |
| 721 | } |
| 722 | if (inode) |
| 723 | inode = igrab(inode); |
| 724 | return inode; |
| 725 | } |
| 726 | |
| 727 | /* |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 728 | * Choose mds to send request to next. If there is a hint set in the |
| 729 | * request (e.g., due to a prior forward hint from the mds), use that. |
| 730 | * Otherwise, consult frag tree and/or caps to identify the |
| 731 | * appropriate mds. If all else fails, choose randomly. |
| 732 | * |
| 733 | * Called under mdsc->mutex. |
| 734 | */ |
| 735 | static int __choose_mds(struct ceph_mds_client *mdsc, |
| 736 | struct ceph_mds_request *req) |
| 737 | { |
| 738 | struct inode *inode; |
| 739 | struct ceph_inode_info *ci; |
| 740 | struct ceph_cap *cap; |
| 741 | int mode = req->r_direct_mode; |
| 742 | int mds = -1; |
| 743 | u32 hash = req->r_direct_hash; |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 744 | bool is_hash = test_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 745 | |
| 746 | /* |
| 747 | * is there a specific mds we should try? ignore hint if we have |
| 748 | * no session and the mds is not up (active or recovering). |
| 749 | */ |
| 750 | if (req->r_resend_mds >= 0 && |
| 751 | (__have_session(mdsc, req->r_resend_mds) || |
| 752 | ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) { |
| 753 | dout("choose_mds using resend_mds mds%d\n", |
| 754 | req->r_resend_mds); |
| 755 | return req->r_resend_mds; |
| 756 | } |
| 757 | |
| 758 | if (mode == USE_RANDOM_MDS) |
| 759 | goto random; |
| 760 | |
| 761 | inode = NULL; |
| 762 | if (req->r_inode) { |
Yan, Zheng | 5d37ca1 | 2017-07-26 12:48:08 +0800 | [diff] [blame] | 763 | if (ceph_snap(req->r_inode) != CEPH_SNAPDIR) { |
| 764 | inode = req->r_inode; |
| 765 | ihold(inode); |
| 766 | } else { |
Yan, Zheng | 38f340c | 2017-09-22 11:41:06 +0800 | [diff] [blame] | 767 | /* req->r_dentry is non-null for LSSNAP request */ |
| 768 | rcu_read_lock(); |
| 769 | inode = get_nonsnap_parent(req->r_dentry); |
| 770 | rcu_read_unlock(); |
| 771 | dout("__choose_mds using snapdir's parent %p\n", inode); |
Yan, Zheng | 5d37ca1 | 2017-07-26 12:48:08 +0800 | [diff] [blame] | 772 | } |
Yan, Zheng | 38f340c | 2017-09-22 11:41:06 +0800 | [diff] [blame] | 773 | } else if (req->r_dentry) { |
Sage Weil | d79698d | 2011-07-26 11:31:26 -0700 | [diff] [blame] | 774 | /* ignore race with rename; old or new d_parent is okay */ |
Jeff Layton | 30c7123 | 2016-12-15 08:37:56 -0500 | [diff] [blame] | 775 | struct dentry *parent; |
| 776 | struct inode *dir; |
Sage Weil | eb6bb1c | 2010-08-16 09:21:27 -0700 | [diff] [blame] | 777 | |
Jeff Layton | 30c7123 | 2016-12-15 08:37:56 -0500 | [diff] [blame] | 778 | rcu_read_lock(); |
| 779 | parent = req->r_dentry->d_parent; |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 780 | dir = req->r_parent ? : d_inode_rcu(parent); |
Jeff Layton | 30c7123 | 2016-12-15 08:37:56 -0500 | [diff] [blame] | 781 | |
| 782 | if (!dir || dir->i_sb != mdsc->fsc->sb) { |
| 783 | /* not this fs or parent went negative */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 784 | inode = d_inode(req->r_dentry); |
Jeff Layton | 30c7123 | 2016-12-15 08:37:56 -0500 | [diff] [blame] | 785 | if (inode) |
| 786 | ihold(inode); |
Sage Weil | eb6bb1c | 2010-08-16 09:21:27 -0700 | [diff] [blame] | 787 | } else if (ceph_snap(dir) != CEPH_NOSNAP) { |
| 788 | /* direct snapped/virtual snapdir requests |
| 789 | * based on parent dir inode */ |
Jeff Layton | 30c7123 | 2016-12-15 08:37:56 -0500 | [diff] [blame] | 790 | inode = get_nonsnap_parent(parent); |
Sage Weil | eb6bb1c | 2010-08-16 09:21:27 -0700 | [diff] [blame] | 791 | dout("__choose_mds using nonsnap parent %p\n", inode); |
Yan, Zheng | ca18bed | 2013-11-22 14:21:44 +0800 | [diff] [blame] | 792 | } else { |
Sage Weil | eb6bb1c | 2010-08-16 09:21:27 -0700 | [diff] [blame] | 793 | /* dentry target */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 794 | inode = d_inode(req->r_dentry); |
Yan, Zheng | ca18bed | 2013-11-22 14:21:44 +0800 | [diff] [blame] | 795 | if (!inode || mode == USE_AUTH_MDS) { |
| 796 | /* dir + name */ |
Jeff Layton | 30c7123 | 2016-12-15 08:37:56 -0500 | [diff] [blame] | 797 | inode = igrab(dir); |
Yan, Zheng | ca18bed | 2013-11-22 14:21:44 +0800 | [diff] [blame] | 798 | hash = ceph_dentry_hash(dir, req->r_dentry); |
| 799 | is_hash = true; |
Jeff Layton | 30c7123 | 2016-12-15 08:37:56 -0500 | [diff] [blame] | 800 | } else { |
| 801 | ihold(inode); |
Yan, Zheng | ca18bed | 2013-11-22 14:21:44 +0800 | [diff] [blame] | 802 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 803 | } |
Jeff Layton | 30c7123 | 2016-12-15 08:37:56 -0500 | [diff] [blame] | 804 | rcu_read_unlock(); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 805 | } |
Sage Weil | eb6bb1c | 2010-08-16 09:21:27 -0700 | [diff] [blame] | 806 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 807 | dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash, |
| 808 | (int)hash, mode); |
| 809 | if (!inode) |
| 810 | goto random; |
| 811 | ci = ceph_inode(inode); |
| 812 | |
| 813 | if (is_hash && S_ISDIR(inode->i_mode)) { |
| 814 | struct ceph_inode_frag frag; |
| 815 | int found; |
| 816 | |
| 817 | ceph_choose_frag(ci, hash, &frag, &found); |
| 818 | if (found) { |
| 819 | if (mode == USE_ANY_MDS && frag.ndist > 0) { |
| 820 | u8 r; |
| 821 | |
| 822 | /* choose a random replica */ |
| 823 | get_random_bytes(&r, 1); |
| 824 | r %= frag.ndist; |
| 825 | mds = frag.dist[r]; |
| 826 | dout("choose_mds %p %llx.%llx " |
| 827 | "frag %u mds%d (%d/%d)\n", |
| 828 | inode, ceph_vinop(inode), |
Sage Weil | d66bbd4 | 2011-01-21 21:16:46 -0800 | [diff] [blame] | 829 | frag.frag, mds, |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 830 | (int)r, frag.ndist); |
Sage Weil | d66bbd4 | 2011-01-21 21:16:46 -0800 | [diff] [blame] | 831 | if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >= |
| 832 | CEPH_MDS_STATE_ACTIVE) |
Jeff Layton | 30c7123 | 2016-12-15 08:37:56 -0500 | [diff] [blame] | 833 | goto out; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | /* since this file/dir wasn't known to be |
| 837 | * replicated, then we want to look for the |
| 838 | * authoritative mds. */ |
| 839 | mode = USE_AUTH_MDS; |
| 840 | if (frag.mds >= 0) { |
| 841 | /* choose auth mds */ |
| 842 | mds = frag.mds; |
| 843 | dout("choose_mds %p %llx.%llx " |
| 844 | "frag %u mds%d (auth)\n", |
| 845 | inode, ceph_vinop(inode), frag.frag, mds); |
Sage Weil | d66bbd4 | 2011-01-21 21:16:46 -0800 | [diff] [blame] | 846 | if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >= |
| 847 | CEPH_MDS_STATE_ACTIVE) |
Jeff Layton | 30c7123 | 2016-12-15 08:37:56 -0500 | [diff] [blame] | 848 | goto out; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 849 | } |
| 850 | } |
| 851 | } |
| 852 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 853 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 854 | cap = NULL; |
| 855 | if (mode == USE_AUTH_MDS) |
| 856 | cap = ci->i_auth_cap; |
| 857 | if (!cap && !RB_EMPTY_ROOT(&ci->i_caps)) |
| 858 | cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node); |
| 859 | if (!cap) { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 860 | spin_unlock(&ci->i_ceph_lock); |
Jeff Layton | 30c7123 | 2016-12-15 08:37:56 -0500 | [diff] [blame] | 861 | iput(inode); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 862 | goto random; |
| 863 | } |
| 864 | mds = cap->session->s_mds; |
| 865 | dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n", |
| 866 | inode, ceph_vinop(inode), mds, |
| 867 | cap == ci->i_auth_cap ? "auth " : "", cap); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 868 | spin_unlock(&ci->i_ceph_lock); |
Jeff Layton | 30c7123 | 2016-12-15 08:37:56 -0500 | [diff] [blame] | 869 | out: |
| 870 | iput(inode); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 871 | return mds; |
| 872 | |
| 873 | random: |
| 874 | mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap); |
| 875 | dout("choose_mds chose random mds%d\n", mds); |
| 876 | return mds; |
| 877 | } |
| 878 | |
| 879 | |
| 880 | /* |
| 881 | * session messages |
| 882 | */ |
| 883 | static struct ceph_msg *create_session_msg(u32 op, u64 seq) |
| 884 | { |
| 885 | struct ceph_msg *msg; |
| 886 | struct ceph_mds_session_head *h; |
| 887 | |
Sage Weil | b61c276 | 2011-08-09 15:03:46 -0700 | [diff] [blame] | 888 | msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS, |
| 889 | false); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 890 | if (!msg) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 891 | pr_err("create_session_msg ENOMEM creating msg\n"); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 892 | return NULL; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 893 | } |
| 894 | h = msg->front.iov_base; |
| 895 | h->op = cpu_to_le32(op); |
| 896 | h->seq = cpu_to_le64(seq); |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 897 | |
| 898 | return msg; |
| 899 | } |
| 900 | |
Yan, Zheng | 342ce18 | 2018-05-11 18:47:29 +0800 | [diff] [blame] | 901 | static void encode_supported_features(void **p, void *end) |
| 902 | { |
| 903 | static const unsigned char bits[] = CEPHFS_FEATURES_CLIENT_SUPPORTED; |
| 904 | static const size_t count = ARRAY_SIZE(bits); |
| 905 | |
| 906 | if (count > 0) { |
| 907 | size_t i; |
| 908 | size_t size = ((size_t)bits[count - 1] + 64) / 64 * 8; |
| 909 | |
| 910 | BUG_ON(*p + 4 + size > end); |
| 911 | ceph_encode_32(p, size); |
| 912 | memset(*p, 0, size); |
| 913 | for (i = 0; i < count; i++) |
| 914 | ((unsigned char*)(*p))[i / 8] |= 1 << (bits[i] % 8); |
| 915 | *p += size; |
| 916 | } else { |
| 917 | BUG_ON(*p + 4 > end); |
| 918 | ceph_encode_32(p, 0); |
| 919 | } |
| 920 | } |
| 921 | |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 922 | /* |
| 923 | * session message, specialization for CEPH_SESSION_REQUEST_OPEN |
| 924 | * to include additional client metadata fields. |
| 925 | */ |
| 926 | static struct ceph_msg *create_session_open_msg(struct ceph_mds_client *mdsc, u64 seq) |
| 927 | { |
| 928 | struct ceph_msg *msg; |
| 929 | struct ceph_mds_session_head *h; |
| 930 | int i = -1; |
Yan, Zheng | 342ce18 | 2018-05-11 18:47:29 +0800 | [diff] [blame] | 931 | int extra_bytes = 0; |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 932 | int metadata_key_count = 0; |
| 933 | struct ceph_options *opt = mdsc->fsc->client->options; |
Yan, Zheng | 3f38495 | 2016-04-21 11:09:55 +0800 | [diff] [blame] | 934 | struct ceph_mount_options *fsopt = mdsc->fsc->mount_options; |
Yan, Zheng | 342ce18 | 2018-05-11 18:47:29 +0800 | [diff] [blame] | 935 | void *p, *end; |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 936 | |
Yan, Zheng | a6a5ce4 | 2015-01-16 10:54:43 +0800 | [diff] [blame] | 937 | const char* metadata[][2] = { |
Yan, Zheng | 717e6f2 | 2017-09-11 12:10:08 +0800 | [diff] [blame] | 938 | {"hostname", mdsc->nodename}, |
| 939 | {"kernel_version", init_utsname()->release}, |
Yan, Zheng | 3f38495 | 2016-04-21 11:09:55 +0800 | [diff] [blame] | 940 | {"entity_id", opt->name ? : ""}, |
| 941 | {"root", fsopt->server_path ? : "/"}, |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 942 | {NULL, NULL} |
| 943 | }; |
| 944 | |
| 945 | /* Calculate serialized length of metadata */ |
Yan, Zheng | 342ce18 | 2018-05-11 18:47:29 +0800 | [diff] [blame] | 946 | extra_bytes = 4; /* map length */ |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 947 | for (i = 0; metadata[i][0]; ++i) { |
Yan, Zheng | 342ce18 | 2018-05-11 18:47:29 +0800 | [diff] [blame] | 948 | extra_bytes += 8 + strlen(metadata[i][0]) + |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 949 | strlen(metadata[i][1]); |
| 950 | metadata_key_count++; |
| 951 | } |
Yan, Zheng | 342ce18 | 2018-05-11 18:47:29 +0800 | [diff] [blame] | 952 | /* supported feature */ |
| 953 | extra_bytes += 4 + 8; |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 954 | |
| 955 | /* Allocate the message */ |
Yan, Zheng | 342ce18 | 2018-05-11 18:47:29 +0800 | [diff] [blame] | 956 | msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + extra_bytes, |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 957 | GFP_NOFS, false); |
| 958 | if (!msg) { |
| 959 | pr_err("create_session_msg ENOMEM creating msg\n"); |
| 960 | return NULL; |
| 961 | } |
Yan, Zheng | 342ce18 | 2018-05-11 18:47:29 +0800 | [diff] [blame] | 962 | p = msg->front.iov_base; |
| 963 | end = p + msg->front.iov_len; |
| 964 | |
| 965 | h = p; |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 966 | h->op = cpu_to_le32(CEPH_SESSION_REQUEST_OPEN); |
| 967 | h->seq = cpu_to_le64(seq); |
| 968 | |
| 969 | /* |
| 970 | * Serialize client metadata into waiting buffer space, using |
| 971 | * the format that userspace expects for map<string, string> |
John Spray | 7cfa031 | 2014-10-30 17:15:26 +0000 | [diff] [blame] | 972 | * |
| 973 | * ClientSession messages with metadata are v2 |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 974 | */ |
Yan, Zheng | 342ce18 | 2018-05-11 18:47:29 +0800 | [diff] [blame] | 975 | msg->hdr.version = cpu_to_le16(3); |
John Spray | 7cfa031 | 2014-10-30 17:15:26 +0000 | [diff] [blame] | 976 | msg->hdr.compat_version = cpu_to_le16(1); |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 977 | |
| 978 | /* The write pointer, following the session_head structure */ |
Yan, Zheng | 342ce18 | 2018-05-11 18:47:29 +0800 | [diff] [blame] | 979 | p += sizeof(*h); |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 980 | |
| 981 | /* Number of entries in the map */ |
| 982 | ceph_encode_32(&p, metadata_key_count); |
| 983 | |
| 984 | /* Two length-prefixed strings for each entry in the map */ |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 985 | for (i = 0; metadata[i][0]; ++i) { |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 986 | size_t const key_len = strlen(metadata[i][0]); |
| 987 | size_t const val_len = strlen(metadata[i][1]); |
| 988 | |
| 989 | ceph_encode_32(&p, key_len); |
| 990 | memcpy(p, metadata[i][0], key_len); |
| 991 | p += key_len; |
| 992 | ceph_encode_32(&p, val_len); |
| 993 | memcpy(p, metadata[i][1], val_len); |
| 994 | p += val_len; |
| 995 | } |
| 996 | |
Yan, Zheng | 342ce18 | 2018-05-11 18:47:29 +0800 | [diff] [blame] | 997 | encode_supported_features(&p, end); |
| 998 | msg->front.iov_len = p - msg->front.iov_base; |
| 999 | msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); |
| 1000 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1001 | return msg; |
| 1002 | } |
| 1003 | |
| 1004 | /* |
| 1005 | * send session open request. |
| 1006 | * |
| 1007 | * called under mdsc->mutex |
| 1008 | */ |
| 1009 | static int __open_session(struct ceph_mds_client *mdsc, |
| 1010 | struct ceph_mds_session *session) |
| 1011 | { |
| 1012 | struct ceph_msg *msg; |
| 1013 | int mstate; |
| 1014 | int mds = session->s_mds; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1015 | |
| 1016 | /* wait for mds to go active? */ |
| 1017 | mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds); |
| 1018 | dout("open_session to mds%d (%s)\n", mds, |
| 1019 | ceph_mds_state_name(mstate)); |
| 1020 | session->s_state = CEPH_MDS_SESSION_OPENING; |
| 1021 | session->s_renew_requested = jiffies; |
| 1022 | |
| 1023 | /* send connect message */ |
John Spray | dbd0c8b | 2014-09-09 19:26:01 +0100 | [diff] [blame] | 1024 | msg = create_session_open_msg(mdsc, session->s_seq); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 1025 | if (!msg) |
| 1026 | return -ENOMEM; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1027 | ceph_con_send(&session->s_con, msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1028 | return 0; |
| 1029 | } |
| 1030 | |
| 1031 | /* |
Sage Weil | ed0552a | 2010-06-21 13:38:25 -0700 | [diff] [blame] | 1032 | * open sessions for any export targets for the given mds |
| 1033 | * |
| 1034 | * called under mdsc->mutex |
| 1035 | */ |
Yan, Zheng | 5d72d13 | 2013-11-24 14:33:01 +0800 | [diff] [blame] | 1036 | static struct ceph_mds_session * |
| 1037 | __open_export_target_session(struct ceph_mds_client *mdsc, int target) |
| 1038 | { |
| 1039 | struct ceph_mds_session *session; |
| 1040 | |
| 1041 | session = __ceph_lookup_mds_session(mdsc, target); |
| 1042 | if (!session) { |
| 1043 | session = register_session(mdsc, target); |
| 1044 | if (IS_ERR(session)) |
| 1045 | return session; |
| 1046 | } |
| 1047 | if (session->s_state == CEPH_MDS_SESSION_NEW || |
| 1048 | session->s_state == CEPH_MDS_SESSION_CLOSING) |
| 1049 | __open_session(mdsc, session); |
| 1050 | |
| 1051 | return session; |
| 1052 | } |
| 1053 | |
| 1054 | struct ceph_mds_session * |
| 1055 | ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target) |
| 1056 | { |
| 1057 | struct ceph_mds_session *session; |
| 1058 | |
| 1059 | dout("open_export_target_session to mds%d\n", target); |
| 1060 | |
| 1061 | mutex_lock(&mdsc->mutex); |
| 1062 | session = __open_export_target_session(mdsc, target); |
| 1063 | mutex_unlock(&mdsc->mutex); |
| 1064 | |
| 1065 | return session; |
| 1066 | } |
| 1067 | |
Sage Weil | ed0552a | 2010-06-21 13:38:25 -0700 | [diff] [blame] | 1068 | static void __open_export_target_sessions(struct ceph_mds_client *mdsc, |
| 1069 | struct ceph_mds_session *session) |
| 1070 | { |
| 1071 | struct ceph_mds_info *mi; |
| 1072 | struct ceph_mds_session *ts; |
| 1073 | int i, mds = session->s_mds; |
Sage Weil | ed0552a | 2010-06-21 13:38:25 -0700 | [diff] [blame] | 1074 | |
Yan, Zheng | 76201b6 | 2017-03-28 17:04:13 +0800 | [diff] [blame] | 1075 | if (mds >= mdsc->mdsmap->m_num_mds) |
Sage Weil | ed0552a | 2010-06-21 13:38:25 -0700 | [diff] [blame] | 1076 | return; |
Yan, Zheng | 5d72d13 | 2013-11-24 14:33:01 +0800 | [diff] [blame] | 1077 | |
Sage Weil | ed0552a | 2010-06-21 13:38:25 -0700 | [diff] [blame] | 1078 | mi = &mdsc->mdsmap->m_info[mds]; |
| 1079 | dout("open_export_target_sessions for mds%d (%d targets)\n", |
| 1080 | session->s_mds, mi->num_export_targets); |
| 1081 | |
| 1082 | for (i = 0; i < mi->num_export_targets; i++) { |
Yan, Zheng | 5d72d13 | 2013-11-24 14:33:01 +0800 | [diff] [blame] | 1083 | ts = __open_export_target_session(mdsc, mi->export_targets[i]); |
| 1084 | if (!IS_ERR(ts)) |
| 1085 | ceph_put_mds_session(ts); |
Sage Weil | ed0552a | 2010-06-21 13:38:25 -0700 | [diff] [blame] | 1086 | } |
| 1087 | } |
| 1088 | |
Sage Weil | 154f42c | 2010-06-21 13:45:04 -0700 | [diff] [blame] | 1089 | void ceph_mdsc_open_export_target_sessions(struct ceph_mds_client *mdsc, |
| 1090 | struct ceph_mds_session *session) |
| 1091 | { |
| 1092 | mutex_lock(&mdsc->mutex); |
| 1093 | __open_export_target_sessions(mdsc, session); |
| 1094 | mutex_unlock(&mdsc->mutex); |
| 1095 | } |
| 1096 | |
Sage Weil | ed0552a | 2010-06-21 13:38:25 -0700 | [diff] [blame] | 1097 | /* |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1098 | * session caps |
| 1099 | */ |
| 1100 | |
Jeff Layton | c8a96a3 | 2017-10-19 08:53:58 -0400 | [diff] [blame] | 1101 | static void detach_cap_releases(struct ceph_mds_session *session, |
| 1102 | struct list_head *target) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1103 | { |
Jeff Layton | c8a96a3 | 2017-10-19 08:53:58 -0400 | [diff] [blame] | 1104 | lockdep_assert_held(&session->s_cap_lock); |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1105 | |
Jeff Layton | c8a96a3 | 2017-10-19 08:53:58 -0400 | [diff] [blame] | 1106 | list_splice_init(&session->s_cap_releases, target); |
| 1107 | session->s_num_cap_releases = 0; |
| 1108 | dout("dispose_cap_releases mds%d\n", session->s_mds); |
| 1109 | } |
| 1110 | |
| 1111 | static void dispose_cap_releases(struct ceph_mds_client *mdsc, |
| 1112 | struct list_head *dispose) |
| 1113 | { |
| 1114 | while (!list_empty(dispose)) { |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1115 | struct ceph_cap *cap; |
| 1116 | /* zero out the in-progress message */ |
Jeff Layton | c8a96a3 | 2017-10-19 08:53:58 -0400 | [diff] [blame] | 1117 | cap = list_first_entry(dispose, struct ceph_cap, session_caps); |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1118 | list_del(&cap->session_caps); |
| 1119 | ceph_put_cap(mdsc, cap); |
| 1120 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
Yan, Zheng | 1c841a9 | 2015-03-24 20:15:36 +0800 | [diff] [blame] | 1123 | static void cleanup_session_requests(struct ceph_mds_client *mdsc, |
| 1124 | struct ceph_mds_session *session) |
| 1125 | { |
| 1126 | struct ceph_mds_request *req; |
| 1127 | struct rb_node *p; |
| 1128 | |
| 1129 | dout("cleanup_session_requests mds%d\n", session->s_mds); |
| 1130 | mutex_lock(&mdsc->mutex); |
| 1131 | while (!list_empty(&session->s_unsafe)) { |
| 1132 | req = list_first_entry(&session->s_unsafe, |
| 1133 | struct ceph_mds_request, r_unsafe_item); |
Yan, Zheng | 3e0708b | 2015-05-22 16:38:02 +0800 | [diff] [blame] | 1134 | pr_warn_ratelimited(" dropping unsafe request %llu\n", |
| 1135 | req->r_tid); |
Yan, Zheng | 1c841a9 | 2015-03-24 20:15:36 +0800 | [diff] [blame] | 1136 | __unregister_request(mdsc, req); |
| 1137 | } |
| 1138 | /* zero r_attempts, so kick_requests() will re-send requests */ |
| 1139 | p = rb_first(&mdsc->request_tree); |
| 1140 | while (p) { |
| 1141 | req = rb_entry(p, struct ceph_mds_request, r_node); |
| 1142 | p = rb_next(p); |
| 1143 | if (req->r_session && |
| 1144 | req->r_session->s_mds == session->s_mds) |
| 1145 | req->r_attempts = 0; |
| 1146 | } |
| 1147 | mutex_unlock(&mdsc->mutex); |
| 1148 | } |
| 1149 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1150 | /* |
Sage Weil | f818a73 | 2010-05-11 20:56:31 -0700 | [diff] [blame] | 1151 | * Helper to safely iterate over all caps associated with a session, with |
| 1152 | * special care taken to handle a racing __ceph_remove_cap(). |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1153 | * |
Sage Weil | f818a73 | 2010-05-11 20:56:31 -0700 | [diff] [blame] | 1154 | * Caller must hold session s_mutex. |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1155 | */ |
| 1156 | static int iterate_session_caps(struct ceph_mds_session *session, |
| 1157 | int (*cb)(struct inode *, struct ceph_cap *, |
| 1158 | void *), void *arg) |
| 1159 | { |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1160 | struct list_head *p; |
| 1161 | struct ceph_cap *cap; |
| 1162 | struct inode *inode, *last_inode = NULL; |
| 1163 | struct ceph_cap *old_cap = NULL; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1164 | int ret; |
| 1165 | |
| 1166 | dout("iterate_session_caps %p mds%d\n", session, session->s_mds); |
| 1167 | spin_lock(&session->s_cap_lock); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1168 | p = session->s_caps.next; |
| 1169 | while (p != &session->s_caps) { |
| 1170 | cap = list_entry(p, struct ceph_cap, session_caps); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1171 | inode = igrab(&cap->ci->vfs_inode); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1172 | if (!inode) { |
| 1173 | p = p->next; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1174 | continue; |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1175 | } |
| 1176 | session->s_cap_iterator = cap; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1177 | spin_unlock(&session->s_cap_lock); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1178 | |
| 1179 | if (last_inode) { |
| 1180 | iput(last_inode); |
| 1181 | last_inode = NULL; |
| 1182 | } |
| 1183 | if (old_cap) { |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 1184 | ceph_put_cap(session->s_mdsc, old_cap); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1185 | old_cap = NULL; |
| 1186 | } |
| 1187 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1188 | ret = cb(inode, cap, arg); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1189 | last_inode = inode; |
| 1190 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1191 | spin_lock(&session->s_cap_lock); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1192 | p = p->next; |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 1193 | if (!cap->ci) { |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1194 | dout("iterate_session_caps finishing cap %p removal\n", |
| 1195 | cap); |
| 1196 | BUG_ON(cap->session != session); |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1197 | cap->session = NULL; |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1198 | list_del_init(&cap->session_caps); |
| 1199 | session->s_nr_caps--; |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1200 | if (cap->queue_release) { |
| 1201 | list_add_tail(&cap->session_caps, |
| 1202 | &session->s_cap_releases); |
| 1203 | session->s_num_cap_releases++; |
| 1204 | } else { |
| 1205 | old_cap = cap; /* put_cap it w/o locks held */ |
| 1206 | } |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1207 | } |
Sage Weil | 5dacf09 | 2009-12-21 20:40:34 -0800 | [diff] [blame] | 1208 | if (ret < 0) |
| 1209 | goto out; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1210 | } |
Sage Weil | 5dacf09 | 2009-12-21 20:40:34 -0800 | [diff] [blame] | 1211 | ret = 0; |
| 1212 | out: |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1213 | session->s_cap_iterator = NULL; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1214 | spin_unlock(&session->s_cap_lock); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1215 | |
SF Markus Elfring | e96a650 | 2014-11-02 15:20:59 +0100 | [diff] [blame] | 1216 | iput(last_inode); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1217 | if (old_cap) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 1218 | ceph_put_cap(session->s_mdsc, old_cap); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1219 | |
Sage Weil | 5dacf09 | 2009-12-21 20:40:34 -0800 | [diff] [blame] | 1220 | return ret; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap, |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1224 | void *arg) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1225 | { |
Yan, Zheng | 6c93df5 | 2016-04-15 13:56:12 +0800 | [diff] [blame] | 1226 | struct ceph_fs_client *fsc = (struct ceph_fs_client *)arg; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1227 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 1228 | LIST_HEAD(to_remove); |
Yan, Zheng | 6c93df5 | 2016-04-15 13:56:12 +0800 | [diff] [blame] | 1229 | bool drop = false; |
| 1230 | bool invalidate = false; |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1231 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1232 | dout("removing cap %p, ci is %p, inode is %p\n", |
| 1233 | cap, ci, &ci->vfs_inode); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1234 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | d2f8bb2 | 2018-12-10 16:35:09 +0800 | [diff] [blame] | 1235 | if (cap->mds_wanted | cap->issued) |
| 1236 | ci->i_ceph_flags |= CEPH_I_CAP_DROPPED; |
Yan, Zheng | a096b09 | 2013-09-22 10:15:58 +0800 | [diff] [blame] | 1237 | __ceph_remove_cap(cap, false); |
Yan, Zheng | 571ade3 | 2015-03-24 11:36:08 +0800 | [diff] [blame] | 1238 | if (!ci->i_auth_cap) { |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 1239 | struct ceph_cap_flush *cf; |
Yan, Zheng | 6c93df5 | 2016-04-15 13:56:12 +0800 | [diff] [blame] | 1240 | struct ceph_mds_client *mdsc = fsc->mdsc; |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1241 | |
Yan, Zheng | 6c93df5 | 2016-04-15 13:56:12 +0800 | [diff] [blame] | 1242 | if (ci->i_wrbuffer_ref > 0 && |
Seraphime Kirkovski | 52953d5 | 2016-12-26 10:26:34 +0100 | [diff] [blame] | 1243 | READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) |
Yan, Zheng | 6c93df5 | 2016-04-15 13:56:12 +0800 | [diff] [blame] | 1244 | invalidate = true; |
| 1245 | |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 1246 | while (!list_empty(&ci->i_cap_flush_list)) { |
| 1247 | cf = list_first_entry(&ci->i_cap_flush_list, |
| 1248 | struct ceph_cap_flush, i_list); |
Wei Yongjun | 8cdcc07 | 2016-08-13 09:10:28 +0000 | [diff] [blame] | 1249 | list_move(&cf->i_list, &to_remove); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 1250 | } |
| 1251 | |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1252 | spin_lock(&mdsc->cap_dirty_lock); |
Yan, Zheng | 8310b08 | 2015-06-09 17:20:12 +0800 | [diff] [blame] | 1253 | |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 1254 | list_for_each_entry(cf, &to_remove, i_list) |
| 1255 | list_del(&cf->g_list); |
Yan, Zheng | 8310b08 | 2015-06-09 17:20:12 +0800 | [diff] [blame] | 1256 | |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1257 | if (!list_empty(&ci->i_dirty_item)) { |
Yan, Zheng | 3e0708b | 2015-05-22 16:38:02 +0800 | [diff] [blame] | 1258 | pr_warn_ratelimited( |
| 1259 | " dropping dirty %s state for %p %lld\n", |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1260 | ceph_cap_string(ci->i_dirty_caps), |
| 1261 | inode, ceph_ino(inode)); |
| 1262 | ci->i_dirty_caps = 0; |
| 1263 | list_del_init(&ci->i_dirty_item); |
Yan, Zheng | 6c93df5 | 2016-04-15 13:56:12 +0800 | [diff] [blame] | 1264 | drop = true; |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1265 | } |
| 1266 | if (!list_empty(&ci->i_flushing_item)) { |
Yan, Zheng | 3e0708b | 2015-05-22 16:38:02 +0800 | [diff] [blame] | 1267 | pr_warn_ratelimited( |
| 1268 | " dropping dirty+flushing %s state for %p %lld\n", |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1269 | ceph_cap_string(ci->i_flushing_caps), |
| 1270 | inode, ceph_ino(inode)); |
| 1271 | ci->i_flushing_caps = 0; |
| 1272 | list_del_init(&ci->i_flushing_item); |
| 1273 | mdsc->num_cap_flushing--; |
Yan, Zheng | 6c93df5 | 2016-04-15 13:56:12 +0800 | [diff] [blame] | 1274 | drop = true; |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1275 | } |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1276 | spin_unlock(&mdsc->cap_dirty_lock); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 1277 | |
Yan, Zheng | b3f8d68 | 2017-09-11 10:58:55 +0800 | [diff] [blame] | 1278 | if (atomic_read(&ci->i_filelock_ref) > 0) { |
| 1279 | /* make further file lock syscall return -EIO */ |
| 1280 | ci->i_ceph_flags |= CEPH_I_ERROR_FILELOCK; |
| 1281 | pr_warn_ratelimited(" dropping file locks for %p %lld\n", |
| 1282 | inode, ceph_ino(inode)); |
| 1283 | } |
| 1284 | |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1285 | if (!ci->i_dirty_caps && ci->i_prealloc_cap_flush) { |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 1286 | list_add(&ci->i_prealloc_cap_flush->i_list, &to_remove); |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1287 | ci->i_prealloc_cap_flush = NULL; |
| 1288 | } |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1289 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1290 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 1291 | while (!list_empty(&to_remove)) { |
| 1292 | struct ceph_cap_flush *cf; |
| 1293 | cf = list_first_entry(&to_remove, |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 1294 | struct ceph_cap_flush, i_list); |
| 1295 | list_del(&cf->i_list); |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1296 | ceph_free_cap_flush(cf); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 1297 | } |
Yan, Zheng | 7731032 | 2016-04-08 15:27:16 +0800 | [diff] [blame] | 1298 | |
| 1299 | wake_up_all(&ci->i_cap_wq); |
Yan, Zheng | 6c93df5 | 2016-04-15 13:56:12 +0800 | [diff] [blame] | 1300 | if (invalidate) |
| 1301 | ceph_queue_invalidate(inode); |
Yan, Zheng | 7731032 | 2016-04-08 15:27:16 +0800 | [diff] [blame] | 1302 | if (drop) |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1303 | iput(inode); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1304 | return 0; |
| 1305 | } |
| 1306 | |
| 1307 | /* |
| 1308 | * caller must hold session s_mutex |
| 1309 | */ |
| 1310 | static void remove_session_caps(struct ceph_mds_session *session) |
| 1311 | { |
Yan, Zheng | 6c93df5 | 2016-04-15 13:56:12 +0800 | [diff] [blame] | 1312 | struct ceph_fs_client *fsc = session->s_mdsc->fsc; |
| 1313 | struct super_block *sb = fsc->sb; |
Jeff Layton | c8a96a3 | 2017-10-19 08:53:58 -0400 | [diff] [blame] | 1314 | LIST_HEAD(dispose); |
| 1315 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1316 | dout("remove_session_caps on %p\n", session); |
Yan, Zheng | 6c93df5 | 2016-04-15 13:56:12 +0800 | [diff] [blame] | 1317 | iterate_session_caps(session, remove_session_caps_cb, fsc); |
Yan, Zheng | 6f60f88 | 2013-07-24 12:22:11 +0800 | [diff] [blame] | 1318 | |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 1319 | wake_up_all(&fsc->mdsc->cap_flushing_wq); |
| 1320 | |
Yan, Zheng | 6f60f88 | 2013-07-24 12:22:11 +0800 | [diff] [blame] | 1321 | spin_lock(&session->s_cap_lock); |
| 1322 | if (session->s_nr_caps > 0) { |
Yan, Zheng | 6f60f88 | 2013-07-24 12:22:11 +0800 | [diff] [blame] | 1323 | struct inode *inode; |
| 1324 | struct ceph_cap *cap, *prev = NULL; |
| 1325 | struct ceph_vino vino; |
| 1326 | /* |
| 1327 | * iterate_session_caps() skips inodes that are being |
| 1328 | * deleted, we need to wait until deletions are complete. |
| 1329 | * __wait_on_freeing_inode() is designed for the job, |
| 1330 | * but it is not exported, so use lookup inode function |
| 1331 | * to access it. |
| 1332 | */ |
| 1333 | while (!list_empty(&session->s_caps)) { |
| 1334 | cap = list_entry(session->s_caps.next, |
| 1335 | struct ceph_cap, session_caps); |
| 1336 | if (cap == prev) |
| 1337 | break; |
| 1338 | prev = cap; |
| 1339 | vino = cap->ci->i_vino; |
| 1340 | spin_unlock(&session->s_cap_lock); |
| 1341 | |
Yan, Zheng | ed284c4 | 2013-09-02 15:19:53 +0800 | [diff] [blame] | 1342 | inode = ceph_find_inode(sb, vino); |
Yan, Zheng | 6f60f88 | 2013-07-24 12:22:11 +0800 | [diff] [blame] | 1343 | iput(inode); |
| 1344 | |
| 1345 | spin_lock(&session->s_cap_lock); |
| 1346 | } |
| 1347 | } |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1348 | |
| 1349 | // drop cap expires and unlock s_cap_lock |
Jeff Layton | c8a96a3 | 2017-10-19 08:53:58 -0400 | [diff] [blame] | 1350 | detach_cap_releases(session, &dispose); |
Yan, Zheng | 6f60f88 | 2013-07-24 12:22:11 +0800 | [diff] [blame] | 1351 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1352 | BUG_ON(session->s_nr_caps > 0); |
Sage Weil | 6c99f25 | 2010-05-10 16:12:25 -0700 | [diff] [blame] | 1353 | BUG_ON(!list_empty(&session->s_cap_flushing)); |
Jeff Layton | c8a96a3 | 2017-10-19 08:53:58 -0400 | [diff] [blame] | 1354 | spin_unlock(&session->s_cap_lock); |
| 1355 | dispose_cap_releases(session->s_mdsc, &dispose); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1356 | } |
| 1357 | |
Yan, Zheng | d2f8bb2 | 2018-12-10 16:35:09 +0800 | [diff] [blame] | 1358 | enum { |
| 1359 | RECONNECT, |
| 1360 | RENEWCAPS, |
| 1361 | FORCE_RO, |
| 1362 | }; |
| 1363 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1364 | /* |
| 1365 | * wake up any threads waiting on this session's caps. if the cap is |
| 1366 | * old (didn't get renewed on the client reconnect), remove it now. |
| 1367 | * |
| 1368 | * caller must hold s_mutex. |
| 1369 | */ |
| 1370 | static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap, |
| 1371 | void *arg) |
| 1372 | { |
Sage Weil | 0dc2570 | 2009-11-20 13:43:45 -0800 | [diff] [blame] | 1373 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | d2f8bb2 | 2018-12-10 16:35:09 +0800 | [diff] [blame] | 1374 | unsigned long ev = (unsigned long)arg; |
Sage Weil | 0dc2570 | 2009-11-20 13:43:45 -0800 | [diff] [blame] | 1375 | |
Yan, Zheng | d2f8bb2 | 2018-12-10 16:35:09 +0800 | [diff] [blame] | 1376 | if (ev == RECONNECT) { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1377 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 0dc2570 | 2009-11-20 13:43:45 -0800 | [diff] [blame] | 1378 | ci->i_wanted_max_size = 0; |
| 1379 | ci->i_requested_max_size = 0; |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1380 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | d2f8bb2 | 2018-12-10 16:35:09 +0800 | [diff] [blame] | 1381 | } else if (ev == RENEWCAPS) { |
| 1382 | if (cap->cap_gen < cap->session->s_cap_gen) { |
| 1383 | /* mds did not re-issue stale cap */ |
| 1384 | spin_lock(&ci->i_ceph_lock); |
| 1385 | cap->issued = cap->implemented = CEPH_CAP_PIN; |
| 1386 | /* make sure mds knows what we want */ |
| 1387 | if (__ceph_caps_file_wanted(ci) & ~cap->mds_wanted) |
| 1388 | ci->i_ceph_flags |= CEPH_I_CAP_DROPPED; |
| 1389 | spin_unlock(&ci->i_ceph_lock); |
| 1390 | } |
| 1391 | } else if (ev == FORCE_RO) { |
Sage Weil | 0dc2570 | 2009-11-20 13:43:45 -0800 | [diff] [blame] | 1392 | } |
Yan, Zheng | e536030 | 2016-05-19 19:15:19 +0800 | [diff] [blame] | 1393 | wake_up_all(&ci->i_cap_wq); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1394 | return 0; |
| 1395 | } |
| 1396 | |
Yan, Zheng | d2f8bb2 | 2018-12-10 16:35:09 +0800 | [diff] [blame] | 1397 | static void wake_up_session_caps(struct ceph_mds_session *session, int ev) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1398 | { |
| 1399 | dout("wake_up_session_caps %p mds%d\n", session, session->s_mds); |
Sage Weil | 0dc2570 | 2009-11-20 13:43:45 -0800 | [diff] [blame] | 1400 | iterate_session_caps(session, wake_up_session_cb, |
Yan, Zheng | d2f8bb2 | 2018-12-10 16:35:09 +0800 | [diff] [blame] | 1401 | (void *)(unsigned long)ev); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | /* |
| 1405 | * Send periodic message to MDS renewing all currently held caps. The |
| 1406 | * ack will reset the expiration for all caps from this session. |
| 1407 | * |
| 1408 | * caller holds s_mutex |
| 1409 | */ |
| 1410 | static int send_renew_caps(struct ceph_mds_client *mdsc, |
| 1411 | struct ceph_mds_session *session) |
| 1412 | { |
| 1413 | struct ceph_msg *msg; |
| 1414 | int state; |
| 1415 | |
| 1416 | if (time_after_eq(jiffies, session->s_cap_ttl) && |
| 1417 | time_after_eq(session->s_cap_ttl, session->s_renew_requested)) |
| 1418 | pr_info("mds%d caps stale\n", session->s_mds); |
Sage Weil | e4cb4cb | 2010-03-18 13:43:09 -0700 | [diff] [blame] | 1419 | session->s_renew_requested = jiffies; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1420 | |
| 1421 | /* do not try to renew caps until a recovering mds has reconnected |
| 1422 | * with its clients. */ |
| 1423 | state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds); |
| 1424 | if (state < CEPH_MDS_STATE_RECONNECT) { |
| 1425 | dout("send_renew_caps ignoring mds%d (%s)\n", |
| 1426 | session->s_mds, ceph_mds_state_name(state)); |
| 1427 | return 0; |
| 1428 | } |
| 1429 | |
| 1430 | dout("send_renew_caps to mds%d (%s)\n", session->s_mds, |
| 1431 | ceph_mds_state_name(state)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1432 | msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS, |
| 1433 | ++session->s_renew_seq); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 1434 | if (!msg) |
| 1435 | return -ENOMEM; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1436 | ceph_con_send(&session->s_con, msg); |
| 1437 | return 0; |
| 1438 | } |
| 1439 | |
Yan, Zheng | 186e4f7 | 2013-11-22 14:48:37 +0800 | [diff] [blame] | 1440 | static int send_flushmsg_ack(struct ceph_mds_client *mdsc, |
| 1441 | struct ceph_mds_session *session, u64 seq) |
| 1442 | { |
| 1443 | struct ceph_msg *msg; |
| 1444 | |
| 1445 | dout("send_flushmsg_ack to mds%d (%s)s seq %lld\n", |
John Spray | a687eca | 2014-09-19 13:51:08 +0100 | [diff] [blame] | 1446 | session->s_mds, ceph_session_state_name(session->s_state), seq); |
Yan, Zheng | 186e4f7 | 2013-11-22 14:48:37 +0800 | [diff] [blame] | 1447 | msg = create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq); |
| 1448 | if (!msg) |
| 1449 | return -ENOMEM; |
| 1450 | ceph_con_send(&session->s_con, msg); |
| 1451 | return 0; |
| 1452 | } |
| 1453 | |
| 1454 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1455 | /* |
| 1456 | * Note new cap ttl, and any transition from stale -> not stale (fresh?). |
Sage Weil | 0dc2570 | 2009-11-20 13:43:45 -0800 | [diff] [blame] | 1457 | * |
| 1458 | * Called under session->s_mutex |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1459 | */ |
| 1460 | static void renewed_caps(struct ceph_mds_client *mdsc, |
| 1461 | struct ceph_mds_session *session, int is_renew) |
| 1462 | { |
| 1463 | int was_stale; |
| 1464 | int wake = 0; |
| 1465 | |
| 1466 | spin_lock(&session->s_cap_lock); |
Alex Elder | 1ce208a | 2012-01-12 17:48:11 -0800 | [diff] [blame] | 1467 | was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1468 | |
| 1469 | session->s_cap_ttl = session->s_renew_requested + |
| 1470 | mdsc->mdsmap->m_session_timeout*HZ; |
| 1471 | |
| 1472 | if (was_stale) { |
| 1473 | if (time_before(jiffies, session->s_cap_ttl)) { |
| 1474 | pr_info("mds%d caps renewed\n", session->s_mds); |
| 1475 | wake = 1; |
| 1476 | } else { |
| 1477 | pr_info("mds%d caps still stale\n", session->s_mds); |
| 1478 | } |
| 1479 | } |
| 1480 | dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n", |
| 1481 | session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh", |
| 1482 | time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh"); |
| 1483 | spin_unlock(&session->s_cap_lock); |
| 1484 | |
| 1485 | if (wake) |
Yan, Zheng | d2f8bb2 | 2018-12-10 16:35:09 +0800 | [diff] [blame] | 1486 | wake_up_session_caps(session, RENEWCAPS); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | /* |
| 1490 | * send a session close request |
| 1491 | */ |
| 1492 | static int request_close_session(struct ceph_mds_client *mdsc, |
| 1493 | struct ceph_mds_session *session) |
| 1494 | { |
| 1495 | struct ceph_msg *msg; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1496 | |
| 1497 | dout("request_close_session mds%d state %s seq %lld\n", |
John Spray | a687eca | 2014-09-19 13:51:08 +0100 | [diff] [blame] | 1498 | session->s_mds, ceph_session_state_name(session->s_state), |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1499 | session->s_seq); |
| 1500 | msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 1501 | if (!msg) |
| 1502 | return -ENOMEM; |
| 1503 | ceph_con_send(&session->s_con, msg); |
Yan, Zheng | fcff415 | 2016-09-14 16:39:51 +0800 | [diff] [blame] | 1504 | return 1; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | /* |
| 1508 | * Called with s_mutex held. |
| 1509 | */ |
| 1510 | static int __close_session(struct ceph_mds_client *mdsc, |
| 1511 | struct ceph_mds_session *session) |
| 1512 | { |
| 1513 | if (session->s_state >= CEPH_MDS_SESSION_CLOSING) |
| 1514 | return 0; |
| 1515 | session->s_state = CEPH_MDS_SESSION_CLOSING; |
| 1516 | return request_close_session(mdsc, session); |
| 1517 | } |
| 1518 | |
Yan, Zheng | 040d786 | 2017-11-30 11:59:22 +0800 | [diff] [blame] | 1519 | static bool drop_negative_children(struct dentry *dentry) |
| 1520 | { |
| 1521 | struct dentry *child; |
| 1522 | bool all_negative = true; |
| 1523 | |
| 1524 | if (!d_is_dir(dentry)) |
| 1525 | goto out; |
| 1526 | |
| 1527 | spin_lock(&dentry->d_lock); |
| 1528 | list_for_each_entry(child, &dentry->d_subdirs, d_child) { |
| 1529 | if (d_really_is_positive(child)) { |
| 1530 | all_negative = false; |
| 1531 | break; |
| 1532 | } |
| 1533 | } |
| 1534 | spin_unlock(&dentry->d_lock); |
| 1535 | |
| 1536 | if (all_negative) |
| 1537 | shrink_dcache_parent(dentry); |
| 1538 | out: |
| 1539 | return all_negative; |
| 1540 | } |
| 1541 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1542 | /* |
| 1543 | * Trim old(er) caps. |
| 1544 | * |
| 1545 | * Because we can't cache an inode without one or more caps, we do |
| 1546 | * this indirectly: if a cap is unused, we prune its aliases, at which |
| 1547 | * point the inode will hopefully get dropped to. |
| 1548 | * |
| 1549 | * Yes, this is a bit sloppy. Our only real goal here is to respond to |
| 1550 | * memory pressure from the MDS, though, so it needn't be perfect. |
| 1551 | */ |
| 1552 | static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg) |
| 1553 | { |
| 1554 | struct ceph_mds_session *session = arg; |
| 1555 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | 979abfd | 2013-11-22 13:56:24 +0800 | [diff] [blame] | 1556 | int used, wanted, oissued, mine; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1557 | |
| 1558 | if (session->s_trim_caps <= 0) |
| 1559 | return -1; |
| 1560 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1561 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1562 | mine = cap->issued | cap->implemented; |
| 1563 | used = __ceph_caps_used(ci); |
Yan, Zheng | 979abfd | 2013-11-22 13:56:24 +0800 | [diff] [blame] | 1564 | wanted = __ceph_caps_file_wanted(ci); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1565 | oissued = __ceph_caps_issued_other(ci, cap); |
| 1566 | |
Yan, Zheng | 979abfd | 2013-11-22 13:56:24 +0800 | [diff] [blame] | 1567 | dout("trim_caps_cb %p cap %p mine %s oissued %s used %s wanted %s\n", |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1568 | inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued), |
Yan, Zheng | 979abfd | 2013-11-22 13:56:24 +0800 | [diff] [blame] | 1569 | ceph_cap_string(used), ceph_cap_string(wanted)); |
| 1570 | if (cap == ci->i_auth_cap) { |
Yan, Zheng | 622f3e2 | 2015-05-07 10:59:47 +0800 | [diff] [blame] | 1571 | if (ci->i_dirty_caps || ci->i_flushing_caps || |
| 1572 | !list_empty(&ci->i_cap_snaps)) |
Yan, Zheng | 979abfd | 2013-11-22 13:56:24 +0800 | [diff] [blame] | 1573 | goto out; |
| 1574 | if ((used | wanted) & CEPH_CAP_ANY_WR) |
| 1575 | goto out; |
Yan, Zheng | 89aa593 | 2017-09-08 15:23:18 +0800 | [diff] [blame] | 1576 | /* Note: it's possible that i_filelock_ref becomes non-zero |
| 1577 | * after dropping auth caps. It doesn't hurt because reply |
| 1578 | * of lock mds request will re-add auth caps. */ |
| 1579 | if (atomic_read(&ci->i_filelock_ref) > 0) |
| 1580 | goto out; |
Yan, Zheng | 979abfd | 2013-11-22 13:56:24 +0800 | [diff] [blame] | 1581 | } |
Yan, Zheng | 5e804ac | 2015-10-26 16:08:43 +0800 | [diff] [blame] | 1582 | /* The inode has cached pages, but it's no longer used. |
| 1583 | * we can safely drop it */ |
| 1584 | if (wanted == 0 && used == CEPH_CAP_FILE_CACHE && |
| 1585 | !(oissued & CEPH_CAP_FILE_CACHE)) { |
| 1586 | used = 0; |
| 1587 | oissued = 0; |
| 1588 | } |
Yan, Zheng | 979abfd | 2013-11-22 13:56:24 +0800 | [diff] [blame] | 1589 | if ((used | wanted) & ~oissued & mine) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1590 | goto out; /* we need these caps */ |
| 1591 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1592 | if (oissued) { |
| 1593 | /* we aren't the only cap.. just remove us */ |
Yan, Zheng | a096b09 | 2013-09-22 10:15:58 +0800 | [diff] [blame] | 1594 | __ceph_remove_cap(cap, true); |
Yan, Zheng | 040d786 | 2017-11-30 11:59:22 +0800 | [diff] [blame] | 1595 | session->s_trim_caps--; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1596 | } else { |
Yan, Zheng | 040d786 | 2017-11-30 11:59:22 +0800 | [diff] [blame] | 1597 | struct dentry *dentry; |
Yan, Zheng | 5e804ac | 2015-10-26 16:08:43 +0800 | [diff] [blame] | 1598 | /* try dropping referring dentries */ |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1599 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | 040d786 | 2017-11-30 11:59:22 +0800 | [diff] [blame] | 1600 | dentry = d_find_any_alias(inode); |
| 1601 | if (dentry && drop_negative_children(dentry)) { |
| 1602 | int count; |
| 1603 | dput(dentry); |
| 1604 | d_prune_aliases(inode); |
| 1605 | count = atomic_read(&inode->i_count); |
| 1606 | if (count == 1) |
| 1607 | session->s_trim_caps--; |
| 1608 | dout("trim_caps_cb %p cap %p pruned, count now %d\n", |
| 1609 | inode, cap, count); |
| 1610 | } else { |
| 1611 | dput(dentry); |
| 1612 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1613 | return 0; |
| 1614 | } |
| 1615 | |
| 1616 | out: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1617 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1618 | return 0; |
| 1619 | } |
| 1620 | |
| 1621 | /* |
| 1622 | * Trim session cap count down to some max number. |
| 1623 | */ |
Zhi Zhang | e30ee58 | 2018-01-24 21:24:33 +0800 | [diff] [blame] | 1624 | int ceph_trim_caps(struct ceph_mds_client *mdsc, |
| 1625 | struct ceph_mds_session *session, |
| 1626 | int max_caps) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1627 | { |
| 1628 | int trim_caps = session->s_nr_caps - max_caps; |
| 1629 | |
| 1630 | dout("trim_caps mds%d start: %d / %d, trim %d\n", |
| 1631 | session->s_mds, session->s_nr_caps, max_caps, trim_caps); |
| 1632 | if (trim_caps > 0) { |
| 1633 | session->s_trim_caps = trim_caps; |
| 1634 | iterate_session_caps(session, trim_caps_cb, session); |
| 1635 | dout("trim_caps mds%d done: %d / %d, trimmed %d\n", |
| 1636 | session->s_mds, session->s_nr_caps, max_caps, |
| 1637 | trim_caps - session->s_trim_caps); |
Sage Weil | 5dacf09 | 2009-12-21 20:40:34 -0800 | [diff] [blame] | 1638 | session->s_trim_caps = 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1639 | } |
Yan, Zheng | a56371d | 2014-04-01 20:34:56 +0800 | [diff] [blame] | 1640 | |
Yan, Zheng | a56371d | 2014-04-01 20:34:56 +0800 | [diff] [blame] | 1641 | ceph_send_cap_releases(mdsc, session); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1642 | return 0; |
| 1643 | } |
| 1644 | |
Yan, Zheng | 8310b08 | 2015-06-09 17:20:12 +0800 | [diff] [blame] | 1645 | static int check_caps_flush(struct ceph_mds_client *mdsc, |
| 1646 | u64 want_flush_tid) |
| 1647 | { |
Yan, Zheng | 8310b08 | 2015-06-09 17:20:12 +0800 | [diff] [blame] | 1648 | int ret = 1; |
| 1649 | |
| 1650 | spin_lock(&mdsc->cap_dirty_lock); |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 1651 | if (!list_empty(&mdsc->cap_flush_list)) { |
| 1652 | struct ceph_cap_flush *cf = |
| 1653 | list_first_entry(&mdsc->cap_flush_list, |
| 1654 | struct ceph_cap_flush, g_list); |
| 1655 | if (cf->tid <= want_flush_tid) { |
| 1656 | dout("check_caps_flush still flushing tid " |
| 1657 | "%llu <= %llu\n", cf->tid, want_flush_tid); |
| 1658 | ret = 0; |
| 1659 | } |
Yan, Zheng | 8310b08 | 2015-06-09 17:20:12 +0800 | [diff] [blame] | 1660 | } |
| 1661 | spin_unlock(&mdsc->cap_dirty_lock); |
| 1662 | return ret; |
Yan, Zheng | d3383a8 | 2015-01-08 21:30:12 +0800 | [diff] [blame] | 1663 | } |
| 1664 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1665 | /* |
| 1666 | * flush all dirty inode data to disk. |
| 1667 | * |
Yan, Zheng | 8310b08 | 2015-06-09 17:20:12 +0800 | [diff] [blame] | 1668 | * returns true if we've flushed through want_flush_tid |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1669 | */ |
Yan, Zheng | affbc19 | 2015-05-05 21:22:13 +0800 | [diff] [blame] | 1670 | static void wait_caps_flush(struct ceph_mds_client *mdsc, |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 1671 | u64 want_flush_tid) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1672 | { |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 1673 | dout("check_caps_flush want %llu\n", want_flush_tid); |
Yan, Zheng | 8310b08 | 2015-06-09 17:20:12 +0800 | [diff] [blame] | 1674 | |
| 1675 | wait_event(mdsc->cap_flushing_wq, |
| 1676 | check_caps_flush(mdsc, want_flush_tid)); |
| 1677 | |
| 1678 | dout("check_caps_flush ok, flushed thru %llu\n", want_flush_tid); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1679 | } |
| 1680 | |
| 1681 | /* |
| 1682 | * called under s_mutex |
| 1683 | */ |
Sage Weil | 3d7ded4 | 2010-06-09 16:47:10 -0700 | [diff] [blame] | 1684 | void ceph_send_cap_releases(struct ceph_mds_client *mdsc, |
| 1685 | struct ceph_mds_session *session) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1686 | { |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1687 | struct ceph_msg *msg = NULL; |
| 1688 | struct ceph_mds_cap_release *head; |
| 1689 | struct ceph_mds_cap_item *item; |
Jeff Layton | 92475f0 | 2017-04-13 11:07:04 -0400 | [diff] [blame] | 1690 | struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc; |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1691 | struct ceph_cap *cap; |
| 1692 | LIST_HEAD(tmp_list); |
| 1693 | int num_cap_releases; |
Jeff Layton | 92475f0 | 2017-04-13 11:07:04 -0400 | [diff] [blame] | 1694 | __le32 barrier, *cap_barrier; |
| 1695 | |
| 1696 | down_read(&osdc->lock); |
| 1697 | barrier = cpu_to_le32(osdc->epoch_barrier); |
| 1698 | up_read(&osdc->lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1699 | |
Sage Weil | 0f8605f | 2010-05-05 15:51:35 -0700 | [diff] [blame] | 1700 | spin_lock(&session->s_cap_lock); |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1701 | again: |
| 1702 | list_splice_init(&session->s_cap_releases, &tmp_list); |
| 1703 | num_cap_releases = session->s_num_cap_releases; |
| 1704 | session->s_num_cap_releases = 0; |
| 1705 | spin_unlock(&session->s_cap_lock); |
| 1706 | |
| 1707 | while (!list_empty(&tmp_list)) { |
| 1708 | if (!msg) { |
| 1709 | msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1710 | PAGE_SIZE, GFP_NOFS, false); |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1711 | if (!msg) |
| 1712 | goto out_err; |
| 1713 | head = msg->front.iov_base; |
| 1714 | head->num = cpu_to_le32(0); |
| 1715 | msg->front.iov_len = sizeof(*head); |
Jeff Layton | 92475f0 | 2017-04-13 11:07:04 -0400 | [diff] [blame] | 1716 | |
| 1717 | msg->hdr.version = cpu_to_le16(2); |
| 1718 | msg->hdr.compat_version = cpu_to_le16(1); |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1719 | } |
Jeff Layton | 92475f0 | 2017-04-13 11:07:04 -0400 | [diff] [blame] | 1720 | |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1721 | cap = list_first_entry(&tmp_list, struct ceph_cap, |
| 1722 | session_caps); |
| 1723 | list_del(&cap->session_caps); |
| 1724 | num_cap_releases--; |
| 1725 | |
| 1726 | head = msg->front.iov_base; |
| 1727 | le32_add_cpu(&head->num, 1); |
| 1728 | item = msg->front.iov_base + msg->front.iov_len; |
| 1729 | item->ino = cpu_to_le64(cap->cap_ino); |
| 1730 | item->cap_id = cpu_to_le64(cap->cap_id); |
| 1731 | item->migrate_seq = cpu_to_le32(cap->mseq); |
| 1732 | item->seq = cpu_to_le32(cap->issue_seq); |
| 1733 | msg->front.iov_len += sizeof(*item); |
| 1734 | |
| 1735 | ceph_put_cap(mdsc, cap); |
| 1736 | |
| 1737 | if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) { |
Jeff Layton | 92475f0 | 2017-04-13 11:07:04 -0400 | [diff] [blame] | 1738 | // Append cap_barrier field |
| 1739 | cap_barrier = msg->front.iov_base + msg->front.iov_len; |
| 1740 | *cap_barrier = barrier; |
| 1741 | msg->front.iov_len += sizeof(*cap_barrier); |
| 1742 | |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1743 | msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); |
| 1744 | dout("send_cap_releases mds%d %p\n", session->s_mds, msg); |
| 1745 | ceph_con_send(&session->s_con, msg); |
| 1746 | msg = NULL; |
| 1747 | } |
| 1748 | } |
| 1749 | |
| 1750 | BUG_ON(num_cap_releases != 0); |
| 1751 | |
| 1752 | spin_lock(&session->s_cap_lock); |
| 1753 | if (!list_empty(&session->s_cap_releases)) |
| 1754 | goto again; |
| 1755 | spin_unlock(&session->s_cap_lock); |
| 1756 | |
| 1757 | if (msg) { |
Jeff Layton | 92475f0 | 2017-04-13 11:07:04 -0400 | [diff] [blame] | 1758 | // Append cap_barrier field |
| 1759 | cap_barrier = msg->front.iov_base + msg->front.iov_len; |
| 1760 | *cap_barrier = barrier; |
| 1761 | msg->front.iov_len += sizeof(*cap_barrier); |
| 1762 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1763 | msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); |
| 1764 | dout("send_cap_releases mds%d %p\n", session->s_mds, msg); |
| 1765 | ceph_con_send(&session->s_con, msg); |
| 1766 | } |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1767 | return; |
| 1768 | out_err: |
| 1769 | pr_err("send_cap_releases mds%d, failed to allocate message\n", |
| 1770 | session->s_mds); |
| 1771 | spin_lock(&session->s_cap_lock); |
| 1772 | list_splice(&tmp_list, &session->s_cap_releases); |
| 1773 | session->s_num_cap_releases += num_cap_releases; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1774 | spin_unlock(&session->s_cap_lock); |
| 1775 | } |
| 1776 | |
| 1777 | /* |
| 1778 | * requests |
| 1779 | */ |
| 1780 | |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 1781 | int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req, |
| 1782 | struct inode *dir) |
| 1783 | { |
| 1784 | struct ceph_inode_info *ci = ceph_inode(dir); |
| 1785 | struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; |
| 1786 | struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options; |
Yan, Zheng | 2a5beea | 2016-04-28 09:37:39 +0800 | [diff] [blame] | 1787 | size_t size = sizeof(struct ceph_mds_reply_dir_entry); |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 1788 | int order, num_entries; |
| 1789 | |
| 1790 | spin_lock(&ci->i_ceph_lock); |
| 1791 | num_entries = ci->i_files + ci->i_subdirs; |
| 1792 | spin_unlock(&ci->i_ceph_lock); |
| 1793 | num_entries = max(num_entries, 1); |
| 1794 | num_entries = min(num_entries, opt->max_readdir); |
| 1795 | |
| 1796 | order = get_order(size * num_entries); |
| 1797 | while (order >= 0) { |
Yan, Zheng | 2a5beea | 2016-04-28 09:37:39 +0800 | [diff] [blame] | 1798 | rinfo->dir_entries = (void*)__get_free_pages(GFP_KERNEL | |
| 1799 | __GFP_NOWARN, |
| 1800 | order); |
| 1801 | if (rinfo->dir_entries) |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 1802 | break; |
| 1803 | order--; |
| 1804 | } |
Yan, Zheng | 2a5beea | 2016-04-28 09:37:39 +0800 | [diff] [blame] | 1805 | if (!rinfo->dir_entries) |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 1806 | return -ENOMEM; |
| 1807 | |
| 1808 | num_entries = (PAGE_SIZE << order) / size; |
| 1809 | num_entries = min(num_entries, opt->max_readdir); |
| 1810 | |
| 1811 | rinfo->dir_buf_size = PAGE_SIZE << order; |
| 1812 | req->r_num_caps = num_entries + 1; |
| 1813 | req->r_args.readdir.max_entries = cpu_to_le32(num_entries); |
| 1814 | req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes); |
| 1815 | return 0; |
| 1816 | } |
| 1817 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1818 | /* |
| 1819 | * Create an mds request. |
| 1820 | */ |
| 1821 | struct ceph_mds_request * |
| 1822 | ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode) |
| 1823 | { |
| 1824 | struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS); |
Arnd Bergmann | 63ecae7 | 2018-07-13 22:18:35 +0200 | [diff] [blame] | 1825 | struct timespec64 ts; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1826 | |
| 1827 | if (!req) |
| 1828 | return ERR_PTR(-ENOMEM); |
| 1829 | |
Sage Weil | b455639 | 2010-05-13 12:01:13 -0700 | [diff] [blame] | 1830 | mutex_init(&req->r_fill_mutex); |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 1831 | req->r_mdsc = mdsc; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1832 | req->r_started = jiffies; |
| 1833 | req->r_resend_mds = -1; |
| 1834 | INIT_LIST_HEAD(&req->r_unsafe_dir_item); |
Yan, Zheng | 68cd5b4 | 2015-10-27 18:36:06 +0800 | [diff] [blame] | 1835 | INIT_LIST_HEAD(&req->r_unsafe_target_item); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1836 | req->r_fmode = -1; |
Sage Weil | 153c8e6 | 2009-12-07 12:31:09 -0800 | [diff] [blame] | 1837 | kref_init(&req->r_kref); |
Ilya Dryomov | fcd00b6 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 1838 | RB_CLEAR_NODE(&req->r_node); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1839 | INIT_LIST_HEAD(&req->r_wait); |
| 1840 | init_completion(&req->r_completion); |
| 1841 | init_completion(&req->r_safe_completion); |
| 1842 | INIT_LIST_HEAD(&req->r_unsafe_item); |
| 1843 | |
Arnd Bergmann | 63ecae7 | 2018-07-13 22:18:35 +0200 | [diff] [blame] | 1844 | ktime_get_coarse_real_ts64(&ts); |
Arnd Bergmann | 0ed1e90 | 2018-07-13 22:18:38 +0200 | [diff] [blame] | 1845 | req->r_stamp = timespec64_trunc(ts, mdsc->fsc->sb->s_time_gran); |
Sage Weil | b8e6906 | 2014-05-21 17:41:08 -0700 | [diff] [blame] | 1846 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1847 | req->r_op = op; |
| 1848 | req->r_direct_mode = mode; |
| 1849 | return req; |
| 1850 | } |
| 1851 | |
| 1852 | /* |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 1853 | * return oldest (lowest) request, tid in request tree, 0 if none. |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1854 | * |
| 1855 | * called under mdsc->mutex. |
| 1856 | */ |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 1857 | static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc) |
| 1858 | { |
| 1859 | if (RB_EMPTY_ROOT(&mdsc->request_tree)) |
| 1860 | return NULL; |
| 1861 | return rb_entry(rb_first(&mdsc->request_tree), |
| 1862 | struct ceph_mds_request, r_node); |
| 1863 | } |
| 1864 | |
Yan, Zheng | e8a7b8b | 2015-05-19 18:54:40 +0800 | [diff] [blame] | 1865 | static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1866 | { |
Yan, Zheng | e8a7b8b | 2015-05-19 18:54:40 +0800 | [diff] [blame] | 1867 | return mdsc->oldest_tid; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1868 | } |
| 1869 | |
| 1870 | /* |
| 1871 | * Build a dentry's path. Allocate on heap; caller must kfree. Based |
| 1872 | * on build_path_from_dentry in fs/cifs/dir.c. |
| 1873 | * |
| 1874 | * If @stop_on_nosnap, generate path relative to the first non-snapped |
| 1875 | * inode. |
| 1876 | * |
| 1877 | * Encode hidden .snap dirs as a double /, i.e. |
| 1878 | * foo/.snap/bar -> foo//bar |
| 1879 | */ |
| 1880 | char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base, |
| 1881 | int stop_on_nosnap) |
| 1882 | { |
| 1883 | struct dentry *temp; |
| 1884 | char *path; |
| 1885 | int len, pos; |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 1886 | unsigned seq; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1887 | |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 1888 | if (!dentry) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1889 | return ERR_PTR(-EINVAL); |
| 1890 | |
| 1891 | retry: |
| 1892 | len = 0; |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 1893 | seq = read_seqbegin(&rename_lock); |
| 1894 | rcu_read_lock(); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1895 | for (temp = dentry; !IS_ROOT(temp);) { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1896 | struct inode *inode = d_inode(temp); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1897 | if (inode && ceph_snap(inode) == CEPH_SNAPDIR) |
| 1898 | len++; /* slash only */ |
| 1899 | else if (stop_on_nosnap && inode && |
| 1900 | ceph_snap(inode) == CEPH_NOSNAP) |
| 1901 | break; |
| 1902 | else |
| 1903 | len += 1 + temp->d_name.len; |
| 1904 | temp = temp->d_parent; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1905 | } |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 1906 | rcu_read_unlock(); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1907 | if (len) |
| 1908 | len--; /* no leading '/' */ |
| 1909 | |
| 1910 | path = kmalloc(len+1, GFP_NOFS); |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 1911 | if (!path) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1912 | return ERR_PTR(-ENOMEM); |
| 1913 | pos = len; |
| 1914 | path[pos] = 0; /* trailing null */ |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 1915 | rcu_read_lock(); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1916 | for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) { |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 1917 | struct inode *inode; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1918 | |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 1919 | spin_lock(&temp->d_lock); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1920 | inode = d_inode(temp); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1921 | if (inode && ceph_snap(inode) == CEPH_SNAPDIR) { |
Sage Weil | 104648a | 2010-03-18 10:14:30 -0700 | [diff] [blame] | 1922 | dout("build_path path+%d: %p SNAPDIR\n", |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1923 | pos, temp); |
| 1924 | } else if (stop_on_nosnap && inode && |
| 1925 | ceph_snap(inode) == CEPH_NOSNAP) { |
Yehuda Sadeh | 9d5a09e | 2011-12-13 09:57:44 -0800 | [diff] [blame] | 1926 | spin_unlock(&temp->d_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1927 | break; |
| 1928 | } else { |
| 1929 | pos -= temp->d_name.len; |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 1930 | if (pos < 0) { |
| 1931 | spin_unlock(&temp->d_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1932 | break; |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 1933 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1934 | strncpy(path + pos, temp->d_name.name, |
| 1935 | temp->d_name.len); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1936 | } |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 1937 | spin_unlock(&temp->d_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1938 | if (pos) |
| 1939 | path[--pos] = '/'; |
| 1940 | temp = temp->d_parent; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1941 | } |
Al Viro | 1b71fe2 | 2011-07-16 23:43:58 -0400 | [diff] [blame] | 1942 | rcu_read_unlock(); |
| 1943 | if (pos != 0 || read_seqretry(&rename_lock, seq)) { |
Sage Weil | 104648a | 2010-03-18 10:14:30 -0700 | [diff] [blame] | 1944 | pr_err("build_path did not end path lookup where " |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1945 | "expected, namelen is %d, pos is %d\n", len, pos); |
| 1946 | /* presumably this is only possible if racing with a |
| 1947 | rename of one of the parent directories (we can not |
| 1948 | lock the dentries above us to prevent this, but |
| 1949 | retrying should be harmless) */ |
| 1950 | kfree(path); |
| 1951 | goto retry; |
| 1952 | } |
| 1953 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1954 | *base = ceph_ino(d_inode(temp)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1955 | *plen = len; |
Sage Weil | 104648a | 2010-03-18 10:14:30 -0700 | [diff] [blame] | 1956 | dout("build_path on %p %d built %llx '%.*s'\n", |
Al Viro | 84d08fa | 2013-07-05 18:59:33 +0400 | [diff] [blame] | 1957 | dentry, d_count(dentry), *base, len, path); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1958 | return path; |
| 1959 | } |
| 1960 | |
Jeff Layton | fd36a71 | 2016-12-15 08:37:58 -0500 | [diff] [blame] | 1961 | static int build_dentry_path(struct dentry *dentry, struct inode *dir, |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1962 | const char **ppath, int *ppathlen, u64 *pino, |
| 1963 | int *pfreepath) |
| 1964 | { |
| 1965 | char *path; |
| 1966 | |
Jeff Layton | c6b0b65 | 2016-12-15 08:37:57 -0500 | [diff] [blame] | 1967 | rcu_read_lock(); |
Jeff Layton | fd36a71 | 2016-12-15 08:37:58 -0500 | [diff] [blame] | 1968 | if (!dir) |
| 1969 | dir = d_inode_rcu(dentry->d_parent); |
Jeff Layton | c6b0b65 | 2016-12-15 08:37:57 -0500 | [diff] [blame] | 1970 | if (dir && ceph_snap(dir) == CEPH_NOSNAP) { |
| 1971 | *pino = ceph_ino(dir); |
| 1972 | rcu_read_unlock(); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1973 | *ppath = dentry->d_name.name; |
| 1974 | *ppathlen = dentry->d_name.len; |
| 1975 | return 0; |
| 1976 | } |
Jeff Layton | c6b0b65 | 2016-12-15 08:37:57 -0500 | [diff] [blame] | 1977 | rcu_read_unlock(); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 1978 | path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1); |
| 1979 | if (IS_ERR(path)) |
| 1980 | return PTR_ERR(path); |
| 1981 | *ppath = path; |
| 1982 | *pfreepath = 1; |
| 1983 | return 0; |
| 1984 | } |
| 1985 | |
| 1986 | static int build_inode_path(struct inode *inode, |
| 1987 | const char **ppath, int *ppathlen, u64 *pino, |
| 1988 | int *pfreepath) |
| 1989 | { |
| 1990 | struct dentry *dentry; |
| 1991 | char *path; |
| 1992 | |
| 1993 | if (ceph_snap(inode) == CEPH_NOSNAP) { |
| 1994 | *pino = ceph_ino(inode); |
| 1995 | *ppathlen = 0; |
| 1996 | return 0; |
| 1997 | } |
| 1998 | dentry = d_find_alias(inode); |
| 1999 | path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1); |
| 2000 | dput(dentry); |
| 2001 | if (IS_ERR(path)) |
| 2002 | return PTR_ERR(path); |
| 2003 | *ppath = path; |
| 2004 | *pfreepath = 1; |
| 2005 | return 0; |
| 2006 | } |
| 2007 | |
| 2008 | /* |
| 2009 | * request arguments may be specified via an inode *, a dentry *, or |
| 2010 | * an explicit ino+path. |
| 2011 | */ |
| 2012 | static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry, |
Jeff Layton | fd36a71 | 2016-12-15 08:37:58 -0500 | [diff] [blame] | 2013 | struct inode *rdiri, const char *rpath, |
| 2014 | u64 rino, const char **ppath, int *pathlen, |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2015 | u64 *ino, int *freepath) |
| 2016 | { |
| 2017 | int r = 0; |
| 2018 | |
| 2019 | if (rinode) { |
| 2020 | r = build_inode_path(rinode, ppath, pathlen, ino, freepath); |
| 2021 | dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode), |
| 2022 | ceph_snap(rinode)); |
| 2023 | } else if (rdentry) { |
Jeff Layton | fd36a71 | 2016-12-15 08:37:58 -0500 | [diff] [blame] | 2024 | r = build_dentry_path(rdentry, rdiri, ppath, pathlen, ino, |
| 2025 | freepath); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2026 | dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen, |
| 2027 | *ppath); |
Sage Weil | 795858d | 2011-08-15 13:02:37 -0700 | [diff] [blame] | 2028 | } else if (rpath || rino) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2029 | *ino = rino; |
| 2030 | *ppath = rpath; |
David Zafman | b000056 | 2012-10-25 10:23:46 -0700 | [diff] [blame] | 2031 | *pathlen = rpath ? strlen(rpath) : 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2032 | dout(" path %.*s\n", *pathlen, rpath); |
| 2033 | } |
| 2034 | |
| 2035 | return r; |
| 2036 | } |
| 2037 | |
| 2038 | /* |
| 2039 | * called under mdsc->mutex |
| 2040 | */ |
| 2041 | static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc, |
| 2042 | struct ceph_mds_request *req, |
Yan, Zheng | 6e6f092 | 2015-02-27 08:54:08 +0800 | [diff] [blame] | 2043 | int mds, bool drop_cap_releases) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2044 | { |
| 2045 | struct ceph_msg *msg; |
| 2046 | struct ceph_mds_request_head *head; |
| 2047 | const char *path1 = NULL; |
| 2048 | const char *path2 = NULL; |
| 2049 | u64 ino1 = 0, ino2 = 0; |
| 2050 | int pathlen1 = 0, pathlen2 = 0; |
| 2051 | int freepath1 = 0, freepath2 = 0; |
| 2052 | int len; |
| 2053 | u16 releases; |
| 2054 | void *p, *end; |
| 2055 | int ret; |
| 2056 | |
| 2057 | ret = set_request_path_attr(req->r_inode, req->r_dentry, |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 2058 | req->r_parent, req->r_path1, req->r_ino1.ino, |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2059 | &path1, &pathlen1, &ino1, &freepath1); |
| 2060 | if (ret < 0) { |
| 2061 | msg = ERR_PTR(ret); |
| 2062 | goto out; |
| 2063 | } |
| 2064 | |
| 2065 | ret = set_request_path_attr(NULL, req->r_old_dentry, |
Jeff Layton | fd36a71 | 2016-12-15 08:37:58 -0500 | [diff] [blame] | 2066 | req->r_old_dentry_dir, |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2067 | req->r_path2, req->r_ino2.ino, |
| 2068 | &path2, &pathlen2, &ino2, &freepath2); |
| 2069 | if (ret < 0) { |
| 2070 | msg = ERR_PTR(ret); |
| 2071 | goto out_free1; |
| 2072 | } |
| 2073 | |
| 2074 | len = sizeof(*head) + |
Sage Weil | b8e6906 | 2014-05-21 17:41:08 -0700 | [diff] [blame] | 2075 | pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)) + |
Arnd Bergmann | 777d738 | 2015-09-30 15:04:42 +0200 | [diff] [blame] | 2076 | sizeof(struct ceph_timespec); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2077 | |
| 2078 | /* calculate (max) length for cap releases */ |
| 2079 | len += sizeof(struct ceph_mds_request_release) * |
| 2080 | (!!req->r_inode_drop + !!req->r_dentry_drop + |
| 2081 | !!req->r_old_inode_drop + !!req->r_old_dentry_drop); |
| 2082 | if (req->r_dentry_drop) |
| 2083 | len += req->r_dentry->d_name.len; |
| 2084 | if (req->r_old_dentry_drop) |
| 2085 | len += req->r_old_dentry->d_name.len; |
| 2086 | |
Ilya Dryomov | 0d9c1ab | 2018-10-15 17:38:23 +0200 | [diff] [blame] | 2087 | msg = ceph_msg_new2(CEPH_MSG_CLIENT_REQUEST, len, 1, GFP_NOFS, false); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 2088 | if (!msg) { |
| 2089 | msg = ERR_PTR(-ENOMEM); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2090 | goto out_free2; |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 2091 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2092 | |
John Spray | 7cfa031 | 2014-10-30 17:15:26 +0000 | [diff] [blame] | 2093 | msg->hdr.version = cpu_to_le16(2); |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 2094 | msg->hdr.tid = cpu_to_le64(req->r_tid); |
| 2095 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2096 | head = msg->front.iov_base; |
| 2097 | p = msg->front.iov_base + sizeof(*head); |
| 2098 | end = msg->front.iov_base + msg->front.iov_len; |
| 2099 | |
| 2100 | head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch); |
| 2101 | head->op = cpu_to_le32(req->r_op); |
Eric W. Biederman | ff3d004 | 2013-01-31 04:01:53 -0800 | [diff] [blame] | 2102 | head->caller_uid = cpu_to_le32(from_kuid(&init_user_ns, req->r_uid)); |
| 2103 | head->caller_gid = cpu_to_le32(from_kgid(&init_user_ns, req->r_gid)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2104 | head->args = req->r_args; |
| 2105 | |
| 2106 | ceph_encode_filepath(&p, end, ino1, path1); |
| 2107 | ceph_encode_filepath(&p, end, ino2, path2); |
| 2108 | |
Sage Weil | e979cf5 | 2010-07-15 14:58:39 -0700 | [diff] [blame] | 2109 | /* make note of release offset, in case we need to replay */ |
| 2110 | req->r_request_release_offset = p - msg->front.iov_base; |
| 2111 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2112 | /* cap releases */ |
| 2113 | releases = 0; |
| 2114 | if (req->r_inode_drop) |
| 2115 | releases += ceph_encode_inode_release(&p, |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2116 | req->r_inode ? req->r_inode : d_inode(req->r_dentry), |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2117 | mds, req->r_inode_drop, req->r_inode_unless, 0); |
| 2118 | if (req->r_dentry_drop) |
| 2119 | releases += ceph_encode_dentry_release(&p, req->r_dentry, |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 2120 | req->r_parent, mds, req->r_dentry_drop, |
Jeff Layton | ca6c8ae | 2016-12-15 08:37:59 -0500 | [diff] [blame] | 2121 | req->r_dentry_unless); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2122 | if (req->r_old_dentry_drop) |
| 2123 | releases += ceph_encode_dentry_release(&p, req->r_old_dentry, |
Jeff Layton | ca6c8ae | 2016-12-15 08:37:59 -0500 | [diff] [blame] | 2124 | req->r_old_dentry_dir, mds, |
| 2125 | req->r_old_dentry_drop, |
| 2126 | req->r_old_dentry_unless); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2127 | if (req->r_old_inode_drop) |
| 2128 | releases += ceph_encode_inode_release(&p, |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2129 | d_inode(req->r_old_dentry), |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2130 | mds, req->r_old_inode_drop, req->r_old_inode_unless, 0); |
Yan, Zheng | 6e6f092 | 2015-02-27 08:54:08 +0800 | [diff] [blame] | 2131 | |
| 2132 | if (drop_cap_releases) { |
| 2133 | releases = 0; |
| 2134 | p = msg->front.iov_base + req->r_request_release_offset; |
| 2135 | } |
| 2136 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2137 | head->num_releases = cpu_to_le16(releases); |
| 2138 | |
Sage Weil | b8e6906 | 2014-05-21 17:41:08 -0700 | [diff] [blame] | 2139 | /* time stamp */ |
Yan, Zheng | 1f041a8 | 2015-01-13 15:20:52 +0800 | [diff] [blame] | 2140 | { |
| 2141 | struct ceph_timespec ts; |
Arnd Bergmann | 0ed1e90 | 2018-07-13 22:18:38 +0200 | [diff] [blame] | 2142 | ceph_encode_timespec64(&ts, &req->r_stamp); |
Yan, Zheng | 1f041a8 | 2015-01-13 15:20:52 +0800 | [diff] [blame] | 2143 | ceph_encode_copy(&p, &ts, sizeof(ts)); |
| 2144 | } |
Sage Weil | b8e6906 | 2014-05-21 17:41:08 -0700 | [diff] [blame] | 2145 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2146 | BUG_ON(p > end); |
| 2147 | msg->front.iov_len = p - msg->front.iov_base; |
| 2148 | msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); |
| 2149 | |
Yan, Zheng | 25e6bae | 2014-09-16 19:15:28 +0800 | [diff] [blame] | 2150 | if (req->r_pagelist) { |
| 2151 | struct ceph_pagelist *pagelist = req->r_pagelist; |
Yan, Zheng | 25e6bae | 2014-09-16 19:15:28 +0800 | [diff] [blame] | 2152 | ceph_msg_data_add_pagelist(msg, pagelist); |
| 2153 | msg->hdr.data_len = cpu_to_le32(pagelist->length); |
| 2154 | } else { |
| 2155 | msg->hdr.data_len = 0; |
Alex Elder | ebf18f4 | 2013-03-04 22:29:57 -0600 | [diff] [blame] | 2156 | } |
Alex Elder | 02afca6 | 2013-02-14 12:16:43 -0600 | [diff] [blame] | 2157 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2158 | msg->hdr.data_off = cpu_to_le16(0); |
| 2159 | |
| 2160 | out_free2: |
| 2161 | if (freepath2) |
| 2162 | kfree((char *)path2); |
| 2163 | out_free1: |
| 2164 | if (freepath1) |
| 2165 | kfree((char *)path1); |
| 2166 | out: |
| 2167 | return msg; |
| 2168 | } |
| 2169 | |
| 2170 | /* |
| 2171 | * called under mdsc->mutex if error, under no mutex if |
| 2172 | * success. |
| 2173 | */ |
| 2174 | static void complete_request(struct ceph_mds_client *mdsc, |
| 2175 | struct ceph_mds_request *req) |
| 2176 | { |
| 2177 | if (req->r_callback) |
| 2178 | req->r_callback(mdsc, req); |
| 2179 | else |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 2180 | complete_all(&req->r_completion); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2181 | } |
| 2182 | |
| 2183 | /* |
| 2184 | * called under mdsc->mutex |
| 2185 | */ |
| 2186 | static int __prepare_send_request(struct ceph_mds_client *mdsc, |
| 2187 | struct ceph_mds_request *req, |
Yan, Zheng | 6e6f092 | 2015-02-27 08:54:08 +0800 | [diff] [blame] | 2188 | int mds, bool drop_cap_releases) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2189 | { |
| 2190 | struct ceph_mds_request_head *rhead; |
| 2191 | struct ceph_msg *msg; |
| 2192 | int flags = 0; |
| 2193 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2194 | req->r_attempts++; |
Greg Farnum | e55b71f | 2010-06-22 15:58:01 -0700 | [diff] [blame] | 2195 | if (req->r_inode) { |
| 2196 | struct ceph_cap *cap = |
| 2197 | ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds); |
| 2198 | |
| 2199 | if (cap) |
| 2200 | req->r_sent_on_mseq = cap->mseq; |
| 2201 | else |
| 2202 | req->r_sent_on_mseq = -1; |
| 2203 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2204 | dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req, |
| 2205 | req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts); |
| 2206 | |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2207 | if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) { |
Yan, Zheng | c5c9a0b | 2014-07-01 16:54:34 +0800 | [diff] [blame] | 2208 | void *p; |
Sage Weil | 01a92f1 | 2010-07-15 13:24:32 -0700 | [diff] [blame] | 2209 | /* |
| 2210 | * Replay. Do not regenerate message (and rebuild |
| 2211 | * paths, etc.); just use the original message. |
| 2212 | * Rebuilding paths will break for renames because |
| 2213 | * d_move mangles the src name. |
| 2214 | */ |
| 2215 | msg = req->r_request; |
| 2216 | rhead = msg->front.iov_base; |
| 2217 | |
| 2218 | flags = le32_to_cpu(rhead->flags); |
| 2219 | flags |= CEPH_MDS_FLAG_REPLAY; |
| 2220 | rhead->flags = cpu_to_le32(flags); |
| 2221 | |
| 2222 | if (req->r_target_inode) |
| 2223 | rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode)); |
| 2224 | |
| 2225 | rhead->num_retry = req->r_attempts - 1; |
Sage Weil | e979cf5 | 2010-07-15 14:58:39 -0700 | [diff] [blame] | 2226 | |
| 2227 | /* remove cap/dentry releases from message */ |
| 2228 | rhead->num_releases = 0; |
Yan, Zheng | c5c9a0b | 2014-07-01 16:54:34 +0800 | [diff] [blame] | 2229 | |
| 2230 | /* time stamp */ |
| 2231 | p = msg->front.iov_base + req->r_request_release_offset; |
Yan, Zheng | 1f041a8 | 2015-01-13 15:20:52 +0800 | [diff] [blame] | 2232 | { |
| 2233 | struct ceph_timespec ts; |
Arnd Bergmann | 0ed1e90 | 2018-07-13 22:18:38 +0200 | [diff] [blame] | 2234 | ceph_encode_timespec64(&ts, &req->r_stamp); |
Yan, Zheng | 1f041a8 | 2015-01-13 15:20:52 +0800 | [diff] [blame] | 2235 | ceph_encode_copy(&p, &ts, sizeof(ts)); |
| 2236 | } |
Yan, Zheng | c5c9a0b | 2014-07-01 16:54:34 +0800 | [diff] [blame] | 2237 | |
| 2238 | msg->front.iov_len = p - msg->front.iov_base; |
| 2239 | msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); |
Sage Weil | 01a92f1 | 2010-07-15 13:24:32 -0700 | [diff] [blame] | 2240 | return 0; |
| 2241 | } |
| 2242 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2243 | if (req->r_request) { |
| 2244 | ceph_msg_put(req->r_request); |
| 2245 | req->r_request = NULL; |
| 2246 | } |
Yan, Zheng | 6e6f092 | 2015-02-27 08:54:08 +0800 | [diff] [blame] | 2247 | msg = create_request_message(mdsc, req, mds, drop_cap_releases); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2248 | if (IS_ERR(msg)) { |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2249 | req->r_err = PTR_ERR(msg); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 2250 | return PTR_ERR(msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2251 | } |
| 2252 | req->r_request = msg; |
| 2253 | |
| 2254 | rhead = msg->front.iov_base; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2255 | rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc)); |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2256 | if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2257 | flags |= CEPH_MDS_FLAG_REPLAY; |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 2258 | if (req->r_parent) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2259 | flags |= CEPH_MDS_FLAG_WANT_DENTRY; |
| 2260 | rhead->flags = cpu_to_le32(flags); |
| 2261 | rhead->num_fwd = req->r_num_fwd; |
| 2262 | rhead->num_retry = req->r_attempts - 1; |
Sage Weil | 01a92f1 | 2010-07-15 13:24:32 -0700 | [diff] [blame] | 2263 | rhead->ino = 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2264 | |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 2265 | dout(" r_parent = %p\n", req->r_parent); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2266 | return 0; |
| 2267 | } |
| 2268 | |
| 2269 | /* |
| 2270 | * send request, or put it on the appropriate wait list. |
| 2271 | */ |
Chengguang Xu | d554849 | 2018-07-28 16:30:48 +0800 | [diff] [blame] | 2272 | static void __do_request(struct ceph_mds_client *mdsc, |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2273 | struct ceph_mds_request *req) |
| 2274 | { |
| 2275 | struct ceph_mds_session *session = NULL; |
| 2276 | int mds = -1; |
Yan, Zheng | 48fec5d | 2015-07-01 16:27:46 +0800 | [diff] [blame] | 2277 | int err = 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2278 | |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2279 | if (req->r_err || test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) { |
| 2280 | if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) |
Yan, Zheng | eb1b8af | 2013-09-26 14:25:36 +0800 | [diff] [blame] | 2281 | __unregister_request(mdsc, req); |
Chengguang Xu | d554849 | 2018-07-28 16:30:48 +0800 | [diff] [blame] | 2282 | return; |
Yan, Zheng | eb1b8af | 2013-09-26 14:25:36 +0800 | [diff] [blame] | 2283 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2284 | |
| 2285 | if (req->r_timeout && |
| 2286 | time_after_eq(jiffies, req->r_started + req->r_timeout)) { |
| 2287 | dout("do_request timed out\n"); |
| 2288 | err = -EIO; |
| 2289 | goto finish; |
| 2290 | } |
Seraphime Kirkovski | 52953d5 | 2016-12-26 10:26:34 +0100 | [diff] [blame] | 2291 | if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) { |
Yan, Zheng | 48fec5d | 2015-07-01 16:27:46 +0800 | [diff] [blame] | 2292 | dout("do_request forced umount\n"); |
| 2293 | err = -EIO; |
| 2294 | goto finish; |
| 2295 | } |
Seraphime Kirkovski | 52953d5 | 2016-12-26 10:26:34 +0100 | [diff] [blame] | 2296 | if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_MOUNTING) { |
Yan, Zheng | e9e427f | 2016-11-10 16:02:06 +0800 | [diff] [blame] | 2297 | if (mdsc->mdsmap_err) { |
| 2298 | err = mdsc->mdsmap_err; |
| 2299 | dout("do_request mdsmap err %d\n", err); |
| 2300 | goto finish; |
| 2301 | } |
Yan, Zheng | cc8e834 | 2017-01-04 16:21:58 +0800 | [diff] [blame] | 2302 | if (mdsc->mdsmap->m_epoch == 0) { |
| 2303 | dout("do_request no mdsmap, waiting for map\n"); |
| 2304 | list_add(&req->r_wait, &mdsc->waiting_for_map); |
Chengguang Xu | d554849 | 2018-07-28 16:30:48 +0800 | [diff] [blame] | 2305 | return; |
Yan, Zheng | cc8e834 | 2017-01-04 16:21:58 +0800 | [diff] [blame] | 2306 | } |
Yan, Zheng | e9e427f | 2016-11-10 16:02:06 +0800 | [diff] [blame] | 2307 | if (!(mdsc->fsc->mount_options->flags & |
| 2308 | CEPH_MOUNT_OPT_MOUNTWAIT) && |
| 2309 | !ceph_mdsmap_is_cluster_available(mdsc->mdsmap)) { |
| 2310 | err = -ENOENT; |
| 2311 | pr_info("probably no mds server is up\n"); |
| 2312 | goto finish; |
| 2313 | } |
| 2314 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2315 | |
Sage Weil | dc69e2e | 2010-11-02 13:49:00 -0700 | [diff] [blame] | 2316 | put_request_session(req); |
| 2317 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2318 | mds = __choose_mds(mdsc, req); |
| 2319 | if (mds < 0 || |
| 2320 | ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) { |
| 2321 | dout("do_request no mds or not active, waiting for map\n"); |
| 2322 | list_add(&req->r_wait, &mdsc->waiting_for_map); |
Chengguang Xu | d554849 | 2018-07-28 16:30:48 +0800 | [diff] [blame] | 2323 | return; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2324 | } |
| 2325 | |
| 2326 | /* get, open session */ |
| 2327 | session = __ceph_lookup_mds_session(mdsc, mds); |
Sage Weil | 9c423956 | 2010-03-20 20:43:28 -0700 | [diff] [blame] | 2328 | if (!session) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2329 | session = register_session(mdsc, mds); |
Sage Weil | 9c423956 | 2010-03-20 20:43:28 -0700 | [diff] [blame] | 2330 | if (IS_ERR(session)) { |
| 2331 | err = PTR_ERR(session); |
| 2332 | goto finish; |
| 2333 | } |
| 2334 | } |
Sage Weil | dc69e2e | 2010-11-02 13:49:00 -0700 | [diff] [blame] | 2335 | req->r_session = get_session(session); |
| 2336 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2337 | dout("do_request mds%d session %p state %s\n", mds, session, |
John Spray | a687eca | 2014-09-19 13:51:08 +0100 | [diff] [blame] | 2338 | ceph_session_state_name(session->s_state)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2339 | if (session->s_state != CEPH_MDS_SESSION_OPEN && |
| 2340 | session->s_state != CEPH_MDS_SESSION_HUNG) { |
Yan, Zheng | fcff415 | 2016-09-14 16:39:51 +0800 | [diff] [blame] | 2341 | if (session->s_state == CEPH_MDS_SESSION_REJECTED) { |
| 2342 | err = -EACCES; |
| 2343 | goto out_session; |
| 2344 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2345 | if (session->s_state == CEPH_MDS_SESSION_NEW || |
| 2346 | session->s_state == CEPH_MDS_SESSION_CLOSING) |
| 2347 | __open_session(mdsc, session); |
| 2348 | list_add(&req->r_wait, &session->s_waiting); |
| 2349 | goto out_session; |
| 2350 | } |
| 2351 | |
| 2352 | /* send request */ |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2353 | req->r_resend_mds = -1; /* forget any previous mds hint */ |
| 2354 | |
| 2355 | if (req->r_request_started == 0) /* note request start time */ |
| 2356 | req->r_request_started = jiffies; |
| 2357 | |
Yan, Zheng | 6e6f092 | 2015-02-27 08:54:08 +0800 | [diff] [blame] | 2358 | err = __prepare_send_request(mdsc, req, mds, false); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2359 | if (!err) { |
| 2360 | ceph_msg_get(req->r_request); |
| 2361 | ceph_con_send(&session->s_con, req->r_request); |
| 2362 | } |
| 2363 | |
| 2364 | out_session: |
| 2365 | ceph_put_mds_session(session); |
Yan, Zheng | 48fec5d | 2015-07-01 16:27:46 +0800 | [diff] [blame] | 2366 | finish: |
| 2367 | if (err) { |
| 2368 | dout("__do_request early error %d\n", err); |
| 2369 | req->r_err = err; |
| 2370 | complete_request(mdsc, req); |
| 2371 | __unregister_request(mdsc, req); |
| 2372 | } |
Chengguang Xu | d554849 | 2018-07-28 16:30:48 +0800 | [diff] [blame] | 2373 | return; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2374 | } |
| 2375 | |
| 2376 | /* |
| 2377 | * called under mdsc->mutex |
| 2378 | */ |
| 2379 | static void __wake_requests(struct ceph_mds_client *mdsc, |
| 2380 | struct list_head *head) |
| 2381 | { |
Yan, Zheng | ed75ec2 | 2012-11-19 10:49:06 +0800 | [diff] [blame] | 2382 | struct ceph_mds_request *req; |
| 2383 | LIST_HEAD(tmp_list); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2384 | |
Yan, Zheng | ed75ec2 | 2012-11-19 10:49:06 +0800 | [diff] [blame] | 2385 | list_splice_init(head, &tmp_list); |
| 2386 | |
| 2387 | while (!list_empty(&tmp_list)) { |
| 2388 | req = list_entry(tmp_list.next, |
| 2389 | struct ceph_mds_request, r_wait); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2390 | list_del_init(&req->r_wait); |
Sage Weil | 7971bd9 | 2013-05-01 21:15:58 -0700 | [diff] [blame] | 2391 | dout(" wake request %p tid %llu\n", req, req->r_tid); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2392 | __do_request(mdsc, req); |
| 2393 | } |
| 2394 | } |
| 2395 | |
| 2396 | /* |
| 2397 | * Wake up threads with requests pending for @mds, so that they can |
Sage Weil | 29790f2 | 2010-03-18 14:45:05 -0700 | [diff] [blame] | 2398 | * resubmit their requests to a possibly different mds. |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2399 | */ |
Sage Weil | 29790f2 | 2010-03-18 14:45:05 -0700 | [diff] [blame] | 2400 | static void kick_requests(struct ceph_mds_client *mdsc, int mds) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2401 | { |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 2402 | struct ceph_mds_request *req; |
Yan, Zheng | 282c105 | 2014-07-30 10:12:47 +0800 | [diff] [blame] | 2403 | struct rb_node *p = rb_first(&mdsc->request_tree); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2404 | |
| 2405 | dout("kick_requests mds%d\n", mds); |
Yan, Zheng | 282c105 | 2014-07-30 10:12:47 +0800 | [diff] [blame] | 2406 | while (p) { |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 2407 | req = rb_entry(p, struct ceph_mds_request, r_node); |
Yan, Zheng | 282c105 | 2014-07-30 10:12:47 +0800 | [diff] [blame] | 2408 | p = rb_next(p); |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2409 | if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 2410 | continue; |
Yan, Zheng | 3de22be | 2015-02-04 14:26:22 +0800 | [diff] [blame] | 2411 | if (req->r_attempts > 0) |
| 2412 | continue; /* only new requests */ |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 2413 | if (req->r_session && |
| 2414 | req->r_session->s_mds == mds) { |
| 2415 | dout(" kicking tid %llu\n", req->r_tid); |
Yan, Zheng | 03974e8 | 2014-09-11 14:28:56 +0800 | [diff] [blame] | 2416 | list_del_init(&req->r_wait); |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 2417 | __do_request(mdsc, req); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2418 | } |
| 2419 | } |
| 2420 | } |
| 2421 | |
| 2422 | void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, |
| 2423 | struct ceph_mds_request *req) |
| 2424 | { |
| 2425 | dout("submit_request on %p\n", req); |
| 2426 | mutex_lock(&mdsc->mutex); |
| 2427 | __register_request(mdsc, req, NULL); |
| 2428 | __do_request(mdsc, req); |
| 2429 | mutex_unlock(&mdsc->mutex); |
| 2430 | } |
| 2431 | |
| 2432 | /* |
| 2433 | * Synchrously perform an mds request. Take care of all of the |
| 2434 | * session setup, forwarding, retry details. |
| 2435 | */ |
| 2436 | int ceph_mdsc_do_request(struct ceph_mds_client *mdsc, |
| 2437 | struct inode *dir, |
| 2438 | struct ceph_mds_request *req) |
| 2439 | { |
| 2440 | int err; |
| 2441 | |
| 2442 | dout("do_request on %p\n", req); |
| 2443 | |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 2444 | /* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */ |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2445 | if (req->r_inode) |
| 2446 | ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN); |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 2447 | if (req->r_parent) |
| 2448 | ceph_get_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN); |
Sage Weil | 844d87c | 2013-02-05 13:40:09 -0800 | [diff] [blame] | 2449 | if (req->r_old_dentry_dir) |
Sage Weil | 41b02e1 | 2011-07-26 11:31:14 -0700 | [diff] [blame] | 2450 | ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir), |
| 2451 | CEPH_CAP_PIN); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2452 | |
| 2453 | /* issue */ |
| 2454 | mutex_lock(&mdsc->mutex); |
| 2455 | __register_request(mdsc, req, dir); |
| 2456 | __do_request(mdsc, req); |
| 2457 | |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2458 | if (req->r_err) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2459 | err = req->r_err; |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2460 | goto out; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2461 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2462 | |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2463 | /* wait */ |
| 2464 | mutex_unlock(&mdsc->mutex); |
| 2465 | dout("do_request waiting\n"); |
Ilya Dryomov | 5be7303 | 2015-05-19 12:05:38 +0300 | [diff] [blame] | 2466 | if (!req->r_timeout && req->r_wait_for_completion) { |
Yan, Zheng | 9280be2 | 2014-10-14 10:33:35 +0800 | [diff] [blame] | 2467 | err = req->r_wait_for_completion(mdsc, req); |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2468 | } else { |
Ilya Dryomov | 5be7303 | 2015-05-19 12:05:38 +0300 | [diff] [blame] | 2469 | long timeleft = wait_for_completion_killable_timeout( |
| 2470 | &req->r_completion, |
| 2471 | ceph_timeout_jiffies(req->r_timeout)); |
| 2472 | if (timeleft > 0) |
| 2473 | err = 0; |
| 2474 | else if (!timeleft) |
| 2475 | err = -EIO; /* timed out */ |
| 2476 | else |
| 2477 | err = timeleft; /* killed */ |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2478 | } |
| 2479 | dout("do_request waited, got %d\n", err); |
| 2480 | mutex_lock(&mdsc->mutex); |
| 2481 | |
| 2482 | /* only abort if we didn't race with a real reply */ |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2483 | if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) { |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2484 | err = le32_to_cpu(req->r_reply_info.head->result); |
| 2485 | } else if (err < 0) { |
| 2486 | dout("aborted request %lld with %d\n", req->r_tid, err); |
Sage Weil | b455639 | 2010-05-13 12:01:13 -0700 | [diff] [blame] | 2487 | |
| 2488 | /* |
| 2489 | * ensure we aren't running concurrently with |
| 2490 | * ceph_fill_trace or ceph_readdir_prepopulate, which |
| 2491 | * rely on locks (dir mutex) held by our caller. |
| 2492 | */ |
| 2493 | mutex_lock(&req->r_fill_mutex); |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2494 | req->r_err = err; |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2495 | set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags); |
Sage Weil | b455639 | 2010-05-13 12:01:13 -0700 | [diff] [blame] | 2496 | mutex_unlock(&req->r_fill_mutex); |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2497 | |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 2498 | if (req->r_parent && |
Sage Weil | 167c9e3 | 2010-05-14 10:02:57 -0700 | [diff] [blame] | 2499 | (req->r_op & CEPH_MDS_OP_WRITE)) |
| 2500 | ceph_invalidate_dir_request(req); |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2501 | } else { |
| 2502 | err = req->r_err; |
| 2503 | } |
| 2504 | |
| 2505 | out: |
| 2506 | mutex_unlock(&mdsc->mutex); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2507 | dout("do_request %p done, result %d\n", req, err); |
| 2508 | return err; |
| 2509 | } |
| 2510 | |
| 2511 | /* |
Yan, Zheng | 2f276c5 | 2013-03-13 19:44:32 +0800 | [diff] [blame] | 2512 | * Invalidate dir's completeness, dentry lease state on an aborted MDS |
Sage Weil | 167c9e3 | 2010-05-14 10:02:57 -0700 | [diff] [blame] | 2513 | * namespace request. |
| 2514 | */ |
| 2515 | void ceph_invalidate_dir_request(struct ceph_mds_request *req) |
| 2516 | { |
Yan, Zheng | 8d8f371 | 2017-11-24 11:51:32 +0800 | [diff] [blame] | 2517 | struct inode *dir = req->r_parent; |
| 2518 | struct inode *old_dir = req->r_old_dentry_dir; |
Sage Weil | 167c9e3 | 2010-05-14 10:02:57 -0700 | [diff] [blame] | 2519 | |
Yan, Zheng | 8d8f371 | 2017-11-24 11:51:32 +0800 | [diff] [blame] | 2520 | dout("invalidate_dir_request %p %p (complete, lease(s))\n", dir, old_dir); |
Sage Weil | 167c9e3 | 2010-05-14 10:02:57 -0700 | [diff] [blame] | 2521 | |
Yan, Zheng | 8d8f371 | 2017-11-24 11:51:32 +0800 | [diff] [blame] | 2522 | ceph_dir_clear_complete(dir); |
| 2523 | if (old_dir) |
| 2524 | ceph_dir_clear_complete(old_dir); |
Sage Weil | 167c9e3 | 2010-05-14 10:02:57 -0700 | [diff] [blame] | 2525 | if (req->r_dentry) |
| 2526 | ceph_invalidate_dentry_lease(req->r_dentry); |
| 2527 | if (req->r_old_dentry) |
| 2528 | ceph_invalidate_dentry_lease(req->r_old_dentry); |
| 2529 | } |
| 2530 | |
| 2531 | /* |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2532 | * Handle mds reply. |
| 2533 | * |
| 2534 | * We take the session mutex and parse and process the reply immediately. |
| 2535 | * This preserves the logical ordering of replies, capabilities, etc., sent |
| 2536 | * by the MDS as they are applied to our local cache. |
| 2537 | */ |
| 2538 | static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg) |
| 2539 | { |
| 2540 | struct ceph_mds_client *mdsc = session->s_mdsc; |
| 2541 | struct ceph_mds_request *req; |
| 2542 | struct ceph_mds_reply_head *head = msg->front.iov_base; |
| 2543 | struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */ |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 2544 | struct ceph_snap_realm *realm; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2545 | u64 tid; |
| 2546 | int err, result; |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 2547 | int mds = session->s_mds; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2548 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2549 | if (msg->front.iov_len < sizeof(*head)) { |
| 2550 | pr_err("mdsc_handle_reply got corrupt (short) reply\n"); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 2551 | ceph_msg_dump(msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2552 | return; |
| 2553 | } |
| 2554 | |
| 2555 | /* get request, session */ |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 2556 | tid = le64_to_cpu(msg->hdr.tid); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2557 | mutex_lock(&mdsc->mutex); |
Ilya Dryomov | fcd00b6 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2558 | req = lookup_get_request(mdsc, tid); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2559 | if (!req) { |
| 2560 | dout("handle_reply on unknown tid %llu\n", tid); |
| 2561 | mutex_unlock(&mdsc->mutex); |
| 2562 | return; |
| 2563 | } |
| 2564 | dout("handle_reply %p\n", req); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2565 | |
| 2566 | /* correct session? */ |
Sage Weil | d96d604 | 2010-03-20 20:50:58 -0700 | [diff] [blame] | 2567 | if (req->r_session != session) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2568 | pr_err("mdsc_handle_reply got %llu on session mds%d" |
| 2569 | " not mds%d\n", tid, session->s_mds, |
| 2570 | req->r_session ? req->r_session->s_mds : -1); |
| 2571 | mutex_unlock(&mdsc->mutex); |
| 2572 | goto out; |
| 2573 | } |
| 2574 | |
| 2575 | /* dup? */ |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2576 | if ((test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags) && !head->safe) || |
| 2577 | (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags) && head->safe)) { |
Fabian Frederick | f3ae1b9 | 2014-06-06 14:35:37 -0700 | [diff] [blame] | 2578 | pr_warn("got a dup %s reply on %llu from mds%d\n", |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2579 | head->safe ? "safe" : "unsafe", tid, mds); |
| 2580 | mutex_unlock(&mdsc->mutex); |
| 2581 | goto out; |
| 2582 | } |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2583 | if (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags)) { |
Fabian Frederick | f3ae1b9 | 2014-06-06 14:35:37 -0700 | [diff] [blame] | 2584 | pr_warn("got unsafe after safe on %llu from mds%d\n", |
Sage Weil | 85792d0d | 2010-05-13 09:06:02 -0700 | [diff] [blame] | 2585 | tid, mds); |
| 2586 | mutex_unlock(&mdsc->mutex); |
| 2587 | goto out; |
| 2588 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2589 | |
| 2590 | result = le32_to_cpu(head->result); |
| 2591 | |
| 2592 | /* |
Greg Farnum | e55b71f | 2010-06-22 15:58:01 -0700 | [diff] [blame] | 2593 | * Handle an ESTALE |
| 2594 | * if we're not talking to the authority, send to them |
| 2595 | * if the authority has changed while we weren't looking, |
| 2596 | * send to new authority |
| 2597 | * Otherwise we just have to return an ESTALE |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2598 | */ |
| 2599 | if (result == -ESTALE) { |
Chengguang Xu | 4c069a5 | 2018-01-30 16:29:17 +0800 | [diff] [blame] | 2600 | dout("got ESTALE on request %llu\n", req->r_tid); |
Yan, Zheng | 51da8e8 | 2014-07-14 10:48:29 +0800 | [diff] [blame] | 2601 | req->r_resend_mds = -1; |
Yan, Zheng | ca18bed | 2013-11-22 14:21:44 +0800 | [diff] [blame] | 2602 | if (req->r_direct_mode != USE_AUTH_MDS) { |
Chengguang Xu | 4c069a5 | 2018-01-30 16:29:17 +0800 | [diff] [blame] | 2603 | dout("not using auth, setting for that now\n"); |
Greg Farnum | e55b71f | 2010-06-22 15:58:01 -0700 | [diff] [blame] | 2604 | req->r_direct_mode = USE_AUTH_MDS; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2605 | __do_request(mdsc, req); |
| 2606 | mutex_unlock(&mdsc->mutex); |
| 2607 | goto out; |
Greg Farnum | e55b71f | 2010-06-22 15:58:01 -0700 | [diff] [blame] | 2608 | } else { |
Yan, Zheng | ca18bed | 2013-11-22 14:21:44 +0800 | [diff] [blame] | 2609 | int mds = __choose_mds(mdsc, req); |
| 2610 | if (mds >= 0 && mds != req->r_session->s_mds) { |
Chengguang Xu | 4c069a5 | 2018-01-30 16:29:17 +0800 | [diff] [blame] | 2611 | dout("but auth changed, so resending\n"); |
Greg Farnum | e55b71f | 2010-06-22 15:58:01 -0700 | [diff] [blame] | 2612 | __do_request(mdsc, req); |
| 2613 | mutex_unlock(&mdsc->mutex); |
| 2614 | goto out; |
| 2615 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2616 | } |
Chengguang Xu | 4c069a5 | 2018-01-30 16:29:17 +0800 | [diff] [blame] | 2617 | dout("have to return ESTALE on request %llu\n", req->r_tid); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2618 | } |
| 2619 | |
Greg Farnum | e55b71f | 2010-06-22 15:58:01 -0700 | [diff] [blame] | 2620 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2621 | if (head->safe) { |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2622 | set_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2623 | __unregister_request(mdsc, req); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2624 | |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2625 | if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2626 | /* |
| 2627 | * We already handled the unsafe response, now do the |
| 2628 | * cleanup. No need to examine the response; the MDS |
| 2629 | * doesn't include any result info in the safe |
| 2630 | * response. And even if it did, there is nothing |
| 2631 | * useful we could do with a revised return value. |
| 2632 | */ |
| 2633 | dout("got safe reply %llu, mds%d\n", tid, mds); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2634 | |
| 2635 | /* last unsafe request during umount? */ |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 2636 | if (mdsc->stopping && !__get_oldest_req(mdsc)) |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 2637 | complete_all(&mdsc->safe_umount_waiters); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2638 | mutex_unlock(&mdsc->mutex); |
| 2639 | goto out; |
| 2640 | } |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2641 | } else { |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2642 | set_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2643 | list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe); |
Yan, Zheng | 4c06ace | 2015-10-27 17:18:00 +0800 | [diff] [blame] | 2644 | if (req->r_unsafe_dir) { |
| 2645 | struct ceph_inode_info *ci = |
| 2646 | ceph_inode(req->r_unsafe_dir); |
| 2647 | spin_lock(&ci->i_unsafe_lock); |
| 2648 | list_add_tail(&req->r_unsafe_dir_item, |
| 2649 | &ci->i_unsafe_dirops); |
| 2650 | spin_unlock(&ci->i_unsafe_lock); |
| 2651 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2652 | } |
| 2653 | |
| 2654 | dout("handle_reply tid %lld result %d\n", tid, result); |
| 2655 | rinfo = &req->r_reply_info; |
Sage Weil | 14303d2 | 2010-12-14 17:37:52 -0800 | [diff] [blame] | 2656 | err = parse_reply_info(msg, rinfo, session->s_con.peer_features); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2657 | mutex_unlock(&mdsc->mutex); |
| 2658 | |
| 2659 | mutex_lock(&session->s_mutex); |
| 2660 | if (err < 0) { |
Herb Shiu | 25933ab | 2010-12-01 14:14:38 -0800 | [diff] [blame] | 2661 | pr_err("mdsc_handle_reply got corrupt reply mds%d(tid:%lld)\n", mds, tid); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 2662 | ceph_msg_dump(msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2663 | goto out_err; |
| 2664 | } |
| 2665 | |
| 2666 | /* snap trace */ |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 2667 | realm = NULL; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2668 | if (rinfo->snapblob_len) { |
| 2669 | down_write(&mdsc->snap_rwsem); |
| 2670 | ceph_update_snap_trace(mdsc, rinfo->snapblob, |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 2671 | rinfo->snapblob + rinfo->snapblob_len, |
| 2672 | le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP, |
| 2673 | &realm); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2674 | downgrade_write(&mdsc->snap_rwsem); |
| 2675 | } else { |
| 2676 | down_read(&mdsc->snap_rwsem); |
| 2677 | } |
| 2678 | |
| 2679 | /* insert trace into our cache */ |
Sage Weil | b455639 | 2010-05-13 12:01:13 -0700 | [diff] [blame] | 2680 | mutex_lock(&req->r_fill_mutex); |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 2681 | current->journal_info = req; |
Jeff Layton | f5a03b0 | 2017-01-31 11:06:13 -0500 | [diff] [blame] | 2682 | err = ceph_fill_trace(mdsc->fsc->sb, req); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2683 | if (err == 0) { |
Sam Lang | 6e8575f | 2012-12-28 09:56:46 -0800 | [diff] [blame] | 2684 | if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR || |
Yan, Zheng | 81c6aea | 2013-09-18 09:44:13 +0800 | [diff] [blame] | 2685 | req->r_op == CEPH_MDS_OP_LSSNAP)) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2686 | ceph_readdir_prepopulate(req, req->r_session); |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 2687 | ceph_unreserve_caps(mdsc, &req->r_caps_reservation); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2688 | } |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 2689 | current->journal_info = NULL; |
Sage Weil | b455639 | 2010-05-13 12:01:13 -0700 | [diff] [blame] | 2690 | mutex_unlock(&req->r_fill_mutex); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2691 | |
| 2692 | up_read(&mdsc->snap_rwsem); |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 2693 | if (realm) |
| 2694 | ceph_put_snap_realm(mdsc, realm); |
Yan, Zheng | 68cd5b4 | 2015-10-27 18:36:06 +0800 | [diff] [blame] | 2695 | |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2696 | if (err == 0 && req->r_target_inode && |
| 2697 | test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) { |
Yan, Zheng | 68cd5b4 | 2015-10-27 18:36:06 +0800 | [diff] [blame] | 2698 | struct ceph_inode_info *ci = ceph_inode(req->r_target_inode); |
| 2699 | spin_lock(&ci->i_unsafe_lock); |
| 2700 | list_add_tail(&req->r_unsafe_target_item, &ci->i_unsafe_iops); |
| 2701 | spin_unlock(&ci->i_unsafe_lock); |
| 2702 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2703 | out_err: |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2704 | mutex_lock(&mdsc->mutex); |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2705 | if (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) { |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2706 | if (err) { |
| 2707 | req->r_err = err; |
| 2708 | } else { |
Jianpeng Ma | 5fdb138 | 2015-08-18 10:30:38 +0800 | [diff] [blame] | 2709 | req->r_reply = ceph_msg_get(msg); |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2710 | set_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags); |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2711 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2712 | } else { |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2713 | dout("reply arrived after request %lld was aborted\n", tid); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2714 | } |
Sage Weil | e1518c7 | 2010-05-13 11:19:06 -0700 | [diff] [blame] | 2715 | mutex_unlock(&mdsc->mutex); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2716 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2717 | mutex_unlock(&session->s_mutex); |
| 2718 | |
| 2719 | /* kick calling process */ |
| 2720 | complete_request(mdsc, req); |
| 2721 | out: |
| 2722 | ceph_mdsc_put_request(req); |
| 2723 | return; |
| 2724 | } |
| 2725 | |
| 2726 | |
| 2727 | |
| 2728 | /* |
| 2729 | * handle mds notification that our request has been forwarded. |
| 2730 | */ |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 2731 | static void handle_forward(struct ceph_mds_client *mdsc, |
| 2732 | struct ceph_mds_session *session, |
| 2733 | struct ceph_msg *msg) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2734 | { |
| 2735 | struct ceph_mds_request *req; |
Sage Weil | a1ea787 | 2010-02-23 14:02:44 -0800 | [diff] [blame] | 2736 | u64 tid = le64_to_cpu(msg->hdr.tid); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2737 | u32 next_mds; |
| 2738 | u32 fwd_seq; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2739 | int err = -EINVAL; |
| 2740 | void *p = msg->front.iov_base; |
| 2741 | void *end = p + msg->front.iov_len; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2742 | |
Sage Weil | a1ea787 | 2010-02-23 14:02:44 -0800 | [diff] [blame] | 2743 | ceph_decode_need(&p, end, 2*sizeof(u32), bad); |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 2744 | next_mds = ceph_decode_32(&p); |
| 2745 | fwd_seq = ceph_decode_32(&p); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2746 | |
| 2747 | mutex_lock(&mdsc->mutex); |
Ilya Dryomov | fcd00b6 | 2016-04-28 16:07:22 +0200 | [diff] [blame] | 2748 | req = lookup_get_request(mdsc, tid); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2749 | if (!req) { |
Sage Weil | 2a8e5e3 | 2010-05-28 16:43:16 -0700 | [diff] [blame] | 2750 | dout("forward tid %llu to mds%d - req dne\n", tid, next_mds); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2751 | goto out; /* dup reply? */ |
| 2752 | } |
| 2753 | |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2754 | if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) { |
Sage Weil | 2a8e5e3 | 2010-05-28 16:43:16 -0700 | [diff] [blame] | 2755 | dout("forward tid %llu aborted, unregistering\n", tid); |
| 2756 | __unregister_request(mdsc, req); |
| 2757 | } else if (fwd_seq <= req->r_num_fwd) { |
| 2758 | dout("forward tid %llu to mds%d - old seq %d <= %d\n", |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2759 | tid, next_mds, req->r_num_fwd, fwd_seq); |
| 2760 | } else { |
| 2761 | /* resend. forward race not possible; mds would drop */ |
Sage Weil | 2a8e5e3 | 2010-05-28 16:43:16 -0700 | [diff] [blame] | 2762 | dout("forward tid %llu to mds%d (we resend)\n", tid, next_mds); |
| 2763 | BUG_ON(req->r_err); |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2764 | BUG_ON(test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)); |
Yan, Zheng | 3de22be | 2015-02-04 14:26:22 +0800 | [diff] [blame] | 2765 | req->r_attempts = 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2766 | req->r_num_fwd = fwd_seq; |
| 2767 | req->r_resend_mds = next_mds; |
| 2768 | put_request_session(req); |
| 2769 | __do_request(mdsc, req); |
| 2770 | } |
| 2771 | ceph_mdsc_put_request(req); |
| 2772 | out: |
| 2773 | mutex_unlock(&mdsc->mutex); |
| 2774 | return; |
| 2775 | |
| 2776 | bad: |
| 2777 | pr_err("mdsc_handle_forward decode error err=%d\n", err); |
| 2778 | } |
| 2779 | |
Yan, Zheng | 84bf395 | 2018-12-21 17:41:39 +0800 | [diff] [blame^] | 2780 | static int __decode_and_drop_session_metadata(void **p, void *end) |
| 2781 | { |
| 2782 | /* map<string,string> */ |
| 2783 | u32 n; |
| 2784 | ceph_decode_32_safe(p, end, n, bad); |
| 2785 | while (n-- > 0) { |
| 2786 | u32 len; |
| 2787 | ceph_decode_32_safe(p, end, len, bad); |
| 2788 | ceph_decode_need(p, end, len, bad); |
| 2789 | *p += len; |
| 2790 | ceph_decode_32_safe(p, end, len, bad); |
| 2791 | ceph_decode_need(p, end, len, bad); |
| 2792 | *p += len; |
| 2793 | } |
| 2794 | return 0; |
| 2795 | bad: |
| 2796 | return -1; |
| 2797 | } |
| 2798 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2799 | /* |
| 2800 | * handle a mds session control message |
| 2801 | */ |
| 2802 | static void handle_session(struct ceph_mds_session *session, |
| 2803 | struct ceph_msg *msg) |
| 2804 | { |
| 2805 | struct ceph_mds_client *mdsc = session->s_mdsc; |
Yan, Zheng | 84bf395 | 2018-12-21 17:41:39 +0800 | [diff] [blame^] | 2806 | int mds = session->s_mds; |
| 2807 | int msg_version = le16_to_cpu(msg->hdr.version); |
| 2808 | void *p = msg->front.iov_base; |
| 2809 | void *end = p + msg->front.iov_len; |
| 2810 | struct ceph_mds_session_head *h; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2811 | u32 op; |
| 2812 | u64 seq; |
Yan, Zheng | 84bf395 | 2018-12-21 17:41:39 +0800 | [diff] [blame^] | 2813 | unsigned long features = 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2814 | int wake = 0; |
| 2815 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2816 | /* decode */ |
Yan, Zheng | 84bf395 | 2018-12-21 17:41:39 +0800 | [diff] [blame^] | 2817 | ceph_decode_need(&p, end, sizeof(*h), bad); |
| 2818 | h = p; |
| 2819 | p += sizeof(*h); |
| 2820 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2821 | op = le32_to_cpu(h->op); |
| 2822 | seq = le64_to_cpu(h->seq); |
| 2823 | |
Yan, Zheng | 84bf395 | 2018-12-21 17:41:39 +0800 | [diff] [blame^] | 2824 | if (msg_version >= 3) { |
| 2825 | u32 len; |
| 2826 | /* version >= 2, metadata */ |
| 2827 | if (__decode_and_drop_session_metadata(&p, end) < 0) |
| 2828 | goto bad; |
| 2829 | /* version >= 3, feature bits */ |
| 2830 | ceph_decode_32_safe(&p, end, len, bad); |
| 2831 | ceph_decode_need(&p, end, len, bad); |
| 2832 | memcpy(&features, p, min_t(size_t, len, sizeof(features))); |
| 2833 | p += len; |
| 2834 | } |
| 2835 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2836 | mutex_lock(&mdsc->mutex); |
Yan, Zheng | 0a07fc8 | 2017-03-29 15:30:24 +0800 | [diff] [blame] | 2837 | if (op == CEPH_SESSION_CLOSE) { |
| 2838 | get_session(session); |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 2839 | __unregister_session(mdsc, session); |
Yan, Zheng | 0a07fc8 | 2017-03-29 15:30:24 +0800 | [diff] [blame] | 2840 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2841 | /* FIXME: this ttl calculation is generous */ |
| 2842 | session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose; |
| 2843 | mutex_unlock(&mdsc->mutex); |
| 2844 | |
| 2845 | mutex_lock(&session->s_mutex); |
| 2846 | |
| 2847 | dout("handle_session mds%d %s %p state %s seq %llu\n", |
| 2848 | mds, ceph_session_op_name(op), session, |
John Spray | a687eca | 2014-09-19 13:51:08 +0100 | [diff] [blame] | 2849 | ceph_session_state_name(session->s_state), seq); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2850 | |
| 2851 | if (session->s_state == CEPH_MDS_SESSION_HUNG) { |
| 2852 | session->s_state = CEPH_MDS_SESSION_OPEN; |
| 2853 | pr_info("mds%d came back\n", session->s_mds); |
| 2854 | } |
| 2855 | |
| 2856 | switch (op) { |
| 2857 | case CEPH_SESSION_OPEN: |
Sage Weil | 29790f2 | 2010-03-18 14:45:05 -0700 | [diff] [blame] | 2858 | if (session->s_state == CEPH_MDS_SESSION_RECONNECTING) |
| 2859 | pr_info("mds%d reconnect success\n", session->s_mds); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2860 | session->s_state = CEPH_MDS_SESSION_OPEN; |
Yan, Zheng | 84bf395 | 2018-12-21 17:41:39 +0800 | [diff] [blame^] | 2861 | session->s_features = features; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2862 | renewed_caps(mdsc, session, 0); |
| 2863 | wake = 1; |
| 2864 | if (mdsc->stopping) |
| 2865 | __close_session(mdsc, session); |
| 2866 | break; |
| 2867 | |
| 2868 | case CEPH_SESSION_RENEWCAPS: |
| 2869 | if (session->s_renew_seq == seq) |
| 2870 | renewed_caps(mdsc, session, 1); |
| 2871 | break; |
| 2872 | |
| 2873 | case CEPH_SESSION_CLOSE: |
Sage Weil | 29790f2 | 2010-03-18 14:45:05 -0700 | [diff] [blame] | 2874 | if (session->s_state == CEPH_MDS_SESSION_RECONNECTING) |
| 2875 | pr_info("mds%d reconnect denied\n", session->s_mds); |
Yan, Zheng | 1c841a9 | 2015-03-24 20:15:36 +0800 | [diff] [blame] | 2876 | cleanup_session_requests(mdsc, session); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2877 | remove_session_caps(session); |
Yan, Zheng | 656e438 | 2014-09-11 14:25:18 +0800 | [diff] [blame] | 2878 | wake = 2; /* for good measure */ |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 2879 | wake_up_all(&mdsc->session_close_wq); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2880 | break; |
| 2881 | |
| 2882 | case CEPH_SESSION_STALE: |
| 2883 | pr_info("mds%d caps went stale, renewing\n", |
| 2884 | session->s_mds); |
Alex Elder | d8fb02a | 2012-01-12 17:48:10 -0800 | [diff] [blame] | 2885 | spin_lock(&session->s_gen_ttl_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2886 | session->s_cap_gen++; |
Alex Elder | 1ce208a | 2012-01-12 17:48:11 -0800 | [diff] [blame] | 2887 | session->s_cap_ttl = jiffies - 1; |
Alex Elder | d8fb02a | 2012-01-12 17:48:10 -0800 | [diff] [blame] | 2888 | spin_unlock(&session->s_gen_ttl_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2889 | send_renew_caps(mdsc, session); |
| 2890 | break; |
| 2891 | |
| 2892 | case CEPH_SESSION_RECALL_STATE: |
Zhi Zhang | e30ee58 | 2018-01-24 21:24:33 +0800 | [diff] [blame] | 2893 | ceph_trim_caps(mdsc, session, le32_to_cpu(h->max_caps)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2894 | break; |
| 2895 | |
Yan, Zheng | 186e4f7 | 2013-11-22 14:48:37 +0800 | [diff] [blame] | 2896 | case CEPH_SESSION_FLUSHMSG: |
| 2897 | send_flushmsg_ack(mdsc, session, seq); |
| 2898 | break; |
| 2899 | |
Yan, Zheng | 03f4fcb | 2015-01-05 11:04:04 +0800 | [diff] [blame] | 2900 | case CEPH_SESSION_FORCE_RO: |
| 2901 | dout("force_session_readonly %p\n", session); |
| 2902 | spin_lock(&session->s_cap_lock); |
| 2903 | session->s_readonly = true; |
| 2904 | spin_unlock(&session->s_cap_lock); |
Yan, Zheng | d2f8bb2 | 2018-12-10 16:35:09 +0800 | [diff] [blame] | 2905 | wake_up_session_caps(session, FORCE_RO); |
Yan, Zheng | 03f4fcb | 2015-01-05 11:04:04 +0800 | [diff] [blame] | 2906 | break; |
| 2907 | |
Yan, Zheng | fcff415 | 2016-09-14 16:39:51 +0800 | [diff] [blame] | 2908 | case CEPH_SESSION_REJECT: |
| 2909 | WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING); |
| 2910 | pr_info("mds%d rejected session\n", session->s_mds); |
| 2911 | session->s_state = CEPH_MDS_SESSION_REJECTED; |
| 2912 | cleanup_session_requests(mdsc, session); |
| 2913 | remove_session_caps(session); |
| 2914 | wake = 2; /* for good measure */ |
| 2915 | break; |
| 2916 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2917 | default: |
| 2918 | pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds); |
| 2919 | WARN_ON(1); |
| 2920 | } |
| 2921 | |
| 2922 | mutex_unlock(&session->s_mutex); |
| 2923 | if (wake) { |
| 2924 | mutex_lock(&mdsc->mutex); |
| 2925 | __wake_requests(mdsc, &session->s_waiting); |
Yan, Zheng | 656e438 | 2014-09-11 14:25:18 +0800 | [diff] [blame] | 2926 | if (wake == 2) |
| 2927 | kick_requests(mdsc, mds); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2928 | mutex_unlock(&mdsc->mutex); |
| 2929 | } |
Yan, Zheng | 0a07fc8 | 2017-03-29 15:30:24 +0800 | [diff] [blame] | 2930 | if (op == CEPH_SESSION_CLOSE) |
| 2931 | ceph_put_mds_session(session); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2932 | return; |
| 2933 | |
| 2934 | bad: |
| 2935 | pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds, |
| 2936 | (int)msg->front.iov_len); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 2937 | ceph_msg_dump(msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2938 | return; |
| 2939 | } |
| 2940 | |
| 2941 | |
| 2942 | /* |
| 2943 | * called under session->mutex. |
| 2944 | */ |
| 2945 | static void replay_unsafe_requests(struct ceph_mds_client *mdsc, |
| 2946 | struct ceph_mds_session *session) |
| 2947 | { |
| 2948 | struct ceph_mds_request *req, *nreq; |
Yan, Zheng | 3de22be | 2015-02-04 14:26:22 +0800 | [diff] [blame] | 2949 | struct rb_node *p; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2950 | int err; |
| 2951 | |
| 2952 | dout("replay_unsafe_requests mds%d\n", session->s_mds); |
| 2953 | |
| 2954 | mutex_lock(&mdsc->mutex); |
| 2955 | list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) { |
Yan, Zheng | 6e6f092 | 2015-02-27 08:54:08 +0800 | [diff] [blame] | 2956 | err = __prepare_send_request(mdsc, req, session->s_mds, true); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2957 | if (!err) { |
| 2958 | ceph_msg_get(req->r_request); |
| 2959 | ceph_con_send(&session->s_con, req->r_request); |
| 2960 | } |
| 2961 | } |
Yan, Zheng | 3de22be | 2015-02-04 14:26:22 +0800 | [diff] [blame] | 2962 | |
| 2963 | /* |
| 2964 | * also re-send old requests when MDS enters reconnect stage. So that MDS |
| 2965 | * can process completed request in clientreplay stage. |
| 2966 | */ |
| 2967 | p = rb_first(&mdsc->request_tree); |
| 2968 | while (p) { |
| 2969 | req = rb_entry(p, struct ceph_mds_request, r_node); |
| 2970 | p = rb_next(p); |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 2971 | if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) |
Yan, Zheng | 3de22be | 2015-02-04 14:26:22 +0800 | [diff] [blame] | 2972 | continue; |
| 2973 | if (req->r_attempts == 0) |
| 2974 | continue; /* only old requests */ |
| 2975 | if (req->r_session && |
| 2976 | req->r_session->s_mds == session->s_mds) { |
Yan, Zheng | 6e6f092 | 2015-02-27 08:54:08 +0800 | [diff] [blame] | 2977 | err = __prepare_send_request(mdsc, req, |
| 2978 | session->s_mds, true); |
Yan, Zheng | 3de22be | 2015-02-04 14:26:22 +0800 | [diff] [blame] | 2979 | if (!err) { |
| 2980 | ceph_msg_get(req->r_request); |
| 2981 | ceph_con_send(&session->s_con, req->r_request); |
| 2982 | } |
| 2983 | } |
| 2984 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2985 | mutex_unlock(&mdsc->mutex); |
| 2986 | } |
| 2987 | |
| 2988 | /* |
| 2989 | * Encode information about a cap for a reconnect with the MDS. |
| 2990 | */ |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 2991 | static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap, |
| 2992 | void *arg) |
| 2993 | { |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 2994 | union { |
| 2995 | struct ceph_mds_cap_reconnect v2; |
| 2996 | struct ceph_mds_cap_reconnect_v1 v1; |
| 2997 | } rec; |
Yan, Zheng | b3f8d68 | 2017-09-11 10:58:55 +0800 | [diff] [blame] | 2998 | struct ceph_inode_info *ci = cap->ci; |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 2999 | struct ceph_reconnect_state *recon_state = arg; |
| 3000 | struct ceph_pagelist *pagelist = recon_state->pagelist; |
Yan, Zheng | 5ccedf1 | 2018-12-13 16:34:11 +0800 | [diff] [blame] | 3001 | int err; |
Yan, Zheng | 3469ed0 | 2016-07-05 09:32:31 +0800 | [diff] [blame] | 3002 | u64 snap_follows; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3003 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3004 | dout(" adding %p ino %llx.%llx cap %p %lld %s\n", |
| 3005 | inode, ceph_vinop(inode), cap, cap->cap_id, |
| 3006 | ceph_cap_string(cap->issued)); |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 3007 | err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode)); |
| 3008 | if (err) |
| 3009 | return err; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3010 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3011 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3012 | cap->seq = 0; /* reset cap seq */ |
| 3013 | cap->issue_seq = 0; /* and issue_seq */ |
Yan, Zheng | 667ca05 | 2013-05-31 16:25:36 +0800 | [diff] [blame] | 3014 | cap->mseq = 0; /* and migrate_seq */ |
Yan, Zheng | 99a9c27 | 2013-09-22 11:08:14 +0800 | [diff] [blame] | 3015 | cap->cap_gen = cap->session->s_cap_gen; |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 3016 | |
Yan, Zheng | 121f22a | 2016-07-04 22:05:18 +0800 | [diff] [blame] | 3017 | if (recon_state->msg_version >= 2) { |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 3018 | rec.v2.cap_id = cpu_to_le64(cap->cap_id); |
| 3019 | rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci)); |
| 3020 | rec.v2.issued = cpu_to_le32(cap->issued); |
| 3021 | rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino); |
Yan, Zheng | 5ccedf1 | 2018-12-13 16:34:11 +0800 | [diff] [blame] | 3022 | rec.v2.pathbase = 0; |
Jeff Layton | ec1dff2 | 2017-10-31 15:51:14 -0400 | [diff] [blame] | 3023 | rec.v2.flock_len = (__force __le32) |
| 3024 | ((ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) ? 0 : 1); |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 3025 | } else { |
| 3026 | rec.v1.cap_id = cpu_to_le64(cap->cap_id); |
| 3027 | rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci)); |
| 3028 | rec.v1.issued = cpu_to_le32(cap->issued); |
| 3029 | rec.v1.size = cpu_to_le64(inode->i_size); |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 3030 | ceph_encode_timespec64(&rec.v1.mtime, &inode->i_mtime); |
| 3031 | ceph_encode_timespec64(&rec.v1.atime, &inode->i_atime); |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 3032 | rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino); |
Yan, Zheng | 5ccedf1 | 2018-12-13 16:34:11 +0800 | [diff] [blame] | 3033 | rec.v1.pathbase = 0; |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 3034 | } |
Yan, Zheng | 3469ed0 | 2016-07-05 09:32:31 +0800 | [diff] [blame] | 3035 | |
| 3036 | if (list_empty(&ci->i_cap_snaps)) { |
Yan, Zheng | 92776fd | 2017-08-16 21:42:39 +0800 | [diff] [blame] | 3037 | snap_follows = ci->i_head_snapc ? ci->i_head_snapc->seq : 0; |
Yan, Zheng | 3469ed0 | 2016-07-05 09:32:31 +0800 | [diff] [blame] | 3038 | } else { |
| 3039 | struct ceph_cap_snap *capsnap = |
| 3040 | list_first_entry(&ci->i_cap_snaps, |
| 3041 | struct ceph_cap_snap, ci_item); |
| 3042 | snap_follows = capsnap->follows; |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 3043 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3044 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3045 | |
Yan, Zheng | 121f22a | 2016-07-04 22:05:18 +0800 | [diff] [blame] | 3046 | if (recon_state->msg_version >= 2) { |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 3047 | int num_fcntl_locks, num_flock_locks; |
Yan, Zheng | 4deb14a | 2017-09-11 10:36:28 +0800 | [diff] [blame] | 3048 | struct ceph_filelock *flocks = NULL; |
Yan, Zheng | 121f22a | 2016-07-04 22:05:18 +0800 | [diff] [blame] | 3049 | size_t struct_len, total_len = 0; |
| 3050 | u8 struct_v = 0; |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 3051 | |
Jim Schutt | 39be95e | 2013-05-15 13:03:35 -0500 | [diff] [blame] | 3052 | encode_again: |
Yan, Zheng | b3f8d68 | 2017-09-11 10:58:55 +0800 | [diff] [blame] | 3053 | if (rec.v2.flock_len) { |
| 3054 | ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks); |
| 3055 | } else { |
| 3056 | num_fcntl_locks = 0; |
| 3057 | num_flock_locks = 0; |
| 3058 | } |
Yan, Zheng | 4deb14a | 2017-09-11 10:36:28 +0800 | [diff] [blame] | 3059 | if (num_fcntl_locks + num_flock_locks > 0) { |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 3060 | flocks = kmalloc_array(num_fcntl_locks + num_flock_locks, |
| 3061 | sizeof(struct ceph_filelock), |
| 3062 | GFP_NOFS); |
Yan, Zheng | 4deb14a | 2017-09-11 10:36:28 +0800 | [diff] [blame] | 3063 | if (!flocks) { |
| 3064 | err = -ENOMEM; |
Yan, Zheng | 5ccedf1 | 2018-12-13 16:34:11 +0800 | [diff] [blame] | 3065 | goto out_err; |
Yan, Zheng | 4deb14a | 2017-09-11 10:36:28 +0800 | [diff] [blame] | 3066 | } |
| 3067 | err = ceph_encode_locks_to_buffer(inode, flocks, |
| 3068 | num_fcntl_locks, |
| 3069 | num_flock_locks); |
| 3070 | if (err) { |
| 3071 | kfree(flocks); |
| 3072 | flocks = NULL; |
| 3073 | if (err == -ENOSPC) |
| 3074 | goto encode_again; |
Yan, Zheng | 5ccedf1 | 2018-12-13 16:34:11 +0800 | [diff] [blame] | 3075 | goto out_err; |
Yan, Zheng | 4deb14a | 2017-09-11 10:36:28 +0800 | [diff] [blame] | 3076 | } |
| 3077 | } else { |
Jim Schutt | 39be95e | 2013-05-15 13:03:35 -0500 | [diff] [blame] | 3078 | kfree(flocks); |
Yan, Zheng | 4deb14a | 2017-09-11 10:36:28 +0800 | [diff] [blame] | 3079 | flocks = NULL; |
Jim Schutt | 39be95e | 2013-05-15 13:03:35 -0500 | [diff] [blame] | 3080 | } |
Yan, Zheng | 121f22a | 2016-07-04 22:05:18 +0800 | [diff] [blame] | 3081 | |
| 3082 | if (recon_state->msg_version >= 3) { |
| 3083 | /* version, compat_version and struct_len */ |
| 3084 | total_len = 2 * sizeof(u8) + sizeof(u32); |
Yan, Zheng | 3469ed0 | 2016-07-05 09:32:31 +0800 | [diff] [blame] | 3085 | struct_v = 2; |
Yan, Zheng | 121f22a | 2016-07-04 22:05:18 +0800 | [diff] [blame] | 3086 | } |
Jim Schutt | 39be95e | 2013-05-15 13:03:35 -0500 | [diff] [blame] | 3087 | /* |
| 3088 | * number of encoded locks is stable, so copy to pagelist |
| 3089 | */ |
Yan, Zheng | 121f22a | 2016-07-04 22:05:18 +0800 | [diff] [blame] | 3090 | struct_len = 2 * sizeof(u32) + |
| 3091 | (num_fcntl_locks + num_flock_locks) * |
| 3092 | sizeof(struct ceph_filelock); |
| 3093 | rec.v2.flock_len = cpu_to_le32(struct_len); |
| 3094 | |
Yan, Zheng | 5ccedf1 | 2018-12-13 16:34:11 +0800 | [diff] [blame] | 3095 | struct_len += sizeof(u32) + sizeof(rec.v2); |
Yan, Zheng | 121f22a | 2016-07-04 22:05:18 +0800 | [diff] [blame] | 3096 | |
Yan, Zheng | 3469ed0 | 2016-07-05 09:32:31 +0800 | [diff] [blame] | 3097 | if (struct_v >= 2) |
| 3098 | struct_len += sizeof(u64); /* snap_follows */ |
| 3099 | |
Yan, Zheng | 121f22a | 2016-07-04 22:05:18 +0800 | [diff] [blame] | 3100 | total_len += struct_len; |
| 3101 | err = ceph_pagelist_reserve(pagelist, total_len); |
Yan, Zheng | 5ccedf1 | 2018-12-13 16:34:11 +0800 | [diff] [blame] | 3102 | if (err) { |
| 3103 | kfree(flocks); |
| 3104 | goto out_err; |
Yan, Zheng | 121f22a | 2016-07-04 22:05:18 +0800 | [diff] [blame] | 3105 | } |
Yan, Zheng | 5ccedf1 | 2018-12-13 16:34:11 +0800 | [diff] [blame] | 3106 | |
| 3107 | if (recon_state->msg_version >= 3) { |
| 3108 | ceph_pagelist_encode_8(pagelist, struct_v); |
| 3109 | ceph_pagelist_encode_8(pagelist, 1); |
| 3110 | ceph_pagelist_encode_32(pagelist, struct_len); |
| 3111 | } |
| 3112 | ceph_pagelist_encode_string(pagelist, NULL, 0); |
| 3113 | ceph_pagelist_append(pagelist, &rec, sizeof(rec.v2)); |
| 3114 | ceph_locks_to_pagelist(flocks, pagelist, |
| 3115 | num_fcntl_locks, num_flock_locks); |
| 3116 | if (struct_v >= 2) |
| 3117 | ceph_pagelist_encode_64(pagelist, snap_follows); |
| 3118 | |
Jim Schutt | 39be95e | 2013-05-15 13:03:35 -0500 | [diff] [blame] | 3119 | kfree(flocks); |
Sage Weil | 3612abb | 2010-09-07 15:59:27 -0700 | [diff] [blame] | 3120 | } else { |
Yan, Zheng | 5ccedf1 | 2018-12-13 16:34:11 +0800 | [diff] [blame] | 3121 | u64 pathbase = 0; |
| 3122 | int pathlen = 0; |
| 3123 | char *path = NULL; |
| 3124 | struct dentry *dentry; |
| 3125 | |
| 3126 | dentry = d_find_alias(inode); |
| 3127 | if (dentry) { |
| 3128 | path = ceph_mdsc_build_path(dentry, |
| 3129 | &pathlen, &pathbase, 0); |
| 3130 | dput(dentry); |
| 3131 | if (IS_ERR(path)) { |
| 3132 | err = PTR_ERR(path); |
| 3133 | goto out_err; |
| 3134 | } |
| 3135 | rec.v1.pathbase = cpu_to_le64(pathbase); |
Yan, Zheng | 121f22a | 2016-07-04 22:05:18 +0800 | [diff] [blame] | 3136 | } |
Yan, Zheng | 5ccedf1 | 2018-12-13 16:34:11 +0800 | [diff] [blame] | 3137 | |
| 3138 | err = ceph_pagelist_reserve(pagelist, |
| 3139 | pathlen + sizeof(u32) + sizeof(rec.v1)); |
| 3140 | if (err) { |
| 3141 | kfree(path); |
| 3142 | goto out_err; |
| 3143 | } |
| 3144 | |
| 3145 | ceph_pagelist_encode_string(pagelist, path, pathlen); |
| 3146 | ceph_pagelist_append(pagelist, &rec, sizeof(rec.v1)); |
| 3147 | |
| 3148 | kfree(path); |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 3149 | } |
Yan, Zheng | 44c9975 | 2013-09-22 10:28:10 +0800 | [diff] [blame] | 3150 | |
| 3151 | recon_state->nr_caps++; |
Yan, Zheng | 5ccedf1 | 2018-12-13 16:34:11 +0800 | [diff] [blame] | 3152 | out_err: |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 3153 | return err; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3154 | } |
| 3155 | |
| 3156 | |
| 3157 | /* |
| 3158 | * If an MDS fails and recovers, clients need to reconnect in order to |
| 3159 | * reestablish shared state. This includes all caps issued through |
| 3160 | * this session _and_ the snap_realm hierarchy. Because it's not |
| 3161 | * clear which snap realms the mds cares about, we send everything we |
| 3162 | * know about.. that ensures we'll then get any new info the |
| 3163 | * recovering MDS might have. |
| 3164 | * |
| 3165 | * This is a relatively heavyweight operation, but it's rare. |
| 3166 | * |
| 3167 | * called with mdsc->mutex held. |
| 3168 | */ |
Sage Weil | 34b6c85 | 2010-05-10 16:31:25 -0700 | [diff] [blame] | 3169 | static void send_mds_reconnect(struct ceph_mds_client *mdsc, |
| 3170 | struct ceph_mds_session *session) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3171 | { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3172 | struct ceph_msg *reply; |
Sage Weil | a105f00 | 2010-02-15 14:37:55 -0800 | [diff] [blame] | 3173 | struct rb_node *p; |
Sage Weil | 34b6c85 | 2010-05-10 16:31:25 -0700 | [diff] [blame] | 3174 | int mds = session->s_mds; |
Sage Weil | 9abf82b | 2010-05-10 21:58:38 -0700 | [diff] [blame] | 3175 | int err = -ENOMEM; |
Yan, Zheng | 44c9975 | 2013-09-22 10:28:10 +0800 | [diff] [blame] | 3176 | int s_nr_caps; |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 3177 | struct ceph_pagelist *pagelist; |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 3178 | struct ceph_reconnect_state recon_state; |
Jeff Layton | c8a96a3 | 2017-10-19 08:53:58 -0400 | [diff] [blame] | 3179 | LIST_HEAD(dispose); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3180 | |
Sage Weil | 34b6c85 | 2010-05-10 16:31:25 -0700 | [diff] [blame] | 3181 | pr_info("mds%d reconnect start\n", mds); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3182 | |
Ilya Dryomov | 33165d4 | 2018-09-28 15:38:34 +0200 | [diff] [blame] | 3183 | pagelist = ceph_pagelist_alloc(GFP_NOFS); |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 3184 | if (!pagelist) |
| 3185 | goto fail_nopagelist; |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 3186 | |
Ilya Dryomov | 0d9c1ab | 2018-10-15 17:38:23 +0200 | [diff] [blame] | 3187 | reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 3188 | if (!reply) |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 3189 | goto fail_nomsg; |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 3190 | |
Sage Weil | 34b6c85 | 2010-05-10 16:31:25 -0700 | [diff] [blame] | 3191 | mutex_lock(&session->s_mutex); |
| 3192 | session->s_state = CEPH_MDS_SESSION_RECONNECTING; |
| 3193 | session->s_seq = 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3194 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3195 | dout("session %p state %s\n", session, |
John Spray | a687eca | 2014-09-19 13:51:08 +0100 | [diff] [blame] | 3196 | ceph_session_state_name(session->s_state)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3197 | |
Yan, Zheng | 99a9c27 | 2013-09-22 11:08:14 +0800 | [diff] [blame] | 3198 | spin_lock(&session->s_gen_ttl_lock); |
| 3199 | session->s_cap_gen++; |
| 3200 | spin_unlock(&session->s_gen_ttl_lock); |
| 3201 | |
| 3202 | spin_lock(&session->s_cap_lock); |
Yan, Zheng | 03f4fcb | 2015-01-05 11:04:04 +0800 | [diff] [blame] | 3203 | /* don't know if session is readonly */ |
| 3204 | session->s_readonly = 0; |
Yan, Zheng | 99a9c27 | 2013-09-22 11:08:14 +0800 | [diff] [blame] | 3205 | /* |
| 3206 | * notify __ceph_remove_cap() that we are composing cap reconnect. |
| 3207 | * If a cap get released before being added to the cap reconnect, |
| 3208 | * __ceph_remove_cap() should skip queuing cap release. |
| 3209 | */ |
| 3210 | session->s_cap_reconnect = 1; |
Sage Weil | e01a594 | 2010-05-10 15:36:44 -0700 | [diff] [blame] | 3211 | /* drop old cap expires; we're about to reestablish that state */ |
Jeff Layton | c8a96a3 | 2017-10-19 08:53:58 -0400 | [diff] [blame] | 3212 | detach_cap_releases(session, &dispose); |
| 3213 | spin_unlock(&session->s_cap_lock); |
| 3214 | dispose_cap_releases(mdsc, &dispose); |
Sage Weil | e01a594 | 2010-05-10 15:36:44 -0700 | [diff] [blame] | 3215 | |
Yan, Zheng | 5d23371 | 2014-09-10 16:56:23 +0800 | [diff] [blame] | 3216 | /* trim unused caps to reduce MDS's cache rejoin time */ |
Yan, Zheng | c0bd50e | 2015-04-07 15:51:08 +0800 | [diff] [blame] | 3217 | if (mdsc->fsc->sb->s_root) |
| 3218 | shrink_dcache_parent(mdsc->fsc->sb->s_root); |
Yan, Zheng | 5d23371 | 2014-09-10 16:56:23 +0800 | [diff] [blame] | 3219 | |
| 3220 | ceph_con_close(&session->s_con); |
| 3221 | ceph_con_open(&session->s_con, |
| 3222 | CEPH_ENTITY_TYPE_MDS, mds, |
| 3223 | ceph_mdsmap_get_addr(mdsc->mdsmap, mds)); |
| 3224 | |
| 3225 | /* replay unsafe requests */ |
| 3226 | replay_unsafe_requests(mdsc, session); |
| 3227 | |
| 3228 | down_read(&mdsc->snap_rwsem); |
| 3229 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3230 | /* traverse this session's caps */ |
Yan, Zheng | 44c9975 | 2013-09-22 10:28:10 +0800 | [diff] [blame] | 3231 | s_nr_caps = session->s_nr_caps; |
| 3232 | err = ceph_pagelist_encode_32(pagelist, s_nr_caps); |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 3233 | if (err) |
| 3234 | goto fail; |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 3235 | |
Yan, Zheng | 44c9975 | 2013-09-22 10:28:10 +0800 | [diff] [blame] | 3236 | recon_state.nr_caps = 0; |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 3237 | recon_state.pagelist = pagelist; |
Yan, Zheng | 121f22a | 2016-07-04 22:05:18 +0800 | [diff] [blame] | 3238 | if (session->s_con.peer_features & CEPH_FEATURE_MDSENC) |
| 3239 | recon_state.msg_version = 3; |
Yan, Zheng | 121f22a | 2016-07-04 22:05:18 +0800 | [diff] [blame] | 3240 | else |
Ilya Dryomov | 23c625c | 2018-11-08 14:55:21 +0100 | [diff] [blame] | 3241 | recon_state.msg_version = 2; |
Sage Weil | 20cb34a | 2010-05-12 15:21:32 -0700 | [diff] [blame] | 3242 | err = iterate_session_caps(session, encode_caps_cb, &recon_state); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3243 | if (err < 0) |
Sage Weil | 9abf82b | 2010-05-10 21:58:38 -0700 | [diff] [blame] | 3244 | goto fail; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3245 | |
Yan, Zheng | 99a9c27 | 2013-09-22 11:08:14 +0800 | [diff] [blame] | 3246 | spin_lock(&session->s_cap_lock); |
| 3247 | session->s_cap_reconnect = 0; |
| 3248 | spin_unlock(&session->s_cap_lock); |
| 3249 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3250 | /* |
| 3251 | * snaprealms. we provide mds with the ino, seq (version), and |
| 3252 | * parent for all of our realms. If the mds has any newer info, |
| 3253 | * it will tell us. |
| 3254 | */ |
Sage Weil | a105f00 | 2010-02-15 14:37:55 -0800 | [diff] [blame] | 3255 | for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) { |
| 3256 | struct ceph_snap_realm *realm = |
| 3257 | rb_entry(p, struct ceph_snap_realm, node); |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 3258 | struct ceph_mds_snaprealm_reconnect sr_rec; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3259 | |
| 3260 | dout(" adding snap realm %llx seq %lld parent %llx\n", |
| 3261 | realm->ino, realm->seq, realm->parent_ino); |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 3262 | sr_rec.ino = cpu_to_le64(realm->ino); |
| 3263 | sr_rec.seq = cpu_to_le64(realm->seq); |
| 3264 | sr_rec.parent = cpu_to_le64(realm->parent_ino); |
| 3265 | err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec)); |
| 3266 | if (err) |
| 3267 | goto fail; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3268 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3269 | |
Yan, Zheng | 121f22a | 2016-07-04 22:05:18 +0800 | [diff] [blame] | 3270 | reply->hdr.version = cpu_to_le16(recon_state.msg_version); |
Yan, Zheng | 44c9975 | 2013-09-22 10:28:10 +0800 | [diff] [blame] | 3271 | |
| 3272 | /* raced with cap release? */ |
| 3273 | if (s_nr_caps != recon_state.nr_caps) { |
| 3274 | struct page *page = list_first_entry(&pagelist->head, |
| 3275 | struct page, lru); |
| 3276 | __le32 *addr = kmap_atomic(page); |
| 3277 | *addr = cpu_to_le32(recon_state.nr_caps); |
| 3278 | kunmap_atomic(addr); |
Alex Elder | ebf18f4 | 2013-03-04 22:29:57 -0600 | [diff] [blame] | 3279 | } |
Yan, Zheng | 44c9975 | 2013-09-22 10:28:10 +0800 | [diff] [blame] | 3280 | |
| 3281 | reply->hdr.data_len = cpu_to_le32(pagelist->length); |
| 3282 | ceph_msg_data_add_pagelist(reply, pagelist); |
Yan, Zheng | e548e9b | 2015-06-10 15:17:56 +0800 | [diff] [blame] | 3283 | |
| 3284 | ceph_early_kick_flushing_caps(mdsc, session); |
| 3285 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3286 | ceph_con_send(&session->s_con, reply); |
| 3287 | |
Sage Weil | 9abf82b | 2010-05-10 21:58:38 -0700 | [diff] [blame] | 3288 | mutex_unlock(&session->s_mutex); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3289 | |
Sage Weil | 9abf82b | 2010-05-10 21:58:38 -0700 | [diff] [blame] | 3290 | mutex_lock(&mdsc->mutex); |
| 3291 | __wake_requests(mdsc, &session->s_waiting); |
| 3292 | mutex_unlock(&mdsc->mutex); |
| 3293 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3294 | up_read(&mdsc->snap_rwsem); |
Ilya Dryomov | 89486833 | 2018-09-28 16:02:53 +0200 | [diff] [blame] | 3295 | ceph_pagelist_release(pagelist); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3296 | return; |
| 3297 | |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 3298 | fail: |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3299 | ceph_msg_put(reply); |
Sage Weil | 9abf82b | 2010-05-10 21:58:38 -0700 | [diff] [blame] | 3300 | up_read(&mdsc->snap_rwsem); |
| 3301 | mutex_unlock(&session->s_mutex); |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 3302 | fail_nomsg: |
| 3303 | ceph_pagelist_release(pagelist); |
Sage Weil | 93cea5b | 2009-12-23 12:21:51 -0800 | [diff] [blame] | 3304 | fail_nopagelist: |
Sage Weil | 9abf82b | 2010-05-10 21:58:38 -0700 | [diff] [blame] | 3305 | pr_err("error %d preparing reconnect for mds%d\n", err, mds); |
Sage Weil | 9abf82b | 2010-05-10 21:58:38 -0700 | [diff] [blame] | 3306 | return; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3307 | } |
| 3308 | |
| 3309 | |
| 3310 | /* |
| 3311 | * compare old and new mdsmaps, kicking requests |
| 3312 | * and closing out old connections as necessary |
| 3313 | * |
| 3314 | * called under mdsc->mutex. |
| 3315 | */ |
| 3316 | static void check_new_map(struct ceph_mds_client *mdsc, |
| 3317 | struct ceph_mdsmap *newmap, |
| 3318 | struct ceph_mdsmap *oldmap) |
| 3319 | { |
| 3320 | int i; |
| 3321 | int oldstate, newstate; |
| 3322 | struct ceph_mds_session *s; |
| 3323 | |
| 3324 | dout("check_new_map new %u old %u\n", |
| 3325 | newmap->m_epoch, oldmap->m_epoch); |
| 3326 | |
Yan, Zheng | 76201b6 | 2017-03-28 17:04:13 +0800 | [diff] [blame] | 3327 | for (i = 0; i < oldmap->m_num_mds && i < mdsc->max_sessions; i++) { |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 3328 | if (!mdsc->sessions[i]) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3329 | continue; |
| 3330 | s = mdsc->sessions[i]; |
| 3331 | oldstate = ceph_mdsmap_get_state(oldmap, i); |
| 3332 | newstate = ceph_mdsmap_get_state(newmap, i); |
| 3333 | |
Sage Weil | 0deb01c | 2010-06-17 14:19:01 -0700 | [diff] [blame] | 3334 | dout("check_new_map mds%d state %s%s -> %s%s (session %s)\n", |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3335 | i, ceph_mds_state_name(oldstate), |
Sage Weil | 0deb01c | 2010-06-17 14:19:01 -0700 | [diff] [blame] | 3336 | ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "", |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3337 | ceph_mds_state_name(newstate), |
Sage Weil | 0deb01c | 2010-06-17 14:19:01 -0700 | [diff] [blame] | 3338 | ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "", |
John Spray | a687eca | 2014-09-19 13:51:08 +0100 | [diff] [blame] | 3339 | ceph_session_state_name(s->s_state)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3340 | |
Yan, Zheng | 76201b6 | 2017-03-28 17:04:13 +0800 | [diff] [blame] | 3341 | if (i >= newmap->m_num_mds || |
Yan, Zheng | 3e8f43a | 2012-09-20 17:42:25 +0800 | [diff] [blame] | 3342 | memcmp(ceph_mdsmap_get_addr(oldmap, i), |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3343 | ceph_mdsmap_get_addr(newmap, i), |
| 3344 | sizeof(struct ceph_entity_addr))) { |
| 3345 | if (s->s_state == CEPH_MDS_SESSION_OPENING) { |
| 3346 | /* the session never opened, just close it |
| 3347 | * out now */ |
Yan, Zheng | 0a07fc8 | 2017-03-29 15:30:24 +0800 | [diff] [blame] | 3348 | get_session(s); |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 3349 | __unregister_session(mdsc, s); |
Yan, Zheng | 0a07fc8 | 2017-03-29 15:30:24 +0800 | [diff] [blame] | 3350 | __wake_requests(mdsc, &s->s_waiting); |
| 3351 | ceph_put_mds_session(s); |
Yan, Zheng | 2827528 | 2017-03-28 17:56:29 +0800 | [diff] [blame] | 3352 | } else if (i >= newmap->m_num_mds) { |
| 3353 | /* force close session for stopped mds */ |
| 3354 | get_session(s); |
| 3355 | __unregister_session(mdsc, s); |
| 3356 | __wake_requests(mdsc, &s->s_waiting); |
| 3357 | kick_requests(mdsc, i); |
| 3358 | mutex_unlock(&mdsc->mutex); |
| 3359 | |
| 3360 | mutex_lock(&s->s_mutex); |
| 3361 | cleanup_session_requests(mdsc, s); |
| 3362 | remove_session_caps(s); |
| 3363 | mutex_unlock(&s->s_mutex); |
| 3364 | |
| 3365 | ceph_put_mds_session(s); |
| 3366 | |
| 3367 | mutex_lock(&mdsc->mutex); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3368 | } else { |
| 3369 | /* just close it */ |
| 3370 | mutex_unlock(&mdsc->mutex); |
| 3371 | mutex_lock(&s->s_mutex); |
| 3372 | mutex_lock(&mdsc->mutex); |
| 3373 | ceph_con_close(&s->s_con); |
| 3374 | mutex_unlock(&s->s_mutex); |
| 3375 | s->s_state = CEPH_MDS_SESSION_RESTARTING; |
| 3376 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3377 | } else if (oldstate == newstate) { |
| 3378 | continue; /* nothing new with this mds */ |
| 3379 | } |
| 3380 | |
| 3381 | /* |
| 3382 | * send reconnect? |
| 3383 | */ |
| 3384 | if (s->s_state == CEPH_MDS_SESSION_RESTARTING && |
Sage Weil | 34b6c85 | 2010-05-10 16:31:25 -0700 | [diff] [blame] | 3385 | newstate >= CEPH_MDS_STATE_RECONNECT) { |
| 3386 | mutex_unlock(&mdsc->mutex); |
| 3387 | send_mds_reconnect(mdsc, s); |
| 3388 | mutex_lock(&mdsc->mutex); |
| 3389 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3390 | |
| 3391 | /* |
Sage Weil | 29790f2 | 2010-03-18 14:45:05 -0700 | [diff] [blame] | 3392 | * kick request on any mds that has gone active. |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3393 | */ |
| 3394 | if (oldstate < CEPH_MDS_STATE_ACTIVE && |
| 3395 | newstate >= CEPH_MDS_STATE_ACTIVE) { |
Sage Weil | 29790f2 | 2010-03-18 14:45:05 -0700 | [diff] [blame] | 3396 | if (oldstate != CEPH_MDS_STATE_CREATING && |
| 3397 | oldstate != CEPH_MDS_STATE_STARTING) |
| 3398 | pr_info("mds%d recovery completed\n", s->s_mds); |
| 3399 | kick_requests(mdsc, i); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3400 | ceph_kick_flushing_caps(mdsc, s); |
Yan, Zheng | d2f8bb2 | 2018-12-10 16:35:09 +0800 | [diff] [blame] | 3401 | wake_up_session_caps(s, RECONNECT); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3402 | } |
| 3403 | } |
Sage Weil | cb170a2 | 2010-06-21 13:38:35 -0700 | [diff] [blame] | 3404 | |
Yan, Zheng | 76201b6 | 2017-03-28 17:04:13 +0800 | [diff] [blame] | 3405 | for (i = 0; i < newmap->m_num_mds && i < mdsc->max_sessions; i++) { |
Sage Weil | cb170a2 | 2010-06-21 13:38:35 -0700 | [diff] [blame] | 3406 | s = mdsc->sessions[i]; |
| 3407 | if (!s) |
| 3408 | continue; |
| 3409 | if (!ceph_mdsmap_is_laggy(newmap, i)) |
| 3410 | continue; |
| 3411 | if (s->s_state == CEPH_MDS_SESSION_OPEN || |
| 3412 | s->s_state == CEPH_MDS_SESSION_HUNG || |
| 3413 | s->s_state == CEPH_MDS_SESSION_CLOSING) { |
| 3414 | dout(" connecting to export targets of laggy mds%d\n", |
| 3415 | i); |
| 3416 | __open_export_target_sessions(mdsc, s); |
| 3417 | } |
| 3418 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3419 | } |
| 3420 | |
| 3421 | |
| 3422 | |
| 3423 | /* |
| 3424 | * leases |
| 3425 | */ |
| 3426 | |
| 3427 | /* |
| 3428 | * caller must hold session s_mutex, dentry->d_lock |
| 3429 | */ |
| 3430 | void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry) |
| 3431 | { |
| 3432 | struct ceph_dentry_info *di = ceph_dentry(dentry); |
| 3433 | |
| 3434 | ceph_put_mds_session(di->lease_session); |
| 3435 | di->lease_session = NULL; |
| 3436 | } |
| 3437 | |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 3438 | static void handle_lease(struct ceph_mds_client *mdsc, |
| 3439 | struct ceph_mds_session *session, |
| 3440 | struct ceph_msg *msg) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3441 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3442 | struct super_block *sb = mdsc->fsc->sb; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3443 | struct inode *inode; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3444 | struct dentry *parent, *dentry; |
| 3445 | struct ceph_dentry_info *di; |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 3446 | int mds = session->s_mds; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3447 | struct ceph_mds_lease *h = msg->front.iov_base; |
Sage Weil | 1e5ea23 | 2010-06-04 10:05:40 -0700 | [diff] [blame] | 3448 | u32 seq; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3449 | struct ceph_vino vino; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3450 | struct qstr dname; |
| 3451 | int release = 0; |
| 3452 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3453 | dout("handle_lease from mds%d\n", mds); |
| 3454 | |
| 3455 | /* decode */ |
| 3456 | if (msg->front.iov_len < sizeof(*h) + sizeof(u32)) |
| 3457 | goto bad; |
| 3458 | vino.ino = le64_to_cpu(h->ino); |
| 3459 | vino.snap = CEPH_NOSNAP; |
Sage Weil | 1e5ea23 | 2010-06-04 10:05:40 -0700 | [diff] [blame] | 3460 | seq = le32_to_cpu(h->seq); |
Yan, Zheng | 0fcf6c0 | 2018-08-03 16:24:49 +0800 | [diff] [blame] | 3461 | dname.len = get_unaligned_le32(h + 1); |
| 3462 | if (msg->front.iov_len < sizeof(*h) + sizeof(u32) + dname.len) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3463 | goto bad; |
Yan, Zheng | 0fcf6c0 | 2018-08-03 16:24:49 +0800 | [diff] [blame] | 3464 | dname.name = (void *)(h + 1) + sizeof(u32); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3465 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3466 | /* lookup inode */ |
| 3467 | inode = ceph_find_inode(sb, vino); |
Sage Weil | 2f90b85 | 2011-07-26 11:28:25 -0700 | [diff] [blame] | 3468 | dout("handle_lease %s, ino %llx %p %.*s\n", |
| 3469 | ceph_lease_op_name(h->action), vino.ino, inode, |
Sage Weil | 1e5ea23 | 2010-06-04 10:05:40 -0700 | [diff] [blame] | 3470 | dname.len, dname.name); |
Yan, Zheng | 6cd3bca | 2014-09-17 07:45:12 +0800 | [diff] [blame] | 3471 | |
| 3472 | mutex_lock(&session->s_mutex); |
| 3473 | session->s_seq++; |
| 3474 | |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 3475 | if (!inode) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3476 | dout("handle_lease no inode %llx\n", vino.ino); |
| 3477 | goto release; |
| 3478 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3479 | |
| 3480 | /* dentry */ |
| 3481 | parent = d_find_alias(inode); |
| 3482 | if (!parent) { |
| 3483 | dout("no parent dentry on inode %p\n", inode); |
| 3484 | WARN_ON(1); |
| 3485 | goto release; /* hrm... */ |
| 3486 | } |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 3487 | dname.hash = full_name_hash(parent, dname.name, dname.len); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3488 | dentry = d_lookup(parent, &dname); |
| 3489 | dput(parent); |
| 3490 | if (!dentry) |
| 3491 | goto release; |
| 3492 | |
| 3493 | spin_lock(&dentry->d_lock); |
| 3494 | di = ceph_dentry(dentry); |
| 3495 | switch (h->action) { |
| 3496 | case CEPH_MDS_LEASE_REVOKE: |
Sage Weil | 3d8eb7a | 2011-11-11 09:48:53 -0800 | [diff] [blame] | 3497 | if (di->lease_session == session) { |
Sage Weil | 1e5ea23 | 2010-06-04 10:05:40 -0700 | [diff] [blame] | 3498 | if (ceph_seq_cmp(di->lease_seq, seq) > 0) |
| 3499 | h->seq = cpu_to_le32(di->lease_seq); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3500 | __ceph_mdsc_drop_dentry_lease(dentry); |
| 3501 | } |
| 3502 | release = 1; |
| 3503 | break; |
| 3504 | |
| 3505 | case CEPH_MDS_LEASE_RENEW: |
Sage Weil | 3d8eb7a | 2011-11-11 09:48:53 -0800 | [diff] [blame] | 3506 | if (di->lease_session == session && |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3507 | di->lease_gen == session->s_cap_gen && |
| 3508 | di->lease_renew_from && |
| 3509 | di->lease_renew_after == 0) { |
| 3510 | unsigned long duration = |
Nicholas Mc Guire | 3563dbdd | 2015-02-06 06:52:17 -0500 | [diff] [blame] | 3511 | msecs_to_jiffies(le32_to_cpu(h->duration_ms)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3512 | |
Sage Weil | 1e5ea23 | 2010-06-04 10:05:40 -0700 | [diff] [blame] | 3513 | di->lease_seq = seq; |
Miklos Szeredi | 9b16f03c | 2016-06-22 16:35:04 +0200 | [diff] [blame] | 3514 | di->time = di->lease_renew_from + duration; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3515 | di->lease_renew_after = di->lease_renew_from + |
| 3516 | (duration >> 1); |
| 3517 | di->lease_renew_from = 0; |
| 3518 | } |
| 3519 | break; |
| 3520 | } |
| 3521 | spin_unlock(&dentry->d_lock); |
| 3522 | dput(dentry); |
| 3523 | |
| 3524 | if (!release) |
| 3525 | goto out; |
| 3526 | |
| 3527 | release: |
| 3528 | /* let's just reuse the same message */ |
| 3529 | h->action = CEPH_MDS_LEASE_REVOKE_ACK; |
| 3530 | ceph_msg_get(msg); |
| 3531 | ceph_con_send(&session->s_con, msg); |
| 3532 | |
| 3533 | out: |
| 3534 | iput(inode); |
| 3535 | mutex_unlock(&session->s_mutex); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3536 | return; |
| 3537 | |
| 3538 | bad: |
| 3539 | pr_err("corrupt lease message\n"); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 3540 | ceph_msg_dump(msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3541 | } |
| 3542 | |
| 3543 | void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session, |
| 3544 | struct inode *inode, |
| 3545 | struct dentry *dentry, char action, |
| 3546 | u32 seq) |
| 3547 | { |
| 3548 | struct ceph_msg *msg; |
| 3549 | struct ceph_mds_lease *lease; |
| 3550 | int len = sizeof(*lease) + sizeof(u32); |
| 3551 | int dnamelen = 0; |
| 3552 | |
| 3553 | dout("lease_send_msg inode %p dentry %p %s to mds%d\n", |
| 3554 | inode, dentry, ceph_lease_op_name(action), session->s_mds); |
| 3555 | dnamelen = dentry->d_name.len; |
| 3556 | len += dnamelen; |
| 3557 | |
Sage Weil | b61c276 | 2011-08-09 15:03:46 -0700 | [diff] [blame] | 3558 | msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 3559 | if (!msg) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3560 | return; |
| 3561 | lease = msg->front.iov_base; |
| 3562 | lease->action = action; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3563 | lease->ino = cpu_to_le64(ceph_vino(inode).ino); |
| 3564 | lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap); |
| 3565 | lease->seq = cpu_to_le32(seq); |
| 3566 | put_unaligned_le32(dnamelen, lease + 1); |
| 3567 | memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen); |
| 3568 | |
| 3569 | /* |
| 3570 | * if this is a preemptive lease RELEASE, no need to |
| 3571 | * flush request stream, since the actual request will |
| 3572 | * soon follow. |
| 3573 | */ |
| 3574 | msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE); |
| 3575 | |
| 3576 | ceph_con_send(&session->s_con, msg); |
| 3577 | } |
| 3578 | |
| 3579 | /* |
Yan, Zheng | 7aac453 | 2018-03-13 19:52:20 +0800 | [diff] [blame] | 3580 | * lock unlock sessions, to wait ongoing session activities |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3581 | */ |
Yan, Zheng | 7aac453 | 2018-03-13 19:52:20 +0800 | [diff] [blame] | 3582 | static void lock_unlock_sessions(struct ceph_mds_client *mdsc) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3583 | { |
| 3584 | int i; |
| 3585 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3586 | mutex_lock(&mdsc->mutex); |
| 3587 | for (i = 0; i < mdsc->max_sessions; i++) { |
| 3588 | struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i); |
| 3589 | if (!s) |
| 3590 | continue; |
| 3591 | mutex_unlock(&mdsc->mutex); |
| 3592 | mutex_lock(&s->s_mutex); |
| 3593 | mutex_unlock(&s->s_mutex); |
| 3594 | ceph_put_mds_session(s); |
| 3595 | mutex_lock(&mdsc->mutex); |
| 3596 | } |
| 3597 | mutex_unlock(&mdsc->mutex); |
| 3598 | } |
| 3599 | |
| 3600 | |
| 3601 | |
| 3602 | /* |
| 3603 | * delayed work -- periodically trim expired leases, renew caps with mds |
| 3604 | */ |
| 3605 | static void schedule_delayed(struct ceph_mds_client *mdsc) |
| 3606 | { |
| 3607 | int delay = 5; |
| 3608 | unsigned hz = round_jiffies_relative(HZ * delay); |
| 3609 | schedule_delayed_work(&mdsc->delayed_work, hz); |
| 3610 | } |
| 3611 | |
| 3612 | static void delayed_work(struct work_struct *work) |
| 3613 | { |
| 3614 | int i; |
| 3615 | struct ceph_mds_client *mdsc = |
| 3616 | container_of(work, struct ceph_mds_client, delayed_work.work); |
| 3617 | int renew_interval; |
| 3618 | int renew_caps; |
| 3619 | |
| 3620 | dout("mdsc delayed_work\n"); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 3621 | ceph_check_delayed_caps(mdsc); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3622 | |
| 3623 | mutex_lock(&mdsc->mutex); |
| 3624 | renew_interval = mdsc->mdsmap->m_session_timeout >> 2; |
| 3625 | renew_caps = time_after_eq(jiffies, HZ*renew_interval + |
| 3626 | mdsc->last_renew_caps); |
| 3627 | if (renew_caps) |
| 3628 | mdsc->last_renew_caps = jiffies; |
| 3629 | |
| 3630 | for (i = 0; i < mdsc->max_sessions; i++) { |
| 3631 | struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i); |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 3632 | if (!s) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3633 | continue; |
| 3634 | if (s->s_state == CEPH_MDS_SESSION_CLOSING) { |
| 3635 | dout("resending session close request for mds%d\n", |
| 3636 | s->s_mds); |
| 3637 | request_close_session(mdsc, s); |
| 3638 | ceph_put_mds_session(s); |
| 3639 | continue; |
| 3640 | } |
| 3641 | if (s->s_ttl && time_after(jiffies, s->s_ttl)) { |
| 3642 | if (s->s_state == CEPH_MDS_SESSION_OPEN) { |
| 3643 | s->s_state = CEPH_MDS_SESSION_HUNG; |
| 3644 | pr_info("mds%d hung\n", s->s_mds); |
| 3645 | } |
| 3646 | } |
| 3647 | if (s->s_state < CEPH_MDS_SESSION_OPEN) { |
| 3648 | /* this mds is failed or recovering, just wait */ |
| 3649 | ceph_put_mds_session(s); |
| 3650 | continue; |
| 3651 | } |
| 3652 | mutex_unlock(&mdsc->mutex); |
| 3653 | |
| 3654 | mutex_lock(&s->s_mutex); |
| 3655 | if (renew_caps) |
| 3656 | send_renew_caps(mdsc, s); |
| 3657 | else |
| 3658 | ceph_con_keepalive(&s->s_con); |
Sage Weil | aab53dd | 2010-03-17 16:30:21 -0700 | [diff] [blame] | 3659 | if (s->s_state == CEPH_MDS_SESSION_OPEN || |
| 3660 | s->s_state == CEPH_MDS_SESSION_HUNG) |
Sage Weil | 3d7ded4 | 2010-06-09 16:47:10 -0700 | [diff] [blame] | 3661 | ceph_send_cap_releases(mdsc, s); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3662 | mutex_unlock(&s->s_mutex); |
| 3663 | ceph_put_mds_session(s); |
| 3664 | |
| 3665 | mutex_lock(&mdsc->mutex); |
| 3666 | } |
| 3667 | mutex_unlock(&mdsc->mutex); |
| 3668 | |
| 3669 | schedule_delayed(mdsc); |
| 3670 | } |
| 3671 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3672 | int ceph_mdsc_init(struct ceph_fs_client *fsc) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3673 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3674 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3675 | struct ceph_mds_client *mdsc; |
| 3676 | |
| 3677 | mdsc = kzalloc(sizeof(struct ceph_mds_client), GFP_NOFS); |
| 3678 | if (!mdsc) |
| 3679 | return -ENOMEM; |
| 3680 | mdsc->fsc = fsc; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3681 | mutex_init(&mdsc->mutex); |
| 3682 | mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS); |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 3683 | if (!mdsc->mdsmap) { |
majianpeng | fb3101b | 2013-06-25 14:48:19 +0800 | [diff] [blame] | 3684 | kfree(mdsc); |
Cheng Renquan | 2d06eeb | 2010-03-26 18:04:40 +0800 | [diff] [blame] | 3685 | return -ENOMEM; |
majianpeng | fb3101b | 2013-06-25 14:48:19 +0800 | [diff] [blame] | 3686 | } |
Cheng Renquan | 2d06eeb | 2010-03-26 18:04:40 +0800 | [diff] [blame] | 3687 | |
Chengguang Xu | 50c55ae | 2018-03-14 13:47:33 +0800 | [diff] [blame] | 3688 | fsc->mdsc = mdsc; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3689 | init_completion(&mdsc->safe_umount_waiters); |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 3690 | init_waitqueue_head(&mdsc->session_close_wq); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3691 | INIT_LIST_HEAD(&mdsc->waiting_for_map); |
| 3692 | mdsc->sessions = NULL; |
Yan, Zheng | 86d8f67 | 2015-01-09 17:00:42 +0800 | [diff] [blame] | 3693 | atomic_set(&mdsc->num_sessions, 0); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3694 | mdsc->max_sessions = 0; |
| 3695 | mdsc->stopping = 0; |
Luis Henriques | d557c48 | 2018-01-12 17:19:29 +0000 | [diff] [blame] | 3696 | atomic64_set(&mdsc->quotarealms_count, 0); |
Yan, Zheng | affbc19 | 2015-05-05 21:22:13 +0800 | [diff] [blame] | 3697 | mdsc->last_snap_seq = 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3698 | init_rwsem(&mdsc->snap_rwsem); |
Sage Weil | a105f00 | 2010-02-15 14:37:55 -0800 | [diff] [blame] | 3699 | mdsc->snap_realms = RB_ROOT; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3700 | INIT_LIST_HEAD(&mdsc->snap_empty); |
| 3701 | spin_lock_init(&mdsc->snap_empty_lock); |
| 3702 | mdsc->last_tid = 0; |
Yan, Zheng | e8a7b8b | 2015-05-19 18:54:40 +0800 | [diff] [blame] | 3703 | mdsc->oldest_tid = 0; |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 3704 | mdsc->request_tree = RB_ROOT; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3705 | INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work); |
| 3706 | mdsc->last_renew_caps = jiffies; |
| 3707 | INIT_LIST_HEAD(&mdsc->cap_delay_list); |
| 3708 | spin_lock_init(&mdsc->cap_delay_lock); |
| 3709 | INIT_LIST_HEAD(&mdsc->snap_flush_list); |
| 3710 | spin_lock_init(&mdsc->snap_flush_lock); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 3711 | mdsc->last_cap_flush_tid = 1; |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 3712 | INIT_LIST_HEAD(&mdsc->cap_flush_list); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3713 | INIT_LIST_HEAD(&mdsc->cap_dirty); |
Sage Weil | db35405 | 2011-05-24 11:46:31 -0700 | [diff] [blame] | 3714 | INIT_LIST_HEAD(&mdsc->cap_dirty_migrating); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3715 | mdsc->num_cap_flushing = 0; |
| 3716 | spin_lock_init(&mdsc->cap_dirty_lock); |
| 3717 | init_waitqueue_head(&mdsc->cap_flushing_wq); |
| 3718 | spin_lock_init(&mdsc->dentry_lru_lock); |
| 3719 | INIT_LIST_HEAD(&mdsc->dentry_lru); |
Cheng Renquan | 2d06eeb | 2010-03-26 18:04:40 +0800 | [diff] [blame] | 3720 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 3721 | ceph_caps_init(mdsc); |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3722 | ceph_adjust_min_caps(mdsc, fsc->min_caps); |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 3723 | |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 3724 | init_rwsem(&mdsc->pool_perm_rwsem); |
| 3725 | mdsc->pool_perm_tree = RB_ROOT; |
| 3726 | |
Yan, Zheng | dfeb84d | 2018-07-02 15:55:23 +0800 | [diff] [blame] | 3727 | strscpy(mdsc->nodename, utsname()->nodename, |
| 3728 | sizeof(mdsc->nodename)); |
Sage Weil | 5f44f14 | 2009-11-18 14:52:18 -0800 | [diff] [blame] | 3729 | return 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3730 | } |
| 3731 | |
| 3732 | /* |
| 3733 | * Wait for safe replies on open mds requests. If we time out, drop |
| 3734 | * all requests from the tree to avoid dangling dentry refs. |
| 3735 | */ |
| 3736 | static void wait_requests(struct ceph_mds_client *mdsc) |
| 3737 | { |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 3738 | struct ceph_options *opts = mdsc->fsc->client->options; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3739 | struct ceph_mds_request *req; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3740 | |
| 3741 | mutex_lock(&mdsc->mutex); |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 3742 | if (__get_oldest_req(mdsc)) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3743 | mutex_unlock(&mdsc->mutex); |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 3744 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3745 | dout("wait_requests waiting for requests\n"); |
| 3746 | wait_for_completion_timeout(&mdsc->safe_umount_waiters, |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 3747 | ceph_timeout_jiffies(opts->mount_timeout)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3748 | |
| 3749 | /* tear down remaining requests */ |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 3750 | mutex_lock(&mdsc->mutex); |
| 3751 | while ((req = __get_oldest_req(mdsc))) { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3752 | dout("wait_requests timed out on tid %llu\n", |
| 3753 | req->r_tid); |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 3754 | __unregister_request(mdsc, req); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3755 | } |
| 3756 | } |
| 3757 | mutex_unlock(&mdsc->mutex); |
| 3758 | dout("wait_requests done\n"); |
| 3759 | } |
| 3760 | |
| 3761 | /* |
| 3762 | * called before mount is ro, and before dentries are torn down. |
| 3763 | * (hmm, does this still race with new lookups?) |
| 3764 | */ |
| 3765 | void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc) |
| 3766 | { |
| 3767 | dout("pre_umount\n"); |
| 3768 | mdsc->stopping = 1; |
| 3769 | |
Yan, Zheng | 7aac453 | 2018-03-13 19:52:20 +0800 | [diff] [blame] | 3770 | lock_unlock_sessions(mdsc); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 3771 | ceph_flush_dirty_caps(mdsc); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3772 | wait_requests(mdsc); |
Sage Weil | 17c688c | 2010-06-21 16:12:26 -0700 | [diff] [blame] | 3773 | |
| 3774 | /* |
| 3775 | * wait for reply handlers to drop their request refs and |
| 3776 | * their inode/dcache refs |
| 3777 | */ |
| 3778 | ceph_msgr_flush(); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3779 | } |
| 3780 | |
| 3781 | /* |
| 3782 | * wait for all write mds requests to flush. |
| 3783 | */ |
| 3784 | static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid) |
| 3785 | { |
Sage Weil | 80fc731 | 2010-03-16 15:28:54 -0700 | [diff] [blame] | 3786 | struct ceph_mds_request *req = NULL, *nextreq; |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 3787 | struct rb_node *n; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3788 | |
| 3789 | mutex_lock(&mdsc->mutex); |
| 3790 | dout("wait_unsafe_requests want %lld\n", want_tid); |
Sage Weil | 80fc731 | 2010-03-16 15:28:54 -0700 | [diff] [blame] | 3791 | restart: |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 3792 | req = __get_oldest_req(mdsc); |
| 3793 | while (req && req->r_tid <= want_tid) { |
Sage Weil | 80fc731 | 2010-03-16 15:28:54 -0700 | [diff] [blame] | 3794 | /* find next request */ |
| 3795 | n = rb_next(&req->r_node); |
| 3796 | if (n) |
| 3797 | nextreq = rb_entry(n, struct ceph_mds_request, r_node); |
| 3798 | else |
| 3799 | nextreq = NULL; |
Yan, Zheng | e8a7b8b | 2015-05-19 18:54:40 +0800 | [diff] [blame] | 3800 | if (req->r_op != CEPH_MDS_OP_SETFILELOCK && |
| 3801 | (req->r_op & CEPH_MDS_OP_WRITE)) { |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 3802 | /* write op */ |
| 3803 | ceph_mdsc_get_request(req); |
Sage Weil | 80fc731 | 2010-03-16 15:28:54 -0700 | [diff] [blame] | 3804 | if (nextreq) |
| 3805 | ceph_mdsc_get_request(nextreq); |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 3806 | mutex_unlock(&mdsc->mutex); |
| 3807 | dout("wait_unsafe_requests wait on %llu (want %llu)\n", |
| 3808 | req->r_tid, want_tid); |
| 3809 | wait_for_completion(&req->r_safe_completion); |
| 3810 | mutex_lock(&mdsc->mutex); |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 3811 | ceph_mdsc_put_request(req); |
Sage Weil | 80fc731 | 2010-03-16 15:28:54 -0700 | [diff] [blame] | 3812 | if (!nextreq) |
| 3813 | break; /* next dne before, so we're done! */ |
| 3814 | if (RB_EMPTY_NODE(&nextreq->r_node)) { |
| 3815 | /* next request was removed from tree */ |
| 3816 | ceph_mdsc_put_request(nextreq); |
| 3817 | goto restart; |
| 3818 | } |
| 3819 | ceph_mdsc_put_request(nextreq); /* won't go away */ |
Sage Weil | 44ca18f | 2010-02-15 12:08:46 -0800 | [diff] [blame] | 3820 | } |
Sage Weil | 80fc731 | 2010-03-16 15:28:54 -0700 | [diff] [blame] | 3821 | req = nextreq; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3822 | } |
| 3823 | mutex_unlock(&mdsc->mutex); |
| 3824 | dout("wait_unsafe_requests done\n"); |
| 3825 | } |
| 3826 | |
| 3827 | void ceph_mdsc_sync(struct ceph_mds_client *mdsc) |
| 3828 | { |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3829 | u64 want_tid, want_flush; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3830 | |
Seraphime Kirkovski | 52953d5 | 2016-12-26 10:26:34 +0100 | [diff] [blame] | 3831 | if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) |
Sage Weil | 56b7cf9 | 2010-05-03 15:22:00 -0700 | [diff] [blame] | 3832 | return; |
| 3833 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3834 | dout("sync\n"); |
| 3835 | mutex_lock(&mdsc->mutex); |
| 3836 | want_tid = mdsc->last_tid; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3837 | mutex_unlock(&mdsc->mutex); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3838 | |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 3839 | ceph_flush_dirty_caps(mdsc); |
Yan, Zheng | d3383a8 | 2015-01-08 21:30:12 +0800 | [diff] [blame] | 3840 | spin_lock(&mdsc->cap_dirty_lock); |
Yan, Zheng | 8310b08 | 2015-06-09 17:20:12 +0800 | [diff] [blame] | 3841 | want_flush = mdsc->last_cap_flush_tid; |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 3842 | if (!list_empty(&mdsc->cap_flush_list)) { |
| 3843 | struct ceph_cap_flush *cf = |
| 3844 | list_last_entry(&mdsc->cap_flush_list, |
| 3845 | struct ceph_cap_flush, g_list); |
| 3846 | cf->wake = true; |
| 3847 | } |
Yan, Zheng | d3383a8 | 2015-01-08 21:30:12 +0800 | [diff] [blame] | 3848 | spin_unlock(&mdsc->cap_dirty_lock); |
| 3849 | |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3850 | dout("sync want tid %lld flush_seq %lld\n", |
| 3851 | want_tid, want_flush); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3852 | |
| 3853 | wait_unsafe_requests(mdsc, want_tid); |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3854 | wait_caps_flush(mdsc, want_flush); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3855 | } |
| 3856 | |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 3857 | /* |
| 3858 | * true if all sessions are closed, or we force unmount |
| 3859 | */ |
Yan, Zheng | fcff415 | 2016-09-14 16:39:51 +0800 | [diff] [blame] | 3860 | static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped) |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 3861 | { |
Seraphime Kirkovski | 52953d5 | 2016-12-26 10:26:34 +0100 | [diff] [blame] | 3862 | if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 3863 | return true; |
Yan, Zheng | fcff415 | 2016-09-14 16:39:51 +0800 | [diff] [blame] | 3864 | return atomic_read(&mdsc->num_sessions) <= skipped; |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 3865 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3866 | |
| 3867 | /* |
| 3868 | * called after sb is ro. |
| 3869 | */ |
| 3870 | void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc) |
| 3871 | { |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 3872 | struct ceph_options *opts = mdsc->fsc->client->options; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3873 | struct ceph_mds_session *session; |
| 3874 | int i; |
Yan, Zheng | fcff415 | 2016-09-14 16:39:51 +0800 | [diff] [blame] | 3875 | int skipped = 0; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3876 | |
| 3877 | dout("close_sessions\n"); |
| 3878 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3879 | /* close sessions */ |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 3880 | mutex_lock(&mdsc->mutex); |
| 3881 | for (i = 0; i < mdsc->max_sessions; i++) { |
| 3882 | session = __ceph_lookup_mds_session(mdsc, i); |
| 3883 | if (!session) |
| 3884 | continue; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3885 | mutex_unlock(&mdsc->mutex); |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 3886 | mutex_lock(&session->s_mutex); |
Yan, Zheng | fcff415 | 2016-09-14 16:39:51 +0800 | [diff] [blame] | 3887 | if (__close_session(mdsc, session) <= 0) |
| 3888 | skipped++; |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 3889 | mutex_unlock(&session->s_mutex); |
| 3890 | ceph_put_mds_session(session); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3891 | mutex_lock(&mdsc->mutex); |
| 3892 | } |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 3893 | mutex_unlock(&mdsc->mutex); |
| 3894 | |
| 3895 | dout("waiting for sessions to close\n"); |
Yan, Zheng | fcff415 | 2016-09-14 16:39:51 +0800 | [diff] [blame] | 3896 | wait_event_timeout(mdsc->session_close_wq, |
| 3897 | done_closing_sessions(mdsc, skipped), |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 3898 | ceph_timeout_jiffies(opts->mount_timeout)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3899 | |
| 3900 | /* tear down remaining sessions */ |
Sage Weil | f3c60c5 | 2010-08-11 14:51:23 -0700 | [diff] [blame] | 3901 | mutex_lock(&mdsc->mutex); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3902 | for (i = 0; i < mdsc->max_sessions; i++) { |
| 3903 | if (mdsc->sessions[i]) { |
| 3904 | session = get_session(mdsc->sessions[i]); |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 3905 | __unregister_session(mdsc, session); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3906 | mutex_unlock(&mdsc->mutex); |
| 3907 | mutex_lock(&session->s_mutex); |
| 3908 | remove_session_caps(session); |
| 3909 | mutex_unlock(&session->s_mutex); |
| 3910 | ceph_put_mds_session(session); |
| 3911 | mutex_lock(&mdsc->mutex); |
| 3912 | } |
| 3913 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3914 | WARN_ON(!list_empty(&mdsc->cap_delay_list)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3915 | mutex_unlock(&mdsc->mutex); |
| 3916 | |
| 3917 | ceph_cleanup_empty_realms(mdsc); |
| 3918 | |
| 3919 | cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */ |
| 3920 | |
| 3921 | dout("stopped\n"); |
| 3922 | } |
| 3923 | |
Yan, Zheng | 48fec5d | 2015-07-01 16:27:46 +0800 | [diff] [blame] | 3924 | void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc) |
| 3925 | { |
| 3926 | struct ceph_mds_session *session; |
| 3927 | int mds; |
| 3928 | |
| 3929 | dout("force umount\n"); |
| 3930 | |
| 3931 | mutex_lock(&mdsc->mutex); |
| 3932 | for (mds = 0; mds < mdsc->max_sessions; mds++) { |
| 3933 | session = __ceph_lookup_mds_session(mdsc, mds); |
| 3934 | if (!session) |
| 3935 | continue; |
| 3936 | mutex_unlock(&mdsc->mutex); |
| 3937 | mutex_lock(&session->s_mutex); |
| 3938 | __close_session(mdsc, session); |
| 3939 | if (session->s_state == CEPH_MDS_SESSION_CLOSING) { |
| 3940 | cleanup_session_requests(mdsc, session); |
| 3941 | remove_session_caps(session); |
| 3942 | } |
| 3943 | mutex_unlock(&session->s_mutex); |
| 3944 | ceph_put_mds_session(session); |
| 3945 | mutex_lock(&mdsc->mutex); |
| 3946 | kick_requests(mdsc, mds); |
| 3947 | } |
| 3948 | __wake_requests(mdsc, &mdsc->waiting_for_map); |
| 3949 | mutex_unlock(&mdsc->mutex); |
| 3950 | } |
| 3951 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3952 | static void ceph_mdsc_stop(struct ceph_mds_client *mdsc) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3953 | { |
| 3954 | dout("stop\n"); |
| 3955 | cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */ |
| 3956 | if (mdsc->mdsmap) |
| 3957 | ceph_mdsmap_destroy(mdsc->mdsmap); |
| 3958 | kfree(mdsc->sessions); |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 3959 | ceph_caps_finalize(mdsc); |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 3960 | ceph_pool_perm_destroy(mdsc); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 3961 | } |
| 3962 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3963 | void ceph_mdsc_destroy(struct ceph_fs_client *fsc) |
| 3964 | { |
| 3965 | struct ceph_mds_client *mdsc = fsc->mdsc; |
Sage Weil | ef550f6 | 2011-03-25 13:27:48 -0700 | [diff] [blame] | 3966 | dout("mdsc_destroy %p\n", mdsc); |
Sage Weil | ef550f6 | 2011-03-25 13:27:48 -0700 | [diff] [blame] | 3967 | |
Chengguang Xu | 50c55ae | 2018-03-14 13:47:33 +0800 | [diff] [blame] | 3968 | if (!mdsc) |
| 3969 | return; |
| 3970 | |
Sage Weil | ef550f6 | 2011-03-25 13:27:48 -0700 | [diff] [blame] | 3971 | /* flush out any connection work with references to us */ |
| 3972 | ceph_msgr_flush(); |
| 3973 | |
Yan, Zheng | 62a65f3 | 2017-06-22 16:26:34 +0800 | [diff] [blame] | 3974 | ceph_mdsc_stop(mdsc); |
| 3975 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3976 | fsc->mdsc = NULL; |
| 3977 | kfree(mdsc); |
Sage Weil | ef550f6 | 2011-03-25 13:27:48 -0700 | [diff] [blame] | 3978 | dout("mdsc_destroy %p done\n", mdsc); |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3979 | } |
| 3980 | |
Yan, Zheng | 430afba | 2016-07-08 11:25:38 +0800 | [diff] [blame] | 3981 | void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg) |
| 3982 | { |
| 3983 | struct ceph_fs_client *fsc = mdsc->fsc; |
| 3984 | const char *mds_namespace = fsc->mount_options->mds_namespace; |
| 3985 | void *p = msg->front.iov_base; |
| 3986 | void *end = p + msg->front.iov_len; |
| 3987 | u32 epoch; |
| 3988 | u32 map_len; |
| 3989 | u32 num_fs; |
| 3990 | u32 mount_fscid = (u32)-1; |
| 3991 | u8 struct_v, struct_cv; |
| 3992 | int err = -EINVAL; |
| 3993 | |
| 3994 | ceph_decode_need(&p, end, sizeof(u32), bad); |
| 3995 | epoch = ceph_decode_32(&p); |
| 3996 | |
| 3997 | dout("handle_fsmap epoch %u\n", epoch); |
| 3998 | |
| 3999 | ceph_decode_need(&p, end, 2 + sizeof(u32), bad); |
| 4000 | struct_v = ceph_decode_8(&p); |
| 4001 | struct_cv = ceph_decode_8(&p); |
| 4002 | map_len = ceph_decode_32(&p); |
| 4003 | |
| 4004 | ceph_decode_need(&p, end, sizeof(u32) * 3, bad); |
| 4005 | p += sizeof(u32) * 2; /* skip epoch and legacy_client_fscid */ |
| 4006 | |
| 4007 | num_fs = ceph_decode_32(&p); |
| 4008 | while (num_fs-- > 0) { |
| 4009 | void *info_p, *info_end; |
| 4010 | u32 info_len; |
| 4011 | u8 info_v, info_cv; |
| 4012 | u32 fscid, namelen; |
| 4013 | |
| 4014 | ceph_decode_need(&p, end, 2 + sizeof(u32), bad); |
| 4015 | info_v = ceph_decode_8(&p); |
| 4016 | info_cv = ceph_decode_8(&p); |
| 4017 | info_len = ceph_decode_32(&p); |
| 4018 | ceph_decode_need(&p, end, info_len, bad); |
| 4019 | info_p = p; |
| 4020 | info_end = p + info_len; |
| 4021 | p = info_end; |
| 4022 | |
| 4023 | ceph_decode_need(&info_p, info_end, sizeof(u32) * 2, bad); |
| 4024 | fscid = ceph_decode_32(&info_p); |
| 4025 | namelen = ceph_decode_32(&info_p); |
| 4026 | ceph_decode_need(&info_p, info_end, namelen, bad); |
| 4027 | |
| 4028 | if (mds_namespace && |
| 4029 | strlen(mds_namespace) == namelen && |
| 4030 | !strncmp(mds_namespace, (char *)info_p, namelen)) { |
| 4031 | mount_fscid = fscid; |
| 4032 | break; |
| 4033 | } |
| 4034 | } |
| 4035 | |
| 4036 | ceph_monc_got_map(&fsc->client->monc, CEPH_SUB_FSMAP, epoch); |
| 4037 | if (mount_fscid != (u32)-1) { |
| 4038 | fsc->client->monc.fs_cluster_id = mount_fscid; |
| 4039 | ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP, |
| 4040 | 0, true); |
| 4041 | ceph_monc_renew_subs(&fsc->client->monc); |
| 4042 | } else { |
| 4043 | err = -ENOENT; |
| 4044 | goto err_out; |
| 4045 | } |
| 4046 | return; |
Ilya Dryomov | 76bd6ec | 2017-10-16 10:32:50 +0200 | [diff] [blame] | 4047 | |
Yan, Zheng | 430afba | 2016-07-08 11:25:38 +0800 | [diff] [blame] | 4048 | bad: |
| 4049 | pr_err("error decoding fsmap\n"); |
| 4050 | err_out: |
| 4051 | mutex_lock(&mdsc->mutex); |
Ilya Dryomov | 76bd6ec | 2017-10-16 10:32:50 +0200 | [diff] [blame] | 4052 | mdsc->mdsmap_err = err; |
Yan, Zheng | 430afba | 2016-07-08 11:25:38 +0800 | [diff] [blame] | 4053 | __wake_requests(mdsc, &mdsc->waiting_for_map); |
| 4054 | mutex_unlock(&mdsc->mutex); |
Yan, Zheng | 430afba | 2016-07-08 11:25:38 +0800 | [diff] [blame] | 4055 | } |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4056 | |
| 4057 | /* |
| 4058 | * handle mds map update. |
| 4059 | */ |
Yan, Zheng | 430afba | 2016-07-08 11:25:38 +0800 | [diff] [blame] | 4060 | void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg) |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4061 | { |
| 4062 | u32 epoch; |
| 4063 | u32 maplen; |
| 4064 | void *p = msg->front.iov_base; |
| 4065 | void *end = p + msg->front.iov_len; |
| 4066 | struct ceph_mdsmap *newmap, *oldmap; |
| 4067 | struct ceph_fsid fsid; |
| 4068 | int err = -EINVAL; |
| 4069 | |
| 4070 | ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad); |
| 4071 | ceph_decode_copy(&p, &fsid, sizeof(fsid)); |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 4072 | if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0) |
Sage Weil | 0743304 | 2009-11-18 16:50:41 -0800 | [diff] [blame] | 4073 | return; |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 4074 | epoch = ceph_decode_32(&p); |
| 4075 | maplen = ceph_decode_32(&p); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4076 | dout("handle_map epoch %u len %d\n", epoch, (int)maplen); |
| 4077 | |
| 4078 | /* do we need it? */ |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4079 | mutex_lock(&mdsc->mutex); |
| 4080 | if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) { |
| 4081 | dout("handle_map epoch %u <= our %u\n", |
| 4082 | epoch, mdsc->mdsmap->m_epoch); |
| 4083 | mutex_unlock(&mdsc->mutex); |
| 4084 | return; |
| 4085 | } |
| 4086 | |
| 4087 | newmap = ceph_mdsmap_decode(&p, end); |
| 4088 | if (IS_ERR(newmap)) { |
| 4089 | err = PTR_ERR(newmap); |
| 4090 | goto bad_unlock; |
| 4091 | } |
| 4092 | |
| 4093 | /* swap into place */ |
| 4094 | if (mdsc->mdsmap) { |
| 4095 | oldmap = mdsc->mdsmap; |
| 4096 | mdsc->mdsmap = newmap; |
| 4097 | check_new_map(mdsc, newmap, oldmap); |
| 4098 | ceph_mdsmap_destroy(oldmap); |
| 4099 | } else { |
| 4100 | mdsc->mdsmap = newmap; /* first mds map */ |
| 4101 | } |
Chengguang Xu | 719784b | 2018-07-19 22:15:24 +0800 | [diff] [blame] | 4102 | mdsc->fsc->max_file_size = min((loff_t)mdsc->mdsmap->m_max_file_size, |
| 4103 | MAX_LFS_FILESIZE); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4104 | |
| 4105 | __wake_requests(mdsc, &mdsc->waiting_for_map); |
Ilya Dryomov | 82dcaba | 2016-01-19 16:19:06 +0100 | [diff] [blame] | 4106 | ceph_monc_got_map(&mdsc->fsc->client->monc, CEPH_SUB_MDSMAP, |
| 4107 | mdsc->mdsmap->m_epoch); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4108 | |
| 4109 | mutex_unlock(&mdsc->mutex); |
| 4110 | schedule_delayed(mdsc); |
| 4111 | return; |
| 4112 | |
| 4113 | bad_unlock: |
| 4114 | mutex_unlock(&mdsc->mutex); |
| 4115 | bad: |
| 4116 | pr_err("error decoding mdsmap %d\n", err); |
| 4117 | return; |
| 4118 | } |
| 4119 | |
| 4120 | static struct ceph_connection *con_get(struct ceph_connection *con) |
| 4121 | { |
| 4122 | struct ceph_mds_session *s = con->private; |
| 4123 | |
| 4124 | if (get_session(s)) { |
Elena Reshetova | 3997c01 | 2017-03-03 11:15:06 +0200 | [diff] [blame] | 4125 | dout("mdsc con_get %p ok (%d)\n", s, refcount_read(&s->s_ref)); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4126 | return con; |
| 4127 | } |
| 4128 | dout("mdsc con_get %p FAIL\n", s); |
| 4129 | return NULL; |
| 4130 | } |
| 4131 | |
| 4132 | static void con_put(struct ceph_connection *con) |
| 4133 | { |
| 4134 | struct ceph_mds_session *s = con->private; |
| 4135 | |
Elena Reshetova | 3997c01 | 2017-03-03 11:15:06 +0200 | [diff] [blame] | 4136 | dout("mdsc con_put %p (%d)\n", s, refcount_read(&s->s_ref) - 1); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4137 | ceph_put_mds_session(s); |
| 4138 | } |
| 4139 | |
| 4140 | /* |
| 4141 | * if the client is unresponsive for long enough, the mds will kill |
| 4142 | * the session entirely. |
| 4143 | */ |
| 4144 | static void peer_reset(struct ceph_connection *con) |
| 4145 | { |
| 4146 | struct ceph_mds_session *s = con->private; |
Sage Weil | 7e70f0e | 2010-03-18 13:59:12 -0700 | [diff] [blame] | 4147 | struct ceph_mds_client *mdsc = s->s_mdsc; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4148 | |
Fabian Frederick | f3ae1b9 | 2014-06-06 14:35:37 -0700 | [diff] [blame] | 4149 | pr_warn("mds%d closed our session\n", s->s_mds); |
Sage Weil | 7e70f0e | 2010-03-18 13:59:12 -0700 | [diff] [blame] | 4150 | send_mds_reconnect(mdsc, s); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4151 | } |
| 4152 | |
| 4153 | static void dispatch(struct ceph_connection *con, struct ceph_msg *msg) |
| 4154 | { |
| 4155 | struct ceph_mds_session *s = con->private; |
| 4156 | struct ceph_mds_client *mdsc = s->s_mdsc; |
| 4157 | int type = le16_to_cpu(msg->hdr.type); |
| 4158 | |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 4159 | mutex_lock(&mdsc->mutex); |
| 4160 | if (__verify_registered_session(mdsc, s) < 0) { |
| 4161 | mutex_unlock(&mdsc->mutex); |
| 4162 | goto out; |
| 4163 | } |
| 4164 | mutex_unlock(&mdsc->mutex); |
| 4165 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4166 | switch (type) { |
| 4167 | case CEPH_MSG_MDS_MAP: |
Yan, Zheng | 430afba | 2016-07-08 11:25:38 +0800 | [diff] [blame] | 4168 | ceph_mdsc_handle_mdsmap(mdsc, msg); |
| 4169 | break; |
| 4170 | case CEPH_MSG_FS_MAP_USER: |
| 4171 | ceph_mdsc_handle_fsmap(mdsc, msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4172 | break; |
| 4173 | case CEPH_MSG_CLIENT_SESSION: |
| 4174 | handle_session(s, msg); |
| 4175 | break; |
| 4176 | case CEPH_MSG_CLIENT_REPLY: |
| 4177 | handle_reply(s, msg); |
| 4178 | break; |
| 4179 | case CEPH_MSG_CLIENT_REQUEST_FORWARD: |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 4180 | handle_forward(mdsc, s, msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4181 | break; |
| 4182 | case CEPH_MSG_CLIENT_CAPS: |
| 4183 | ceph_handle_caps(s, msg); |
| 4184 | break; |
| 4185 | case CEPH_MSG_CLIENT_SNAP: |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 4186 | ceph_handle_snap(mdsc, s, msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4187 | break; |
| 4188 | case CEPH_MSG_CLIENT_LEASE: |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 4189 | handle_lease(mdsc, s, msg); |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4190 | break; |
Luis Henriques | fb18a57 | 2018-01-05 10:47:18 +0000 | [diff] [blame] | 4191 | case CEPH_MSG_CLIENT_QUOTA: |
| 4192 | ceph_handle_quota(mdsc, s, msg); |
| 4193 | break; |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4194 | |
| 4195 | default: |
| 4196 | pr_err("received unknown message type %d %s\n", type, |
| 4197 | ceph_msg_type_name(type)); |
| 4198 | } |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 4199 | out: |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4200 | ceph_msg_put(msg); |
| 4201 | } |
| 4202 | |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 4203 | /* |
| 4204 | * authentication |
| 4205 | */ |
Alex Elder | a3530df | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 4206 | |
| 4207 | /* |
| 4208 | * Note: returned pointer is the address of a structure that's |
| 4209 | * managed separately. Caller must *not* attempt to free it. |
| 4210 | */ |
| 4211 | static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con, |
Alex Elder | 8f43fb5 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 4212 | int *proto, int force_new) |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 4213 | { |
| 4214 | struct ceph_mds_session *s = con->private; |
| 4215 | struct ceph_mds_client *mdsc = s->s_mdsc; |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 4216 | struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth; |
Alex Elder | 74f1869 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 4217 | struct ceph_auth_handshake *auth = &s->s_auth; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 4218 | |
Alex Elder | 74f1869 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 4219 | if (force_new && auth->authorizer) { |
Ilya Dryomov | 6c1ea26 | 2016-04-11 19:34:49 +0200 | [diff] [blame] | 4220 | ceph_auth_destroy_authorizer(auth->authorizer); |
Alex Elder | 74f1869 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 4221 | auth->authorizer = NULL; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 4222 | } |
Sage Weil | 27859f9 | 2013-03-25 10:26:14 -0700 | [diff] [blame] | 4223 | if (!auth->authorizer) { |
| 4224 | int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_MDS, |
| 4225 | auth); |
Sage Weil | 0bed9b5 | 2013-03-25 10:26:01 -0700 | [diff] [blame] | 4226 | if (ret) |
| 4227 | return ERR_PTR(ret); |
Sage Weil | 27859f9 | 2013-03-25 10:26:14 -0700 | [diff] [blame] | 4228 | } else { |
| 4229 | int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_MDS, |
| 4230 | auth); |
Alex Elder | a255651 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 4231 | if (ret) |
Alex Elder | a3530df | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 4232 | return ERR_PTR(ret); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 4233 | } |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 4234 | *proto = ac->protocol; |
Alex Elder | 74f1869 | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 4235 | |
Alex Elder | a3530df | 2012-05-16 15:16:39 -0500 | [diff] [blame] | 4236 | return auth; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 4237 | } |
| 4238 | |
Ilya Dryomov | 6daca13 | 2018-07-27 19:18:34 +0200 | [diff] [blame] | 4239 | static int add_authorizer_challenge(struct ceph_connection *con, |
| 4240 | void *challenge_buf, int challenge_buf_len) |
| 4241 | { |
| 4242 | struct ceph_mds_session *s = con->private; |
| 4243 | struct ceph_mds_client *mdsc = s->s_mdsc; |
| 4244 | struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth; |
| 4245 | |
| 4246 | return ceph_auth_add_authorizer_challenge(ac, s->s_auth.authorizer, |
| 4247 | challenge_buf, challenge_buf_len); |
| 4248 | } |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 4249 | |
Ilya Dryomov | 0dde584 | 2016-12-02 16:35:09 +0100 | [diff] [blame] | 4250 | static int verify_authorizer_reply(struct ceph_connection *con) |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 4251 | { |
| 4252 | struct ceph_mds_session *s = con->private; |
| 4253 | struct ceph_mds_client *mdsc = s->s_mdsc; |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 4254 | struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth; |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 4255 | |
Ilya Dryomov | 0dde584 | 2016-12-02 16:35:09 +0100 | [diff] [blame] | 4256 | return ceph_auth_verify_authorizer_reply(ac, s->s_auth.authorizer); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 4257 | } |
| 4258 | |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 4259 | static int invalidate_authorizer(struct ceph_connection *con) |
| 4260 | { |
| 4261 | struct ceph_mds_session *s = con->private; |
| 4262 | struct ceph_mds_client *mdsc = s->s_mdsc; |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 4263 | struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth; |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 4264 | |
Sage Weil | 27859f9 | 2013-03-25 10:26:14 -0700 | [diff] [blame] | 4265 | ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS); |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 4266 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 4267 | return ceph_monc_validate_auth(&mdsc->fsc->client->monc); |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 4268 | } |
| 4269 | |
Alex Elder | 53ded49 | 2013-03-01 18:00:14 -0600 | [diff] [blame] | 4270 | static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con, |
| 4271 | struct ceph_msg_header *hdr, int *skip) |
| 4272 | { |
| 4273 | struct ceph_msg *msg; |
| 4274 | int type = (int) le16_to_cpu(hdr->type); |
| 4275 | int front_len = (int) le32_to_cpu(hdr->front_len); |
| 4276 | |
| 4277 | if (con->in_msg) |
| 4278 | return con->in_msg; |
| 4279 | |
| 4280 | *skip = 0; |
| 4281 | msg = ceph_msg_new(type, front_len, GFP_NOFS, false); |
| 4282 | if (!msg) { |
| 4283 | pr_err("unable to allocate msg type %d len %d\n", |
| 4284 | type, front_len); |
| 4285 | return NULL; |
| 4286 | } |
Alex Elder | 53ded49 | 2013-03-01 18:00:14 -0600 | [diff] [blame] | 4287 | |
| 4288 | return msg; |
| 4289 | } |
| 4290 | |
Ilya Dryomov | 79dbd1b | 2015-10-26 22:23:56 +0100 | [diff] [blame] | 4291 | static int mds_sign_message(struct ceph_msg *msg) |
Yan, Zheng | 33d0733 | 2014-11-04 16:33:37 +0800 | [diff] [blame] | 4292 | { |
Ilya Dryomov | 79dbd1b | 2015-10-26 22:23:56 +0100 | [diff] [blame] | 4293 | struct ceph_mds_session *s = msg->con->private; |
Yan, Zheng | 33d0733 | 2014-11-04 16:33:37 +0800 | [diff] [blame] | 4294 | struct ceph_auth_handshake *auth = &s->s_auth; |
Ilya Dryomov | 79dbd1b | 2015-10-26 22:23:56 +0100 | [diff] [blame] | 4295 | |
Yan, Zheng | 33d0733 | 2014-11-04 16:33:37 +0800 | [diff] [blame] | 4296 | return ceph_auth_sign_message(auth, msg); |
| 4297 | } |
| 4298 | |
Ilya Dryomov | 79dbd1b | 2015-10-26 22:23:56 +0100 | [diff] [blame] | 4299 | static int mds_check_message_signature(struct ceph_msg *msg) |
Yan, Zheng | 33d0733 | 2014-11-04 16:33:37 +0800 | [diff] [blame] | 4300 | { |
Ilya Dryomov | 79dbd1b | 2015-10-26 22:23:56 +0100 | [diff] [blame] | 4301 | struct ceph_mds_session *s = msg->con->private; |
Yan, Zheng | 33d0733 | 2014-11-04 16:33:37 +0800 | [diff] [blame] | 4302 | struct ceph_auth_handshake *auth = &s->s_auth; |
Ilya Dryomov | 79dbd1b | 2015-10-26 22:23:56 +0100 | [diff] [blame] | 4303 | |
Yan, Zheng | 33d0733 | 2014-11-04 16:33:37 +0800 | [diff] [blame] | 4304 | return ceph_auth_check_message_signature(auth, msg); |
| 4305 | } |
| 4306 | |
Tobias Klauser | 9e32789 | 2010-05-20 10:40:19 +0200 | [diff] [blame] | 4307 | static const struct ceph_connection_operations mds_con_ops = { |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4308 | .get = con_get, |
| 4309 | .put = con_put, |
| 4310 | .dispatch = dispatch, |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 4311 | .get_authorizer = get_authorizer, |
Ilya Dryomov | 6daca13 | 2018-07-27 19:18:34 +0200 | [diff] [blame] | 4312 | .add_authorizer_challenge = add_authorizer_challenge, |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 4313 | .verify_authorizer_reply = verify_authorizer_reply, |
Sage Weil | 9bd2e6f | 2010-02-02 16:21:06 -0800 | [diff] [blame] | 4314 | .invalidate_authorizer = invalidate_authorizer, |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4315 | .peer_reset = peer_reset, |
Alex Elder | 53ded49 | 2013-03-01 18:00:14 -0600 | [diff] [blame] | 4316 | .alloc_msg = mds_alloc_msg, |
Ilya Dryomov | 79dbd1b | 2015-10-26 22:23:56 +0100 | [diff] [blame] | 4317 | .sign_message = mds_sign_message, |
| 4318 | .check_message_signature = mds_check_message_signature, |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4319 | }; |
| 4320 | |
Sage Weil | 2f2dc05 | 2009-10-06 11:31:09 -0700 | [diff] [blame] | 4321 | /* eof */ |