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