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