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