Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2 | #include <linux/ceph/ceph_debug.h> |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3 | |
| 4 | #include <linux/fs.h> |
| 5 | #include <linux/kernel.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 6 | #include <linux/sched/signal.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 7 | #include <linux/slab.h> |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 8 | #include <linux/vmalloc.h> |
| 9 | #include <linux/wait.h> |
Stephen Rothwell | f1a3d57 | 2010-01-18 11:53:08 +1100 | [diff] [blame] | 10 | #include <linux/writeback.h> |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 11 | |
| 12 | #include "super.h" |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 13 | #include "mds_client.h" |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 14 | #include "cache.h" |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 15 | #include <linux/ceph/decode.h> |
| 16 | #include <linux/ceph/messenger.h> |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 17 | |
| 18 | /* |
| 19 | * Capability management |
| 20 | * |
| 21 | * The Ceph metadata servers control client access to inode metadata |
| 22 | * and file data by issuing capabilities, granting clients permission |
| 23 | * to read and/or write both inode field and file data to OSDs |
| 24 | * (storage nodes). Each capability consists of a set of bits |
| 25 | * indicating which operations are allowed. |
| 26 | * |
| 27 | * If the client holds a *_SHARED cap, the client has a coherent value |
| 28 | * that can be safely read from the cached inode. |
| 29 | * |
| 30 | * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the |
| 31 | * client is allowed to change inode attributes (e.g., file size, |
| 32 | * mtime), note its dirty state in the ceph_cap, and asynchronously |
| 33 | * flush that metadata change to the MDS. |
| 34 | * |
| 35 | * In the event of a conflicting operation (perhaps by another |
| 36 | * client), the MDS will revoke the conflicting client capabilities. |
| 37 | * |
| 38 | * In order for a client to cache an inode, it must hold a capability |
| 39 | * with at least one MDS server. When inodes are released, release |
| 40 | * notifications are batched and periodically sent en masse to the MDS |
| 41 | * cluster to release server state. |
| 42 | */ |
| 43 | |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 44 | static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc); |
Yan, Zheng | 7bc00fd | 2016-07-07 18:34:45 +0800 | [diff] [blame] | 45 | static void __kick_flushing_caps(struct ceph_mds_client *mdsc, |
| 46 | struct ceph_mds_session *session, |
| 47 | struct ceph_inode_info *ci, |
| 48 | u64 oldest_flush_tid); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 49 | |
| 50 | /* |
| 51 | * Generate readable cap strings for debugging output. |
| 52 | */ |
| 53 | #define MAX_CAP_STR 20 |
| 54 | static char cap_str[MAX_CAP_STR][40]; |
| 55 | static DEFINE_SPINLOCK(cap_str_lock); |
| 56 | static int last_cap_str; |
| 57 | |
| 58 | static char *gcap_string(char *s, int c) |
| 59 | { |
| 60 | if (c & CEPH_CAP_GSHARED) |
| 61 | *s++ = 's'; |
| 62 | if (c & CEPH_CAP_GEXCL) |
| 63 | *s++ = 'x'; |
| 64 | if (c & CEPH_CAP_GCACHE) |
| 65 | *s++ = 'c'; |
| 66 | if (c & CEPH_CAP_GRD) |
| 67 | *s++ = 'r'; |
| 68 | if (c & CEPH_CAP_GWR) |
| 69 | *s++ = 'w'; |
| 70 | if (c & CEPH_CAP_GBUFFER) |
| 71 | *s++ = 'b'; |
Yan, Zheng | 49a9f4f | 2018-04-25 17:30:23 +0800 | [diff] [blame] | 72 | if (c & CEPH_CAP_GWREXTEND) |
| 73 | *s++ = 'a'; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 74 | if (c & CEPH_CAP_GLAZYIO) |
| 75 | *s++ = 'l'; |
| 76 | return s; |
| 77 | } |
| 78 | |
| 79 | const char *ceph_cap_string(int caps) |
| 80 | { |
| 81 | int i; |
| 82 | char *s; |
| 83 | int c; |
| 84 | |
| 85 | spin_lock(&cap_str_lock); |
| 86 | i = last_cap_str++; |
| 87 | if (last_cap_str == MAX_CAP_STR) |
| 88 | last_cap_str = 0; |
| 89 | spin_unlock(&cap_str_lock); |
| 90 | |
| 91 | s = cap_str[i]; |
| 92 | |
| 93 | if (caps & CEPH_CAP_PIN) |
| 94 | *s++ = 'p'; |
| 95 | |
| 96 | c = (caps >> CEPH_CAP_SAUTH) & 3; |
| 97 | if (c) { |
| 98 | *s++ = 'A'; |
| 99 | s = gcap_string(s, c); |
| 100 | } |
| 101 | |
| 102 | c = (caps >> CEPH_CAP_SLINK) & 3; |
| 103 | if (c) { |
| 104 | *s++ = 'L'; |
| 105 | s = gcap_string(s, c); |
| 106 | } |
| 107 | |
| 108 | c = (caps >> CEPH_CAP_SXATTR) & 3; |
| 109 | if (c) { |
| 110 | *s++ = 'X'; |
| 111 | s = gcap_string(s, c); |
| 112 | } |
| 113 | |
| 114 | c = caps >> CEPH_CAP_SFILE; |
| 115 | if (c) { |
| 116 | *s++ = 'F'; |
| 117 | s = gcap_string(s, c); |
| 118 | } |
| 119 | |
| 120 | if (s == cap_str[i]) |
| 121 | *s++ = '-'; |
| 122 | *s = 0; |
| 123 | return cap_str[i]; |
| 124 | } |
| 125 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 126 | void ceph_caps_init(struct ceph_mds_client *mdsc) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 127 | { |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 128 | INIT_LIST_HEAD(&mdsc->caps_list); |
| 129 | spin_lock_init(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 132 | void ceph_caps_finalize(struct ceph_mds_client *mdsc) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 133 | { |
| 134 | struct ceph_cap *cap; |
| 135 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 136 | spin_lock(&mdsc->caps_list_lock); |
| 137 | while (!list_empty(&mdsc->caps_list)) { |
| 138 | cap = list_first_entry(&mdsc->caps_list, |
| 139 | struct ceph_cap, caps_item); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 140 | list_del(&cap->caps_item); |
| 141 | kmem_cache_free(ceph_cap_cachep, cap); |
| 142 | } |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 143 | mdsc->caps_total_count = 0; |
| 144 | mdsc->caps_avail_count = 0; |
| 145 | mdsc->caps_use_count = 0; |
| 146 | mdsc->caps_reserve_count = 0; |
| 147 | mdsc->caps_min_count = 0; |
| 148 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | 85ccce4 | 2010-02-17 10:02:43 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Yan, Zheng | fe33032 | 2019-02-01 14:57:15 +0800 | [diff] [blame] | 151 | void ceph_adjust_caps_max_min(struct ceph_mds_client *mdsc, |
| 152 | struct ceph_mount_options *fsopt) |
Sage Weil | 85ccce4 | 2010-02-17 10:02:43 -0800 | [diff] [blame] | 153 | { |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 154 | spin_lock(&mdsc->caps_list_lock); |
Yan, Zheng | fe33032 | 2019-02-01 14:57:15 +0800 | [diff] [blame] | 155 | mdsc->caps_min_count = fsopt->max_readdir; |
| 156 | if (mdsc->caps_min_count < 1024) |
| 157 | mdsc->caps_min_count = 1024; |
| 158 | mdsc->caps_use_max = fsopt->caps_max; |
| 159 | if (mdsc->caps_use_max > 0 && |
| 160 | mdsc->caps_use_max < mdsc->caps_min_count) |
| 161 | mdsc->caps_use_max = mdsc->caps_min_count; |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 162 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Chengguang Xu | 7bf8f73 | 2018-07-28 23:15:35 +0800 | [diff] [blame] | 165 | static void __ceph_unreserve_caps(struct ceph_mds_client *mdsc, int nr_caps) |
| 166 | { |
| 167 | struct ceph_cap *cap; |
| 168 | int i; |
| 169 | |
| 170 | if (nr_caps) { |
| 171 | BUG_ON(mdsc->caps_reserve_count < nr_caps); |
| 172 | mdsc->caps_reserve_count -= nr_caps; |
| 173 | if (mdsc->caps_avail_count >= |
| 174 | mdsc->caps_reserve_count + mdsc->caps_min_count) { |
| 175 | mdsc->caps_total_count -= nr_caps; |
| 176 | for (i = 0; i < nr_caps; i++) { |
| 177 | cap = list_first_entry(&mdsc->caps_list, |
| 178 | struct ceph_cap, caps_item); |
| 179 | list_del(&cap->caps_item); |
| 180 | kmem_cache_free(ceph_cap_cachep, cap); |
| 181 | } |
| 182 | } else { |
| 183 | mdsc->caps_avail_count += nr_caps; |
| 184 | } |
| 185 | |
| 186 | dout("%s: caps %d = %d used + %d resv + %d avail\n", |
| 187 | __func__, |
| 188 | mdsc->caps_total_count, mdsc->caps_use_count, |
| 189 | mdsc->caps_reserve_count, mdsc->caps_avail_count); |
| 190 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
| 191 | mdsc->caps_reserve_count + |
| 192 | mdsc->caps_avail_count); |
| 193 | } |
| 194 | } |
| 195 | |
Zhi Zhang | e30ee58 | 2018-01-24 21:24:33 +0800 | [diff] [blame] | 196 | /* |
| 197 | * Called under mdsc->mutex. |
| 198 | */ |
| 199 | int ceph_reserve_caps(struct ceph_mds_client *mdsc, |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 200 | struct ceph_cap_reservation *ctx, int need) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 201 | { |
Zhi Zhang | e30ee58 | 2018-01-24 21:24:33 +0800 | [diff] [blame] | 202 | int i, j; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 203 | struct ceph_cap *cap; |
| 204 | int have; |
| 205 | int alloc = 0; |
Zhi Zhang | e30ee58 | 2018-01-24 21:24:33 +0800 | [diff] [blame] | 206 | int max_caps; |
Chengguang Xu | e5bc08d | 2018-07-28 23:15:37 +0800 | [diff] [blame] | 207 | int err = 0; |
Zhi Zhang | e30ee58 | 2018-01-24 21:24:33 +0800 | [diff] [blame] | 208 | bool trimmed = false; |
| 209 | struct ceph_mds_session *s; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 210 | LIST_HEAD(newcaps); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 211 | |
| 212 | dout("reserve caps ctx=%p need=%d\n", ctx, need); |
| 213 | |
| 214 | /* first reserve any caps that are already allocated */ |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 215 | spin_lock(&mdsc->caps_list_lock); |
| 216 | if (mdsc->caps_avail_count >= need) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 217 | have = need; |
| 218 | else |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 219 | have = mdsc->caps_avail_count; |
| 220 | mdsc->caps_avail_count -= have; |
| 221 | mdsc->caps_reserve_count += have; |
| 222 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
| 223 | mdsc->caps_reserve_count + |
| 224 | mdsc->caps_avail_count); |
| 225 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 226 | |
Chengguang Xu | 79cd674 | 2018-02-24 18:36:02 +0800 | [diff] [blame] | 227 | for (i = have; i < need; ) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 228 | cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS); |
Chengguang Xu | 79cd674 | 2018-02-24 18:36:02 +0800 | [diff] [blame] | 229 | if (cap) { |
| 230 | list_add(&cap->caps_item, &newcaps); |
| 231 | alloc++; |
| 232 | i++; |
| 233 | continue; |
Zhi Zhang | e30ee58 | 2018-01-24 21:24:33 +0800 | [diff] [blame] | 234 | } |
Chengguang Xu | 79cd674 | 2018-02-24 18:36:02 +0800 | [diff] [blame] | 235 | |
| 236 | if (!trimmed) { |
| 237 | for (j = 0; j < mdsc->max_sessions; j++) { |
| 238 | s = __ceph_lookup_mds_session(mdsc, j); |
| 239 | if (!s) |
| 240 | continue; |
| 241 | mutex_unlock(&mdsc->mutex); |
| 242 | |
| 243 | mutex_lock(&s->s_mutex); |
| 244 | max_caps = s->s_nr_caps - (need - i); |
| 245 | ceph_trim_caps(mdsc, s, max_caps); |
| 246 | mutex_unlock(&s->s_mutex); |
| 247 | |
| 248 | ceph_put_mds_session(s); |
| 249 | mutex_lock(&mdsc->mutex); |
| 250 | } |
| 251 | trimmed = true; |
| 252 | |
| 253 | spin_lock(&mdsc->caps_list_lock); |
| 254 | if (mdsc->caps_avail_count) { |
| 255 | int more_have; |
| 256 | if (mdsc->caps_avail_count >= need - i) |
| 257 | more_have = need - i; |
| 258 | else |
| 259 | more_have = mdsc->caps_avail_count; |
| 260 | |
| 261 | i += more_have; |
| 262 | have += more_have; |
| 263 | mdsc->caps_avail_count -= more_have; |
| 264 | mdsc->caps_reserve_count += more_have; |
| 265 | |
| 266 | } |
| 267 | spin_unlock(&mdsc->caps_list_lock); |
| 268 | |
| 269 | continue; |
| 270 | } |
| 271 | |
| 272 | pr_warn("reserve caps ctx=%p ENOMEM need=%d got=%d\n", |
| 273 | ctx, need, have + alloc); |
Chengguang Xu | e5bc08d | 2018-07-28 23:15:37 +0800 | [diff] [blame] | 274 | err = -ENOMEM; |
| 275 | break; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 276 | } |
Chengguang Xu | e5bc08d | 2018-07-28 23:15:37 +0800 | [diff] [blame] | 277 | |
| 278 | if (!err) { |
| 279 | BUG_ON(have + alloc != need); |
| 280 | ctx->count = need; |
Yan, Zheng | fe33032 | 2019-02-01 14:57:15 +0800 | [diff] [blame] | 281 | ctx->used = 0; |
Chengguang Xu | e5bc08d | 2018-07-28 23:15:37 +0800 | [diff] [blame] | 282 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 283 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 284 | spin_lock(&mdsc->caps_list_lock); |
| 285 | mdsc->caps_total_count += alloc; |
| 286 | mdsc->caps_reserve_count += alloc; |
| 287 | list_splice(&newcaps, &mdsc->caps_list); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 288 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 289 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
| 290 | mdsc->caps_reserve_count + |
| 291 | mdsc->caps_avail_count); |
Chengguang Xu | e5bc08d | 2018-07-28 23:15:37 +0800 | [diff] [blame] | 292 | |
| 293 | if (err) |
| 294 | __ceph_unreserve_caps(mdsc, have + alloc); |
| 295 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 296 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 297 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 298 | dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n", |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 299 | ctx, mdsc->caps_total_count, mdsc->caps_use_count, |
| 300 | mdsc->caps_reserve_count, mdsc->caps_avail_count); |
Chengguang Xu | e5bc08d | 2018-07-28 23:15:37 +0800 | [diff] [blame] | 301 | return err; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Chengguang Xu | 7bf8f73 | 2018-07-28 23:15:35 +0800 | [diff] [blame] | 304 | void ceph_unreserve_caps(struct ceph_mds_client *mdsc, |
Yan, Zheng | fe33032 | 2019-02-01 14:57:15 +0800 | [diff] [blame] | 305 | struct ceph_cap_reservation *ctx) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 306 | { |
Yan, Zheng | fe33032 | 2019-02-01 14:57:15 +0800 | [diff] [blame] | 307 | bool reclaim = false; |
| 308 | if (!ctx->count) |
| 309 | return; |
| 310 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 311 | dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count); |
Chengguang Xu | 7bf8f73 | 2018-07-28 23:15:35 +0800 | [diff] [blame] | 312 | spin_lock(&mdsc->caps_list_lock); |
| 313 | __ceph_unreserve_caps(mdsc, ctx->count); |
| 314 | ctx->count = 0; |
Yan, Zheng | fe33032 | 2019-02-01 14:57:15 +0800 | [diff] [blame] | 315 | |
| 316 | if (mdsc->caps_use_max > 0 && |
| 317 | mdsc->caps_use_count > mdsc->caps_use_max) |
| 318 | reclaim = true; |
Chengguang Xu | 7bf8f73 | 2018-07-28 23:15:35 +0800 | [diff] [blame] | 319 | spin_unlock(&mdsc->caps_list_lock); |
Yan, Zheng | fe33032 | 2019-02-01 14:57:15 +0800 | [diff] [blame] | 320 | |
| 321 | if (reclaim) |
| 322 | ceph_reclaim_caps_nr(mdsc, ctx->used); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 325 | struct ceph_cap *ceph_get_cap(struct ceph_mds_client *mdsc, |
| 326 | struct ceph_cap_reservation *ctx) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 327 | { |
| 328 | struct ceph_cap *cap = NULL; |
| 329 | |
| 330 | /* temporary, until we do something about cap import/export */ |
Sage Weil | 443b376 | 2010-06-29 09:28:39 -0700 | [diff] [blame] | 331 | if (!ctx) { |
| 332 | cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS); |
| 333 | if (cap) { |
Yan, Zheng | 4d1d0534 | 2012-11-03 10:32:37 +0800 | [diff] [blame] | 334 | spin_lock(&mdsc->caps_list_lock); |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 335 | mdsc->caps_use_count++; |
| 336 | mdsc->caps_total_count++; |
Yan, Zheng | 4d1d0534 | 2012-11-03 10:32:37 +0800 | [diff] [blame] | 337 | spin_unlock(&mdsc->caps_list_lock); |
Chengguang Xu | e327ce0 | 2018-02-24 18:35:29 +0800 | [diff] [blame] | 338 | } else { |
| 339 | spin_lock(&mdsc->caps_list_lock); |
| 340 | if (mdsc->caps_avail_count) { |
| 341 | BUG_ON(list_empty(&mdsc->caps_list)); |
| 342 | |
| 343 | mdsc->caps_avail_count--; |
| 344 | mdsc->caps_use_count++; |
| 345 | cap = list_first_entry(&mdsc->caps_list, |
| 346 | struct ceph_cap, caps_item); |
| 347 | list_del(&cap->caps_item); |
| 348 | |
| 349 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
| 350 | mdsc->caps_reserve_count + mdsc->caps_avail_count); |
| 351 | } |
| 352 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | 443b376 | 2010-06-29 09:28:39 -0700 | [diff] [blame] | 353 | } |
Chengguang Xu | e327ce0 | 2018-02-24 18:35:29 +0800 | [diff] [blame] | 354 | |
Sage Weil | 443b376 | 2010-06-29 09:28:39 -0700 | [diff] [blame] | 355 | return cap; |
| 356 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 357 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 358 | spin_lock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 359 | dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n", |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 360 | ctx, ctx->count, mdsc->caps_total_count, mdsc->caps_use_count, |
| 361 | mdsc->caps_reserve_count, mdsc->caps_avail_count); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 362 | BUG_ON(!ctx->count); |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 363 | BUG_ON(ctx->count > mdsc->caps_reserve_count); |
| 364 | BUG_ON(list_empty(&mdsc->caps_list)); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 365 | |
| 366 | ctx->count--; |
Yan, Zheng | fe33032 | 2019-02-01 14:57:15 +0800 | [diff] [blame] | 367 | ctx->used++; |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 368 | mdsc->caps_reserve_count--; |
| 369 | mdsc->caps_use_count++; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 370 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 371 | cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 372 | list_del(&cap->caps_item); |
| 373 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 374 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
| 375 | mdsc->caps_reserve_count + mdsc->caps_avail_count); |
| 376 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 377 | return cap; |
| 378 | } |
| 379 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 380 | void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 381 | { |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 382 | spin_lock(&mdsc->caps_list_lock); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 383 | dout("put_cap %p %d = %d used + %d resv + %d avail\n", |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 384 | cap, mdsc->caps_total_count, mdsc->caps_use_count, |
| 385 | mdsc->caps_reserve_count, mdsc->caps_avail_count); |
| 386 | mdsc->caps_use_count--; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 387 | /* |
Sage Weil | 85ccce4 | 2010-02-17 10:02:43 -0800 | [diff] [blame] | 388 | * Keep some preallocated caps around (ceph_min_count), to |
| 389 | * avoid lots of free/alloc churn. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 390 | */ |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 391 | if (mdsc->caps_avail_count >= mdsc->caps_reserve_count + |
| 392 | mdsc->caps_min_count) { |
| 393 | mdsc->caps_total_count--; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 394 | kmem_cache_free(ceph_cap_cachep, cap); |
| 395 | } else { |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 396 | mdsc->caps_avail_count++; |
| 397 | list_add(&cap->caps_item, &mdsc->caps_list); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 400 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
| 401 | mdsc->caps_reserve_count + mdsc->caps_avail_count); |
| 402 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 405 | void ceph_reservation_status(struct ceph_fs_client *fsc, |
Sage Weil | 85ccce4 | 2010-02-17 10:02:43 -0800 | [diff] [blame] | 406 | int *total, int *avail, int *used, int *reserved, |
| 407 | int *min) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 408 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 409 | struct ceph_mds_client *mdsc = fsc->mdsc; |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 410 | |
Chengguang Xu | b884014 | 2018-02-23 17:09:38 +0800 | [diff] [blame] | 411 | spin_lock(&mdsc->caps_list_lock); |
| 412 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 413 | if (total) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 414 | *total = mdsc->caps_total_count; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 415 | if (avail) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 416 | *avail = mdsc->caps_avail_count; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 417 | if (used) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 418 | *used = mdsc->caps_use_count; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 419 | if (reserved) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 420 | *reserved = mdsc->caps_reserve_count; |
Sage Weil | 85ccce4 | 2010-02-17 10:02:43 -0800 | [diff] [blame] | 421 | if (min) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 422 | *min = mdsc->caps_min_count; |
Chengguang Xu | b884014 | 2018-02-23 17:09:38 +0800 | [diff] [blame] | 423 | |
| 424 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /* |
| 428 | * Find ceph_cap for given mds, if any. |
| 429 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 430 | * Called with i_ceph_lock held. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 431 | */ |
| 432 | static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds) |
| 433 | { |
| 434 | struct ceph_cap *cap; |
| 435 | struct rb_node *n = ci->i_caps.rb_node; |
| 436 | |
| 437 | while (n) { |
| 438 | cap = rb_entry(n, struct ceph_cap, ci_node); |
| 439 | if (mds < cap->mds) |
| 440 | n = n->rb_left; |
| 441 | else if (mds > cap->mds) |
| 442 | n = n->rb_right; |
| 443 | else |
| 444 | return cap; |
| 445 | } |
| 446 | return NULL; |
| 447 | } |
| 448 | |
Greg Farnum | 2bc5025 | 2010-06-30 12:44:34 -0700 | [diff] [blame] | 449 | struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds) |
| 450 | { |
| 451 | struct ceph_cap *cap; |
| 452 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 453 | spin_lock(&ci->i_ceph_lock); |
Greg Farnum | 2bc5025 | 2010-06-30 12:44:34 -0700 | [diff] [blame] | 454 | cap = __get_cap_for_mds(ci, mds); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 455 | spin_unlock(&ci->i_ceph_lock); |
Greg Farnum | 2bc5025 | 2010-06-30 12:44:34 -0700 | [diff] [blame] | 456 | return cap; |
| 457 | } |
| 458 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 459 | /* |
Sage Weil | 33caad3 | 2010-05-26 14:31:27 -0700 | [diff] [blame] | 460 | * Return id of any MDS with a cap, preferably FILE_WR|BUFFER|EXCL, else -1. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 461 | */ |
Sage Weil | ca81f3f | 2010-06-10 14:21:36 -0700 | [diff] [blame] | 462 | static int __ceph_get_cap_mds(struct ceph_inode_info *ci) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 463 | { |
| 464 | struct ceph_cap *cap; |
| 465 | int mds = -1; |
| 466 | struct rb_node *p; |
| 467 | |
Sage Weil | 33caad3 | 2010-05-26 14:31:27 -0700 | [diff] [blame] | 468 | /* prefer mds with WR|BUFFER|EXCL caps */ |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 469 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 470 | cap = rb_entry(p, struct ceph_cap, ci_node); |
| 471 | mds = cap->mds; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 472 | if (cap->issued & (CEPH_CAP_FILE_WR | |
| 473 | CEPH_CAP_FILE_BUFFER | |
| 474 | CEPH_CAP_FILE_EXCL)) |
| 475 | break; |
| 476 | } |
| 477 | return mds; |
| 478 | } |
| 479 | |
| 480 | int ceph_get_cap_mds(struct inode *inode) |
| 481 | { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 482 | struct ceph_inode_info *ci = ceph_inode(inode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 483 | int mds; |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 484 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | ca81f3f | 2010-06-10 14:21:36 -0700 | [diff] [blame] | 485 | mds = __ceph_get_cap_mds(ceph_inode(inode)); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 486 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 487 | return mds; |
| 488 | } |
| 489 | |
| 490 | /* |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 491 | * Called under i_ceph_lock. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 492 | */ |
| 493 | static void __insert_cap_node(struct ceph_inode_info *ci, |
| 494 | struct ceph_cap *new) |
| 495 | { |
| 496 | struct rb_node **p = &ci->i_caps.rb_node; |
| 497 | struct rb_node *parent = NULL; |
| 498 | struct ceph_cap *cap = NULL; |
| 499 | |
| 500 | while (*p) { |
| 501 | parent = *p; |
| 502 | cap = rb_entry(parent, struct ceph_cap, ci_node); |
| 503 | if (new->mds < cap->mds) |
| 504 | p = &(*p)->rb_left; |
| 505 | else if (new->mds > cap->mds) |
| 506 | p = &(*p)->rb_right; |
| 507 | else |
| 508 | BUG(); |
| 509 | } |
| 510 | |
| 511 | rb_link_node(&new->ci_node, parent, p); |
| 512 | rb_insert_color(&new->ci_node, &ci->i_caps); |
| 513 | } |
| 514 | |
| 515 | /* |
| 516 | * (re)set cap hold timeouts, which control the delayed release |
| 517 | * of unused caps back to the MDS. Should be called on cap use. |
| 518 | */ |
| 519 | static void __cap_set_timeouts(struct ceph_mds_client *mdsc, |
| 520 | struct ceph_inode_info *ci) |
| 521 | { |
Yan, Zheng | fe33032 | 2019-02-01 14:57:15 +0800 | [diff] [blame] | 522 | struct ceph_mount_options *opt = mdsc->fsc->mount_options; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 523 | |
| 524 | ci->i_hold_caps_min = round_jiffies(jiffies + |
Yan, Zheng | fe33032 | 2019-02-01 14:57:15 +0800 | [diff] [blame] | 525 | opt->caps_wanted_delay_min * HZ); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 526 | ci->i_hold_caps_max = round_jiffies(jiffies + |
Yan, Zheng | fe33032 | 2019-02-01 14:57:15 +0800 | [diff] [blame] | 527 | opt->caps_wanted_delay_max * HZ); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 528 | dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode, |
| 529 | ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies); |
| 530 | } |
| 531 | |
| 532 | /* |
| 533 | * (Re)queue cap at the end of the delayed cap release list. |
| 534 | * |
| 535 | * If I_FLUSH is set, leave the inode at the front of the list. |
| 536 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 537 | * Caller holds i_ceph_lock |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 538 | * -> we take mdsc->cap_delay_lock |
| 539 | */ |
| 540 | static void __cap_delay_requeue(struct ceph_mds_client *mdsc, |
Xuehan Xu | 6680288 | 2018-10-11 17:55:39 +0800 | [diff] [blame] | 541 | struct ceph_inode_info *ci, |
| 542 | bool set_timeout) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 543 | { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 544 | dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode, |
| 545 | ci->i_ceph_flags, ci->i_hold_caps_max); |
| 546 | if (!mdsc->stopping) { |
| 547 | spin_lock(&mdsc->cap_delay_lock); |
| 548 | if (!list_empty(&ci->i_cap_delay_list)) { |
| 549 | if (ci->i_ceph_flags & CEPH_I_FLUSH) |
| 550 | goto no_change; |
| 551 | list_del_init(&ci->i_cap_delay_list); |
| 552 | } |
Xuehan Xu | 6680288 | 2018-10-11 17:55:39 +0800 | [diff] [blame] | 553 | if (set_timeout) |
| 554 | __cap_set_timeouts(mdsc, ci); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 555 | list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list); |
| 556 | no_change: |
| 557 | spin_unlock(&mdsc->cap_delay_lock); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | /* |
| 562 | * Queue an inode for immediate writeback. Mark inode with I_FLUSH, |
| 563 | * indicating we should send a cap message to flush dirty metadata |
| 564 | * asap, and move to the front of the delayed cap list. |
| 565 | */ |
| 566 | static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc, |
| 567 | struct ceph_inode_info *ci) |
| 568 | { |
| 569 | dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode); |
| 570 | spin_lock(&mdsc->cap_delay_lock); |
| 571 | ci->i_ceph_flags |= CEPH_I_FLUSH; |
| 572 | if (!list_empty(&ci->i_cap_delay_list)) |
| 573 | list_del_init(&ci->i_cap_delay_list); |
| 574 | list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list); |
| 575 | spin_unlock(&mdsc->cap_delay_lock); |
| 576 | } |
| 577 | |
| 578 | /* |
| 579 | * Cancel delayed work on cap. |
| 580 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 581 | * Caller must hold i_ceph_lock. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 582 | */ |
| 583 | static void __cap_delay_cancel(struct ceph_mds_client *mdsc, |
| 584 | struct ceph_inode_info *ci) |
| 585 | { |
| 586 | dout("__cap_delay_cancel %p\n", &ci->vfs_inode); |
| 587 | if (list_empty(&ci->i_cap_delay_list)) |
| 588 | return; |
| 589 | spin_lock(&mdsc->cap_delay_lock); |
| 590 | list_del_init(&ci->i_cap_delay_list); |
| 591 | spin_unlock(&mdsc->cap_delay_lock); |
| 592 | } |
| 593 | |
| 594 | /* |
| 595 | * Common issue checks for add_cap, handle_cap_grant. |
| 596 | */ |
| 597 | static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap, |
| 598 | unsigned issued) |
| 599 | { |
| 600 | unsigned had = __ceph_caps_issued(ci, NULL); |
| 601 | |
| 602 | /* |
| 603 | * Each time we receive FILE_CACHE anew, we increment |
| 604 | * i_rdcache_gen. |
| 605 | */ |
Sage Weil | 2962507 | 2010-05-27 10:40:43 -0700 | [diff] [blame] | 606 | if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) && |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 607 | (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 608 | ci->i_rdcache_gen++; |
Milosz Tanski | 99ccbd2 | 2013-08-21 17:29:54 -0400 | [diff] [blame] | 609 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 610 | |
| 611 | /* |
Yan, Zheng | 15b51bd | 2017-09-06 10:15:16 +0800 | [diff] [blame] | 612 | * If FILE_SHARED is newly issued, mark dir not complete. We don't |
| 613 | * know what happened to this directory while we didn't have the cap. |
| 614 | * If FILE_SHARED is being revoked, also mark dir not complete. It |
| 615 | * stops on-going cached readdir. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 616 | */ |
Yan, Zheng | 15b51bd | 2017-09-06 10:15:16 +0800 | [diff] [blame] | 617 | if ((issued & CEPH_CAP_FILE_SHARED) != (had & CEPH_CAP_FILE_SHARED)) { |
| 618 | if (issued & CEPH_CAP_FILE_SHARED) |
Yan, Zheng | 97aeb6b | 2017-11-27 10:47:46 +0800 | [diff] [blame] | 619 | atomic_inc(&ci->i_shared_gen); |
Yan, Zheng | a8673d6 | 2013-02-18 16:38:14 +0800 | [diff] [blame] | 620 | if (S_ISDIR(ci->vfs_inode.i_mode)) { |
| 621 | dout(" marking %p NOT complete\n", &ci->vfs_inode); |
Yan, Zheng | 2f276c5 | 2013-03-13 19:44:32 +0800 | [diff] [blame] | 622 | __ceph_dir_clear_complete(ci); |
Yan, Zheng | a8673d6 | 2013-02-18 16:38:14 +0800 | [diff] [blame] | 623 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
| 627 | /* |
| 628 | * Add a capability under the given MDS session. |
| 629 | * |
| 630 | * Caller should hold session snap_rwsem (read) and s_mutex. |
| 631 | * |
| 632 | * @fmode is the open file mode, if we are opening a file, otherwise |
| 633 | * it is < 0. (This is so we can atomically add the cap and add an |
| 634 | * open file reference to it.) |
| 635 | */ |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 636 | void ceph_add_cap(struct inode *inode, |
| 637 | struct ceph_mds_session *session, u64 cap_id, |
| 638 | int fmode, unsigned issued, unsigned wanted, |
| 639 | unsigned seq, unsigned mseq, u64 realmino, int flags, |
| 640 | struct ceph_cap **new_cap) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 641 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 642 | struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 643 | struct ceph_inode_info *ci = ceph_inode(inode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 644 | struct ceph_cap *cap; |
| 645 | int mds = session->s_mds; |
| 646 | int actual_wanted; |
| 647 | |
| 648 | dout("add_cap %p mds%d cap %llx %s seq %d\n", inode, |
| 649 | session->s_mds, cap_id, ceph_cap_string(issued), seq); |
| 650 | |
| 651 | /* |
| 652 | * If we are opening the file, include file mode wanted bits |
| 653 | * in wanted. |
| 654 | */ |
| 655 | if (fmode >= 0) |
| 656 | wanted |= ceph_caps_for_mode(fmode); |
| 657 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 658 | cap = __get_cap_for_mds(ci, mds); |
| 659 | if (!cap) { |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 660 | cap = *new_cap; |
| 661 | *new_cap = NULL; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 662 | |
| 663 | cap->issued = 0; |
| 664 | cap->implemented = 0; |
| 665 | cap->mds = mds; |
| 666 | cap->mds_wanted = 0; |
Yan, Zheng | 964266c | 2013-02-27 09:26:09 +0800 | [diff] [blame] | 667 | cap->mseq = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 668 | |
| 669 | cap->ci = ci; |
| 670 | __insert_cap_node(ci, cap); |
| 671 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 672 | /* add to session cap list */ |
| 673 | cap->session = session; |
| 674 | spin_lock(&session->s_cap_lock); |
| 675 | list_add_tail(&cap->session_caps, &session->s_caps); |
| 676 | session->s_nr_caps++; |
| 677 | spin_unlock(&session->s_cap_lock); |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 678 | } else { |
Yan, Zheng | 32f6511 | 2019-01-23 11:20:00 +0800 | [diff] [blame] | 679 | spin_lock(&session->s_cap_lock); |
| 680 | list_move_tail(&cap->session_caps, &session->s_caps); |
| 681 | spin_unlock(&session->s_cap_lock); |
| 682 | |
Yan, Zheng | d2f8bb2 | 2018-12-10 16:35:09 +0800 | [diff] [blame] | 683 | if (cap->cap_gen < session->s_cap_gen) |
| 684 | cap->issued = cap->implemented = CEPH_CAP_PIN; |
| 685 | |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 686 | /* |
| 687 | * auth mds of the inode changed. we received the cap export |
| 688 | * message, but still haven't received the cap import message. |
| 689 | * handle_cap_export() updated the new auth MDS' cap. |
| 690 | * |
| 691 | * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing |
| 692 | * a message that was send before the cap import message. So |
| 693 | * don't remove caps. |
| 694 | */ |
| 695 | if (ceph_seq_cmp(seq, cap->seq) <= 0) { |
| 696 | WARN_ON(cap != ci->i_auth_cap); |
| 697 | WARN_ON(cap->cap_id != cap_id); |
| 698 | seq = cap->seq; |
| 699 | mseq = cap->mseq; |
| 700 | issued |= cap->issued; |
| 701 | flags |= CEPH_CAP_FLAG_AUTH; |
| 702 | } |
| 703 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 704 | |
Yan, Zheng | 7d9c919 | 2017-12-19 18:00:54 +0800 | [diff] [blame] | 705 | if (!ci->i_snap_realm || |
| 706 | ((flags & CEPH_CAP_FLAG_AUTH) && |
| 707 | realmino != (u64)-1 && ci->i_snap_realm->ino != realmino)) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 708 | /* |
| 709 | * add this inode to the appropriate snap realm |
| 710 | */ |
| 711 | struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc, |
| 712 | realmino); |
| 713 | if (realm) { |
Yan, Zheng | 7d9c919 | 2017-12-19 18:00:54 +0800 | [diff] [blame] | 714 | struct ceph_snap_realm *oldrealm = ci->i_snap_realm; |
| 715 | if (oldrealm) { |
| 716 | spin_lock(&oldrealm->inodes_with_caps_lock); |
| 717 | list_del_init(&ci->i_snap_realm_item); |
| 718 | spin_unlock(&oldrealm->inodes_with_caps_lock); |
| 719 | } |
| 720 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 721 | spin_lock(&realm->inodes_with_caps_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 722 | list_add(&ci->i_snap_realm_item, |
| 723 | &realm->inodes_with_caps); |
Luis Henriques | e3161f1 | 2018-01-12 17:19:28 +0000 | [diff] [blame] | 724 | ci->i_snap_realm = realm; |
| 725 | if (realm->ino == ci->i_vino.ino) |
| 726 | realm->inode = inode; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 727 | spin_unlock(&realm->inodes_with_caps_lock); |
Yan, Zheng | 7d9c919 | 2017-12-19 18:00:54 +0800 | [diff] [blame] | 728 | |
| 729 | if (oldrealm) |
| 730 | ceph_put_snap_realm(mdsc, oldrealm); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 731 | } else { |
| 732 | pr_err("ceph_add_cap: couldn't find snap realm %llx\n", |
| 733 | realmino); |
Sage Weil | b8cd07e | 2010-07-16 12:00:02 -0700 | [diff] [blame] | 734 | WARN_ON(!realm); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | |
| 738 | __check_cap_issue(ci, cap, issued); |
| 739 | |
| 740 | /* |
| 741 | * If we are issued caps we don't want, or the mds' wanted |
| 742 | * value appears to be off, queue a check so we'll release |
| 743 | * later and/or update the mds wanted value. |
| 744 | */ |
| 745 | actual_wanted = __ceph_caps_wanted(ci); |
| 746 | if ((wanted & ~actual_wanted) || |
| 747 | (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) { |
| 748 | dout(" issued %s, mds wanted %s, actual %s, queueing\n", |
| 749 | ceph_cap_string(issued), ceph_cap_string(wanted), |
| 750 | ceph_cap_string(actual_wanted)); |
Xuehan Xu | 6680288 | 2018-10-11 17:55:39 +0800 | [diff] [blame] | 751 | __cap_delay_requeue(mdsc, ci, true); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Yan, Zheng | b8c2f3a | 2013-05-31 16:37:11 +0800 | [diff] [blame] | 754 | if (flags & CEPH_CAP_FLAG_AUTH) { |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 755 | if (!ci->i_auth_cap || |
Yan, Zheng | d9ffc4f | 2014-03-18 10:15:29 +0800 | [diff] [blame] | 756 | ceph_seq_cmp(ci->i_auth_cap->mseq, mseq) < 0) { |
Yan, Zheng | b8c2f3a | 2013-05-31 16:37:11 +0800 | [diff] [blame] | 757 | ci->i_auth_cap = cap; |
Yan, Zheng | d9ffc4f | 2014-03-18 10:15:29 +0800 | [diff] [blame] | 758 | cap->mds_wanted = wanted; |
| 759 | } |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 760 | } else { |
| 761 | WARN_ON(ci->i_auth_cap == cap); |
Yan, Zheng | 8a92a11 | 2013-01-04 14:28:07 +0800 | [diff] [blame] | 762 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 763 | |
| 764 | dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n", |
| 765 | inode, ceph_vinop(inode), cap, ceph_cap_string(issued), |
| 766 | ceph_cap_string(issued|cap->issued), seq, mds); |
| 767 | cap->cap_id = cap_id; |
| 768 | cap->issued = issued; |
| 769 | cap->implemented |= issued; |
Yan, Zheng | d1b8780 | 2013-11-13 14:47:19 +0800 | [diff] [blame] | 770 | if (ceph_seq_cmp(mseq, cap->mseq) > 0) |
Yan, Zheng | 964266c | 2013-02-27 09:26:09 +0800 | [diff] [blame] | 771 | cap->mds_wanted = wanted; |
| 772 | else |
| 773 | cap->mds_wanted |= wanted; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 774 | cap->seq = seq; |
| 775 | cap->issue_seq = seq; |
| 776 | cap->mseq = mseq; |
Sage Weil | 685f9a5d | 2009-11-09 12:05:48 -0800 | [diff] [blame] | 777 | cap->cap_gen = session->s_cap_gen; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 778 | |
| 779 | if (fmode >= 0) |
| 780 | __ceph_get_fmode(ci, fmode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | /* |
| 784 | * Return true if cap has not timed out and belongs to the current |
| 785 | * generation of the MDS session (i.e. has not gone 'stale' due to |
| 786 | * us losing touch with the mds). |
| 787 | */ |
| 788 | static int __cap_is_valid(struct ceph_cap *cap) |
| 789 | { |
| 790 | unsigned long ttl; |
Sage Weil | cdac830 | 2009-11-10 16:02:23 -0800 | [diff] [blame] | 791 | u32 gen; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 792 | |
Alex Elder | d8fb02a | 2012-01-12 17:48:10 -0800 | [diff] [blame] | 793 | spin_lock(&cap->session->s_gen_ttl_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 794 | gen = cap->session->s_cap_gen; |
| 795 | ttl = cap->session->s_cap_ttl; |
Alex Elder | d8fb02a | 2012-01-12 17:48:10 -0800 | [diff] [blame] | 796 | spin_unlock(&cap->session->s_gen_ttl_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 797 | |
Sage Weil | 685f9a5d | 2009-11-09 12:05:48 -0800 | [diff] [blame] | 798 | if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 799 | dout("__cap_is_valid %p cap %p issued %s " |
| 800 | "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode, |
Sage Weil | 685f9a5d | 2009-11-09 12:05:48 -0800 | [diff] [blame] | 801 | cap, ceph_cap_string(cap->issued), cap->cap_gen, gen); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 802 | return 0; |
| 803 | } |
| 804 | |
| 805 | return 1; |
| 806 | } |
| 807 | |
| 808 | /* |
| 809 | * Return set of valid cap bits issued to us. Note that caps time |
| 810 | * out, and may be invalidated in bulk if the client session times out |
| 811 | * and session->s_cap_gen is bumped. |
| 812 | */ |
| 813 | int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented) |
| 814 | { |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 815 | int have = ci->i_snap_caps; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 816 | struct ceph_cap *cap; |
| 817 | struct rb_node *p; |
| 818 | |
| 819 | if (implemented) |
| 820 | *implemented = 0; |
| 821 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 822 | cap = rb_entry(p, struct ceph_cap, ci_node); |
| 823 | if (!__cap_is_valid(cap)) |
| 824 | continue; |
| 825 | dout("__ceph_caps_issued %p cap %p issued %s\n", |
| 826 | &ci->vfs_inode, cap, ceph_cap_string(cap->issued)); |
| 827 | have |= cap->issued; |
| 828 | if (implemented) |
| 829 | *implemented |= cap->implemented; |
| 830 | } |
Yan, Zheng | b1530f5 | 2013-07-02 12:40:20 +0800 | [diff] [blame] | 831 | /* |
| 832 | * exclude caps issued by non-auth MDS, but are been revoking |
| 833 | * by the auth MDS. The non-auth MDS should be revoking/exporting |
| 834 | * these caps, but the message is delayed. |
| 835 | */ |
| 836 | if (ci->i_auth_cap) { |
| 837 | cap = ci->i_auth_cap; |
| 838 | have &= ~cap->implemented | cap->issued; |
| 839 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 840 | return have; |
| 841 | } |
| 842 | |
| 843 | /* |
| 844 | * Get cap bits issued by caps other than @ocap |
| 845 | */ |
| 846 | int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap) |
| 847 | { |
| 848 | int have = ci->i_snap_caps; |
| 849 | struct ceph_cap *cap; |
| 850 | struct rb_node *p; |
| 851 | |
| 852 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 853 | cap = rb_entry(p, struct ceph_cap, ci_node); |
| 854 | if (cap == ocap) |
| 855 | continue; |
| 856 | if (!__cap_is_valid(cap)) |
| 857 | continue; |
| 858 | have |= cap->issued; |
| 859 | } |
| 860 | return have; |
| 861 | } |
| 862 | |
| 863 | /* |
| 864 | * Move a cap to the end of the LRU (oldest caps at list head, newest |
| 865 | * at list tail). |
| 866 | */ |
| 867 | static void __touch_cap(struct ceph_cap *cap) |
| 868 | { |
| 869 | struct ceph_mds_session *s = cap->session; |
| 870 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 871 | spin_lock(&s->s_cap_lock); |
Markus Elfring | d37b1d9 | 2017-08-20 20:22:02 +0200 | [diff] [blame] | 872 | if (!s->s_cap_iterator) { |
Sage Weil | 5dacf09 | 2009-12-21 20:40:34 -0800 | [diff] [blame] | 873 | dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap, |
| 874 | s->s_mds); |
| 875 | list_move_tail(&cap->session_caps, &s->s_caps); |
| 876 | } else { |
| 877 | dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n", |
| 878 | &cap->ci->vfs_inode, cap, s->s_mds); |
| 879 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 880 | spin_unlock(&s->s_cap_lock); |
| 881 | } |
| 882 | |
| 883 | /* |
| 884 | * Check if we hold the given mask. If so, move the cap(s) to the |
| 885 | * front of their respective LRUs. (This is the preferred way for |
| 886 | * callers to check for caps they want.) |
| 887 | */ |
| 888 | int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch) |
| 889 | { |
| 890 | struct ceph_cap *cap; |
| 891 | struct rb_node *p; |
| 892 | int have = ci->i_snap_caps; |
| 893 | |
| 894 | if ((have & mask) == mask) { |
Jeff Layton | 5ddc61f | 2019-04-23 13:40:02 -0400 | [diff] [blame^] | 895 | dout("__ceph_caps_issued_mask ino 0x%lx snap issued %s" |
| 896 | " (mask %s)\n", ci->vfs_inode.i_ino, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 897 | ceph_cap_string(have), |
| 898 | ceph_cap_string(mask)); |
| 899 | return 1; |
| 900 | } |
| 901 | |
| 902 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 903 | cap = rb_entry(p, struct ceph_cap, ci_node); |
| 904 | if (!__cap_is_valid(cap)) |
| 905 | continue; |
| 906 | if ((cap->issued & mask) == mask) { |
Jeff Layton | 5ddc61f | 2019-04-23 13:40:02 -0400 | [diff] [blame^] | 907 | dout("__ceph_caps_issued_mask ino 0x%lx cap %p issued %s" |
| 908 | " (mask %s)\n", ci->vfs_inode.i_ino, cap, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 909 | ceph_cap_string(cap->issued), |
| 910 | ceph_cap_string(mask)); |
| 911 | if (touch) |
| 912 | __touch_cap(cap); |
| 913 | return 1; |
| 914 | } |
| 915 | |
| 916 | /* does a combination of caps satisfy mask? */ |
| 917 | have |= cap->issued; |
| 918 | if ((have & mask) == mask) { |
Jeff Layton | 5ddc61f | 2019-04-23 13:40:02 -0400 | [diff] [blame^] | 919 | dout("__ceph_caps_issued_mask ino 0x%lx combo issued %s" |
| 920 | " (mask %s)\n", ci->vfs_inode.i_ino, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 921 | ceph_cap_string(cap->issued), |
| 922 | ceph_cap_string(mask)); |
| 923 | if (touch) { |
| 924 | struct rb_node *q; |
| 925 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 926 | /* touch this + preceding caps */ |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 927 | __touch_cap(cap); |
| 928 | for (q = rb_first(&ci->i_caps); q != p; |
| 929 | q = rb_next(q)) { |
| 930 | cap = rb_entry(q, struct ceph_cap, |
| 931 | ci_node); |
| 932 | if (!__cap_is_valid(cap)) |
| 933 | continue; |
| 934 | __touch_cap(cap); |
| 935 | } |
| 936 | } |
| 937 | return 1; |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | return 0; |
| 942 | } |
| 943 | |
| 944 | /* |
| 945 | * Return true if mask caps are currently being revoked by an MDS. |
| 946 | */ |
Yan, Zheng | 6ee6b953 | 2013-07-02 12:40:21 +0800 | [diff] [blame] | 947 | int __ceph_caps_revoking_other(struct ceph_inode_info *ci, |
| 948 | struct ceph_cap *ocap, int mask) |
| 949 | { |
| 950 | struct ceph_cap *cap; |
| 951 | struct rb_node *p; |
| 952 | |
| 953 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 954 | cap = rb_entry(p, struct ceph_cap, ci_node); |
Yan, Zheng | 9563f88 | 2013-11-22 13:50:45 +0800 | [diff] [blame] | 955 | if (cap != ocap && |
Yan, Zheng | 6ee6b953 | 2013-07-02 12:40:21 +0800 | [diff] [blame] | 956 | (cap->implemented & ~cap->issued & mask)) |
| 957 | return 1; |
| 958 | } |
| 959 | return 0; |
| 960 | } |
| 961 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 962 | int ceph_caps_revoking(struct ceph_inode_info *ci, int mask) |
| 963 | { |
| 964 | struct inode *inode = &ci->vfs_inode; |
Yan, Zheng | 6ee6b953 | 2013-07-02 12:40:21 +0800 | [diff] [blame] | 965 | int ret; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 966 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 967 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | 6ee6b953 | 2013-07-02 12:40:21 +0800 | [diff] [blame] | 968 | ret = __ceph_caps_revoking_other(ci, NULL, mask); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 969 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 970 | dout("ceph_caps_revoking %p %s = %d\n", inode, |
| 971 | ceph_cap_string(mask), ret); |
| 972 | return ret; |
| 973 | } |
| 974 | |
| 975 | int __ceph_caps_used(struct ceph_inode_info *ci) |
| 976 | { |
| 977 | int used = 0; |
| 978 | if (ci->i_pin_ref) |
| 979 | used |= CEPH_CAP_PIN; |
| 980 | if (ci->i_rd_ref) |
| 981 | used |= CEPH_CAP_FILE_RD; |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 982 | if (ci->i_rdcache_ref || |
| 983 | (!S_ISDIR(ci->vfs_inode.i_mode) && /* ignore readdir cache */ |
| 984 | ci->vfs_inode.i_data.nrpages)) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 985 | used |= CEPH_CAP_FILE_CACHE; |
| 986 | if (ci->i_wr_ref) |
| 987 | used |= CEPH_CAP_FILE_WR; |
Henry C Chang | d3d0720 | 2011-05-11 10:29:54 +0000 | [diff] [blame] | 988 | if (ci->i_wb_ref || ci->i_wrbuffer_ref) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 989 | used |= CEPH_CAP_FILE_BUFFER; |
| 990 | return used; |
| 991 | } |
| 992 | |
| 993 | /* |
| 994 | * wanted, by virtue of open file modes |
| 995 | */ |
| 996 | int __ceph_caps_file_wanted(struct ceph_inode_info *ci) |
| 997 | { |
Yan, Zheng | 774a6a1 | 2016-06-06 16:01:39 +0800 | [diff] [blame] | 998 | int i, bits = 0; |
| 999 | for (i = 0; i < CEPH_FILE_MODE_BITS; i++) { |
| 1000 | if (ci->i_nr_by_mode[i]) |
| 1001 | bits |= 1 << i; |
| 1002 | } |
| 1003 | if (bits == 0) |
| 1004 | return 0; |
| 1005 | return ceph_caps_for_mode(bits >> 1); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | /* |
| 1009 | * Return caps we have registered with the MDS(s) as 'wanted'. |
| 1010 | */ |
Yan, Zheng | c1944fe | 2017-01-29 22:15:47 +0800 | [diff] [blame] | 1011 | int __ceph_caps_mds_wanted(struct ceph_inode_info *ci, bool check) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1012 | { |
| 1013 | struct ceph_cap *cap; |
| 1014 | struct rb_node *p; |
| 1015 | int mds_wanted = 0; |
| 1016 | |
| 1017 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 1018 | cap = rb_entry(p, struct ceph_cap, ci_node); |
Yan, Zheng | c1944fe | 2017-01-29 22:15:47 +0800 | [diff] [blame] | 1019 | if (check && !__cap_is_valid(cap)) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1020 | continue; |
Yan, Zheng | a255060 | 2014-03-08 09:51:45 +0800 | [diff] [blame] | 1021 | if (cap == ci->i_auth_cap) |
| 1022 | mds_wanted |= cap->mds_wanted; |
| 1023 | else |
| 1024 | mds_wanted |= (cap->mds_wanted & ~CEPH_CAP_ANY_FILE_WR); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1025 | } |
| 1026 | return mds_wanted; |
| 1027 | } |
| 1028 | |
| 1029 | /* |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1030 | * called under i_ceph_lock |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1031 | */ |
Yan, Zheng | 0f439c7 | 2018-01-08 14:44:10 +0800 | [diff] [blame] | 1032 | static int __ceph_is_single_caps(struct ceph_inode_info *ci) |
| 1033 | { |
| 1034 | return rb_first(&ci->i_caps) == rb_last(&ci->i_caps); |
| 1035 | } |
| 1036 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1037 | static int __ceph_is_any_caps(struct ceph_inode_info *ci) |
| 1038 | { |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 1039 | return !RB_EMPTY_ROOT(&ci->i_caps); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
Yan, Zheng | 9215aee | 2013-11-30 12:47:41 +0800 | [diff] [blame] | 1042 | int ceph_is_any_caps(struct inode *inode) |
| 1043 | { |
| 1044 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1045 | int ret; |
| 1046 | |
| 1047 | spin_lock(&ci->i_ceph_lock); |
| 1048 | ret = __ceph_is_any_caps(ci); |
| 1049 | spin_unlock(&ci->i_ceph_lock); |
| 1050 | |
| 1051 | return ret; |
| 1052 | } |
| 1053 | |
Yan, Zheng | db40cc1 | 2015-03-23 20:12:20 +0800 | [diff] [blame] | 1054 | static void drop_inode_snap_realm(struct ceph_inode_info *ci) |
| 1055 | { |
| 1056 | struct ceph_snap_realm *realm = ci->i_snap_realm; |
| 1057 | spin_lock(&realm->inodes_with_caps_lock); |
| 1058 | list_del_init(&ci->i_snap_realm_item); |
| 1059 | ci->i_snap_realm_counter++; |
| 1060 | ci->i_snap_realm = NULL; |
Yan, Zheng | d95e674 | 2019-01-10 15:41:09 +0800 | [diff] [blame] | 1061 | if (realm->ino == ci->i_vino.ino) |
| 1062 | realm->inode = NULL; |
Yan, Zheng | db40cc1 | 2015-03-23 20:12:20 +0800 | [diff] [blame] | 1063 | spin_unlock(&realm->inodes_with_caps_lock); |
| 1064 | ceph_put_snap_realm(ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc, |
| 1065 | realm); |
| 1066 | } |
| 1067 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1068 | /* |
Sage Weil | f818a73 | 2010-05-11 20:56:31 -0700 | [diff] [blame] | 1069 | * Remove a cap. Take steps to deal with a racing iterate_session_caps. |
| 1070 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1071 | * caller should hold i_ceph_lock. |
Sage Weil | a636974 | 2010-02-22 13:59:00 -0800 | [diff] [blame] | 1072 | * caller will not hold session s_mutex if called from destroy_inode. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1073 | */ |
Yan, Zheng | a096b09 | 2013-09-22 10:15:58 +0800 | [diff] [blame] | 1074 | void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1075 | { |
| 1076 | struct ceph_mds_session *session = cap->session; |
| 1077 | struct ceph_inode_info *ci = cap->ci; |
Cheng Renquan | 640ef79 | 2010-03-26 17:40:33 +0800 | [diff] [blame] | 1078 | struct ceph_mds_client *mdsc = |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1079 | ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc; |
Sage Weil | f818a73 | 2010-05-11 20:56:31 -0700 | [diff] [blame] | 1080 | int removed = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1081 | |
| 1082 | dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode); |
| 1083 | |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1084 | /* remove from session list */ |
| 1085 | spin_lock(&session->s_cap_lock); |
| 1086 | if (session->s_cap_iterator == cap) { |
| 1087 | /* not yet, we are iterating over this very cap */ |
| 1088 | dout("__ceph_remove_cap delaying %p removal from session %p\n", |
| 1089 | cap, cap->session); |
| 1090 | } else { |
| 1091 | list_del_init(&cap->session_caps); |
| 1092 | session->s_nr_caps--; |
| 1093 | cap->session = NULL; |
Sage Weil | f818a73 | 2010-05-11 20:56:31 -0700 | [diff] [blame] | 1094 | removed = 1; |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1095 | } |
Sage Weil | f818a73 | 2010-05-11 20:56:31 -0700 | [diff] [blame] | 1096 | /* protect backpointer with s_cap_lock: see iterate_session_caps */ |
| 1097 | cap->ci = NULL; |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1098 | |
| 1099 | /* |
| 1100 | * s_cap_reconnect is protected by s_cap_lock. no one changes |
| 1101 | * s_cap_gen while session is in the reconnect state. |
| 1102 | */ |
| 1103 | if (queue_release && |
| 1104 | (!session->s_cap_reconnect || cap->cap_gen == session->s_cap_gen)) { |
| 1105 | cap->queue_release = 1; |
| 1106 | if (removed) { |
Yan, Zheng | e3ec8d6 | 2019-01-14 17:21:19 +0800 | [diff] [blame] | 1107 | __ceph_queue_cap_release(session, cap); |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 1108 | removed = 0; |
| 1109 | } |
| 1110 | } else { |
| 1111 | cap->queue_release = 0; |
| 1112 | } |
| 1113 | cap->cap_ino = ci->i_vino.ino; |
| 1114 | |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1115 | spin_unlock(&session->s_cap_lock); |
| 1116 | |
Sage Weil | f818a73 | 2010-05-11 20:56:31 -0700 | [diff] [blame] | 1117 | /* remove from inode list */ |
| 1118 | rb_erase(&cap->ci_node, &ci->i_caps); |
| 1119 | if (ci->i_auth_cap == cap) |
| 1120 | ci->i_auth_cap = NULL; |
| 1121 | |
| 1122 | if (removed) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 1123 | ceph_put_cap(mdsc, cap); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1124 | |
Yan, Zheng | db40cc1 | 2015-03-23 20:12:20 +0800 | [diff] [blame] | 1125 | /* when reconnect denied, we remove session caps forcibly, |
| 1126 | * i_wr_ref can be non-zero. If there are ongoing write, |
| 1127 | * keep i_snap_realm. |
| 1128 | */ |
| 1129 | if (!__ceph_is_any_caps(ci) && ci->i_wr_ref == 0 && ci->i_snap_realm) |
| 1130 | drop_inode_snap_realm(ci); |
| 1131 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1132 | if (!__ceph_is_any_real_caps(ci)) |
| 1133 | __cap_delay_cancel(mdsc, ci); |
| 1134 | } |
| 1135 | |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1136 | struct cap_msg_args { |
| 1137 | struct ceph_mds_session *session; |
| 1138 | u64 ino, cid, follows; |
| 1139 | u64 flush_tid, oldest_flush_tid, size, max_size; |
| 1140 | u64 xattr_version; |
| 1141 | struct ceph_buffer *xattr_buf; |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 1142 | struct timespec64 atime, mtime, ctime; |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1143 | int op, caps, wanted, dirty; |
| 1144 | u32 seq, issue_seq, mseq, time_warp_seq; |
Jeff Layton | 1e4ef0c | 2016-11-10 07:42:06 -0500 | [diff] [blame] | 1145 | u32 flags; |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1146 | kuid_t uid; |
| 1147 | kgid_t gid; |
| 1148 | umode_t mode; |
| 1149 | bool inline_data; |
| 1150 | }; |
| 1151 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1152 | /* |
| 1153 | * Build and send a cap message to the given MDS. |
| 1154 | * |
| 1155 | * Caller should be holding s_mutex. |
| 1156 | */ |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1157 | static int send_cap_msg(struct cap_msg_args *arg) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1158 | { |
| 1159 | struct ceph_mds_caps *fc; |
| 1160 | struct ceph_msg *msg; |
Yan, Zheng | e20d258 | 2014-11-14 22:39:13 +0800 | [diff] [blame] | 1161 | void *p; |
| 1162 | size_t extra_len; |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 1163 | struct timespec64 zerotime = {0}; |
Jeff Layton | 92475f0 | 2017-04-13 11:07:04 -0400 | [diff] [blame] | 1164 | struct ceph_osd_client *osdc = &arg->session->s_mdsc->fsc->client->osdc; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1165 | |
| 1166 | dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s" |
Yan, Zheng | a2971c8 | 2015-06-10 11:09:32 +0800 | [diff] [blame] | 1167 | " seq %u/%u tid %llu/%llu mseq %u follows %lld size %llu/%llu" |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1168 | " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(arg->op), |
| 1169 | arg->cid, arg->ino, ceph_cap_string(arg->caps), |
| 1170 | ceph_cap_string(arg->wanted), ceph_cap_string(arg->dirty), |
| 1171 | arg->seq, arg->issue_seq, arg->flush_tid, arg->oldest_flush_tid, |
| 1172 | arg->mseq, arg->follows, arg->size, arg->max_size, |
| 1173 | arg->xattr_version, |
| 1174 | arg->xattr_buf ? (int)arg->xattr_buf->vec.iov_len : 0); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1175 | |
Yan, Zheng | a2971c8 | 2015-06-10 11:09:32 +0800 | [diff] [blame] | 1176 | /* flock buffer size + inline version + inline data size + |
| 1177 | * osd_epoch_barrier + oldest_flush_tid */ |
Jeff Layton | 43b2967 | 2016-11-10 07:42:05 -0500 | [diff] [blame] | 1178 | extra_len = 4 + 8 + 4 + 4 + 8 + 4 + 4 + 4 + 8 + 8 + 4; |
Yan, Zheng | e20d258 | 2014-11-14 22:39:13 +0800 | [diff] [blame] | 1179 | msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc) + extra_len, |
| 1180 | GFP_NOFS, false); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 1181 | if (!msg) |
| 1182 | return -ENOMEM; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1183 | |
Jeff Layton | 43b2967 | 2016-11-10 07:42:05 -0500 | [diff] [blame] | 1184 | msg->hdr.version = cpu_to_le16(10); |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1185 | msg->hdr.tid = cpu_to_le64(arg->flush_tid); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1186 | |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 1187 | fc = msg->front.iov_base; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1188 | memset(fc, 0, sizeof(*fc)); |
| 1189 | |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1190 | fc->cap_id = cpu_to_le64(arg->cid); |
| 1191 | fc->op = cpu_to_le32(arg->op); |
| 1192 | fc->seq = cpu_to_le32(arg->seq); |
| 1193 | fc->issue_seq = cpu_to_le32(arg->issue_seq); |
| 1194 | fc->migrate_seq = cpu_to_le32(arg->mseq); |
| 1195 | fc->caps = cpu_to_le32(arg->caps); |
| 1196 | fc->wanted = cpu_to_le32(arg->wanted); |
| 1197 | fc->dirty = cpu_to_le32(arg->dirty); |
| 1198 | fc->ino = cpu_to_le64(arg->ino); |
| 1199 | fc->snap_follows = cpu_to_le64(arg->follows); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1200 | |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1201 | fc->size = cpu_to_le64(arg->size); |
| 1202 | fc->max_size = cpu_to_le64(arg->max_size); |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 1203 | ceph_encode_timespec64(&fc->mtime, &arg->mtime); |
| 1204 | ceph_encode_timespec64(&fc->atime, &arg->atime); |
| 1205 | ceph_encode_timespec64(&fc->ctime, &arg->ctime); |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1206 | fc->time_warp_seq = cpu_to_le32(arg->time_warp_seq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1207 | |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1208 | fc->uid = cpu_to_le32(from_kuid(&init_user_ns, arg->uid)); |
| 1209 | fc->gid = cpu_to_le32(from_kgid(&init_user_ns, arg->gid)); |
| 1210 | fc->mode = cpu_to_le32(arg->mode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1211 | |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1212 | fc->xattr_version = cpu_to_le64(arg->xattr_version); |
| 1213 | if (arg->xattr_buf) { |
| 1214 | msg->middle = ceph_buffer_get(arg->xattr_buf); |
| 1215 | fc->xattr_len = cpu_to_le32(arg->xattr_buf->vec.iov_len); |
| 1216 | msg->hdr.middle_len = cpu_to_le32(arg->xattr_buf->vec.iov_len); |
Jeff Layton | 9670079 | 2016-11-10 07:42:02 -0500 | [diff] [blame] | 1217 | } |
| 1218 | |
Yan, Zheng | e20d258 | 2014-11-14 22:39:13 +0800 | [diff] [blame] | 1219 | p = fc + 1; |
Jeff Layton | 43b2967 | 2016-11-10 07:42:05 -0500 | [diff] [blame] | 1220 | /* flock buffer size (version 2) */ |
Yan, Zheng | e20d258 | 2014-11-14 22:39:13 +0800 | [diff] [blame] | 1221 | ceph_encode_32(&p, 0); |
Jeff Layton | 43b2967 | 2016-11-10 07:42:05 -0500 | [diff] [blame] | 1222 | /* inline version (version 4) */ |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1223 | ceph_encode_64(&p, arg->inline_data ? 0 : CEPH_INLINE_NONE); |
Yan, Zheng | e20d258 | 2014-11-14 22:39:13 +0800 | [diff] [blame] | 1224 | /* inline data size */ |
| 1225 | ceph_encode_32(&p, 0); |
Jeff Layton | 92475f0 | 2017-04-13 11:07:04 -0400 | [diff] [blame] | 1226 | /* |
| 1227 | * osd_epoch_barrier (version 5) |
| 1228 | * The epoch_barrier is protected osdc->lock, so READ_ONCE here in |
| 1229 | * case it was recently changed |
| 1230 | */ |
| 1231 | ceph_encode_32(&p, READ_ONCE(osdc->epoch_barrier)); |
Jeff Layton | 43b2967 | 2016-11-10 07:42:05 -0500 | [diff] [blame] | 1232 | /* oldest_flush_tid (version 6) */ |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1233 | ceph_encode_64(&p, arg->oldest_flush_tid); |
Yan, Zheng | e20d258 | 2014-11-14 22:39:13 +0800 | [diff] [blame] | 1234 | |
Jeff Layton | 43b2967 | 2016-11-10 07:42:05 -0500 | [diff] [blame] | 1235 | /* |
| 1236 | * caller_uid/caller_gid (version 7) |
| 1237 | * |
| 1238 | * Currently, we don't properly track which caller dirtied the caps |
| 1239 | * last, and force a flush of them when there is a conflict. For now, |
| 1240 | * just set this to 0:0, to emulate how the MDS has worked up to now. |
| 1241 | */ |
| 1242 | ceph_encode_32(&p, 0); |
| 1243 | ceph_encode_32(&p, 0); |
| 1244 | |
| 1245 | /* pool namespace (version 8) (mds always ignores this) */ |
| 1246 | ceph_encode_32(&p, 0); |
| 1247 | |
| 1248 | /* |
| 1249 | * btime and change_attr (version 9) |
| 1250 | * |
| 1251 | * We just zero these out for now, as the MDS ignores them unless |
| 1252 | * the requisite feature flags are set (which we don't do yet). |
| 1253 | */ |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 1254 | ceph_encode_timespec64(p, &zerotime); |
Jeff Layton | 43b2967 | 2016-11-10 07:42:05 -0500 | [diff] [blame] | 1255 | p += sizeof(struct ceph_timespec); |
| 1256 | ceph_encode_64(&p, 0); |
| 1257 | |
| 1258 | /* Advisory flags (version 10) */ |
Jeff Layton | 1e4ef0c | 2016-11-10 07:42:06 -0500 | [diff] [blame] | 1259 | ceph_encode_32(&p, arg->flags); |
Jeff Layton | 43b2967 | 2016-11-10 07:42:05 -0500 | [diff] [blame] | 1260 | |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1261 | ceph_con_send(&arg->session->s_con, msg); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1262 | return 0; |
| 1263 | } |
| 1264 | |
| 1265 | /* |
Sage Weil | a636974 | 2010-02-22 13:59:00 -0800 | [diff] [blame] | 1266 | * Queue cap releases when an inode is dropped from our cache. Since |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1267 | * inode is about to be destroyed, there is no need for i_ceph_lock. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1268 | */ |
Yan, Zheng | e3ec8d6 | 2019-01-14 17:21:19 +0800 | [diff] [blame] | 1269 | void __ceph_remove_caps(struct inode *inode) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1270 | { |
| 1271 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1272 | struct rb_node *p; |
| 1273 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1274 | p = rb_first(&ci->i_caps); |
| 1275 | while (p) { |
| 1276 | struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1277 | p = rb_next(p); |
Yan, Zheng | a096b09 | 2013-09-22 10:15:58 +0800 | [diff] [blame] | 1278 | __ceph_remove_cap(cap, true); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1279 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | /* |
| 1283 | * Send a cap msg on the given inode. Update our caps state, then |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1284 | * drop i_ceph_lock and send the message. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1285 | * |
| 1286 | * Make note of max_size reported/requested from mds, revoked caps |
| 1287 | * that have now been implemented. |
| 1288 | * |
| 1289 | * Make half-hearted attempt ot to invalidate page cache if we are |
| 1290 | * dropping RDCACHE. Note that this will leave behind locked pages |
| 1291 | * that we'll then need to deal with elsewhere. |
| 1292 | * |
| 1293 | * Return non-zero if delayed release, or we experienced an error |
| 1294 | * such that the caller should requeue + retry later. |
| 1295 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1296 | * called with i_ceph_lock, then drops it. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1297 | * caller should hold snap_rwsem (read), s_mutex. |
| 1298 | */ |
| 1299 | static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap, |
Jeff Layton | 1e4ef0c | 2016-11-10 07:42:06 -0500 | [diff] [blame] | 1300 | int op, bool sync, int used, int want, int retain, |
| 1301 | int flushing, u64 flush_tid, u64 oldest_flush_tid) |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1302 | __releases(cap->ci->i_ceph_lock) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1303 | { |
| 1304 | struct ceph_inode_info *ci = cap->ci; |
| 1305 | struct inode *inode = &ci->vfs_inode; |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1306 | struct cap_msg_args arg; |
Colin Ian King | bb0581f | 2017-10-18 12:34:25 +0100 | [diff] [blame] | 1307 | int held, revoking; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1308 | int wake = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1309 | int delayed = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1310 | int ret; |
| 1311 | |
Sage Weil | 68c2832 | 2010-02-09 13:41:47 -0800 | [diff] [blame] | 1312 | held = cap->issued | cap->implemented; |
| 1313 | revoking = cap->implemented & ~cap->issued; |
| 1314 | retain &= ~revoking; |
Sage Weil | 68c2832 | 2010-02-09 13:41:47 -0800 | [diff] [blame] | 1315 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1316 | dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n", |
| 1317 | inode, cap, cap->session, |
| 1318 | ceph_cap_string(held), ceph_cap_string(held & retain), |
| 1319 | ceph_cap_string(revoking)); |
| 1320 | BUG_ON((retain & CEPH_CAP_PIN) == 0); |
| 1321 | |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1322 | arg.session = cap->session; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1323 | |
| 1324 | /* don't release wanted unless we've waited a bit. */ |
| 1325 | if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 && |
| 1326 | time_before(jiffies, ci->i_hold_caps_min)) { |
| 1327 | dout(" delaying issued %s -> %s, wanted %s -> %s on send\n", |
| 1328 | ceph_cap_string(cap->issued), |
| 1329 | ceph_cap_string(cap->issued & retain), |
| 1330 | ceph_cap_string(cap->mds_wanted), |
| 1331 | ceph_cap_string(want)); |
| 1332 | want |= cap->mds_wanted; |
| 1333 | retain |= cap->issued; |
| 1334 | delayed = 1; |
| 1335 | } |
| 1336 | ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH); |
Yan, Zheng | eb65b91 | 2017-01-12 17:18:00 +0800 | [diff] [blame] | 1337 | if (want & ~cap->mds_wanted) { |
| 1338 | /* user space may open/close single file frequently. |
| 1339 | * This avoids droping mds_wanted immediately after |
| 1340 | * requesting new mds_wanted. |
| 1341 | */ |
| 1342 | __cap_set_timeouts(mdsc, ci); |
| 1343 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1344 | |
| 1345 | cap->issued &= retain; /* drop bits we don't want */ |
| 1346 | if (cap->implemented & ~cap->issued) { |
| 1347 | /* |
| 1348 | * Wake up any waiters on wanted -> needed transition. |
| 1349 | * This is due to the weird transition from buffered |
| 1350 | * to sync IO... we need to flush dirty pages _before_ |
| 1351 | * allowing sync writes to avoid reordering. |
| 1352 | */ |
| 1353 | wake = 1; |
| 1354 | } |
| 1355 | cap->implemented &= cap->issued | used; |
| 1356 | cap->mds_wanted = want; |
| 1357 | |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1358 | arg.ino = ceph_vino(inode).ino; |
| 1359 | arg.cid = cap->cap_id; |
| 1360 | arg.follows = flushing ? ci->i_head_snapc->seq : 0; |
| 1361 | arg.flush_tid = flush_tid; |
| 1362 | arg.oldest_flush_tid = oldest_flush_tid; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1363 | |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1364 | arg.size = inode->i_size; |
| 1365 | ci->i_reported_size = arg.size; |
| 1366 | arg.max_size = ci->i_wanted_max_size; |
| 1367 | ci->i_requested_max_size = arg.max_size; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1368 | |
Sage Weil | 082afec | 2010-08-22 15:16:41 -0700 | [diff] [blame] | 1369 | if (flushing & CEPH_CAP_XATTR_EXCL) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1370 | __ceph_build_xattrs_blob(ci); |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1371 | arg.xattr_version = ci->i_xattrs.version; |
| 1372 | arg.xattr_buf = ci->i_xattrs.blob; |
| 1373 | } else { |
| 1374 | arg.xattr_buf = NULL; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1375 | } |
| 1376 | |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 1377 | arg.mtime = inode->i_mtime; |
| 1378 | arg.atime = inode->i_atime; |
| 1379 | arg.ctime = inode->i_ctime; |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1380 | |
| 1381 | arg.op = op; |
| 1382 | arg.caps = cap->implemented; |
| 1383 | arg.wanted = want; |
| 1384 | arg.dirty = flushing; |
| 1385 | |
| 1386 | arg.seq = cap->seq; |
| 1387 | arg.issue_seq = cap->issue_seq; |
| 1388 | arg.mseq = cap->mseq; |
| 1389 | arg.time_warp_seq = ci->i_time_warp_seq; |
| 1390 | |
| 1391 | arg.uid = inode->i_uid; |
| 1392 | arg.gid = inode->i_gid; |
| 1393 | arg.mode = inode->i_mode; |
| 1394 | |
| 1395 | arg.inline_data = ci->i_inline_version != CEPH_INLINE_NONE; |
Yan, Zheng | 9556971 | 2017-07-24 17:59:39 +0800 | [diff] [blame] | 1396 | if (list_empty(&ci->i_cap_snaps)) |
| 1397 | arg.flags = CEPH_CLIENT_CAPS_NO_CAPSNAP; |
| 1398 | else |
| 1399 | arg.flags = CEPH_CLIENT_CAPS_PENDING_CAPSNAP; |
Jeff Layton | 1e4ef0c | 2016-11-10 07:42:06 -0500 | [diff] [blame] | 1400 | if (sync) |
| 1401 | arg.flags |= CEPH_CLIENT_CAPS_SYNC; |
Yan, Zheng | e20d258 | 2014-11-14 22:39:13 +0800 | [diff] [blame] | 1402 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1403 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1404 | |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1405 | ret = send_cap_msg(&arg); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1406 | if (ret < 0) { |
| 1407 | dout("error sending cap msg, must requeue %p\n", inode); |
| 1408 | delayed = 1; |
| 1409 | } |
| 1410 | |
| 1411 | if (wake) |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 1412 | wake_up_all(&ci->i_cap_wq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1413 | |
| 1414 | return delayed; |
| 1415 | } |
| 1416 | |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 1417 | static inline int __send_flush_snap(struct inode *inode, |
| 1418 | struct ceph_mds_session *session, |
| 1419 | struct ceph_cap_snap *capsnap, |
| 1420 | u32 mseq, u64 oldest_flush_tid) |
| 1421 | { |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1422 | struct cap_msg_args arg; |
| 1423 | |
| 1424 | arg.session = session; |
| 1425 | arg.ino = ceph_vino(inode).ino; |
| 1426 | arg.cid = 0; |
| 1427 | arg.follows = capsnap->follows; |
| 1428 | arg.flush_tid = capsnap->cap_flush.tid; |
| 1429 | arg.oldest_flush_tid = oldest_flush_tid; |
| 1430 | |
| 1431 | arg.size = capsnap->size; |
| 1432 | arg.max_size = 0; |
| 1433 | arg.xattr_version = capsnap->xattr_version; |
| 1434 | arg.xattr_buf = capsnap->xattr_blob; |
| 1435 | |
| 1436 | arg.atime = capsnap->atime; |
| 1437 | arg.mtime = capsnap->mtime; |
| 1438 | arg.ctime = capsnap->ctime; |
| 1439 | |
| 1440 | arg.op = CEPH_CAP_OP_FLUSHSNAP; |
| 1441 | arg.caps = capsnap->issued; |
| 1442 | arg.wanted = 0; |
| 1443 | arg.dirty = capsnap->dirty; |
| 1444 | |
| 1445 | arg.seq = 0; |
| 1446 | arg.issue_seq = 0; |
| 1447 | arg.mseq = mseq; |
| 1448 | arg.time_warp_seq = capsnap->time_warp_seq; |
| 1449 | |
| 1450 | arg.uid = capsnap->uid; |
| 1451 | arg.gid = capsnap->gid; |
| 1452 | arg.mode = capsnap->mode; |
| 1453 | |
| 1454 | arg.inline_data = capsnap->inline_data; |
Jeff Layton | 1e4ef0c | 2016-11-10 07:42:06 -0500 | [diff] [blame] | 1455 | arg.flags = 0; |
Jeff Layton | 0ff8bfb | 2016-11-10 07:42:03 -0500 | [diff] [blame] | 1456 | |
| 1457 | return send_cap_msg(&arg); |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 1458 | } |
| 1459 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1460 | /* |
| 1461 | * When a snapshot is taken, clients accumulate dirty metadata on |
| 1462 | * inodes with capabilities in ceph_cap_snaps to describe the file |
| 1463 | * state at the time the snapshot was taken. This must be flushed |
| 1464 | * asynchronously back to the MDS once sync writes complete and dirty |
| 1465 | * data is written out. |
| 1466 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1467 | * Called under i_ceph_lock. Takes s_mutex as needed. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1468 | */ |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1469 | static void __ceph_flush_snaps(struct ceph_inode_info *ci, |
| 1470 | struct ceph_mds_session *session) |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1471 | __releases(ci->i_ceph_lock) |
| 1472 | __acquires(ci->i_ceph_lock) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1473 | { |
| 1474 | struct inode *inode = &ci->vfs_inode; |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1475 | struct ceph_mds_client *mdsc = session->s_mdsc; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1476 | struct ceph_cap_snap *capsnap; |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1477 | u64 oldest_flush_tid = 0; |
| 1478 | u64 first_tid = 1, last_tid = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1479 | |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1480 | dout("__flush_snaps %p session %p\n", inode, session); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1481 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1482 | list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1483 | /* |
| 1484 | * we need to wait for sync writes to complete and for dirty |
| 1485 | * pages to be written out. |
| 1486 | */ |
| 1487 | if (capsnap->dirty_pages || capsnap->writing) |
Sage Weil | cfc0bf6 | 2010-09-14 15:50:59 -0700 | [diff] [blame] | 1488 | break; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1489 | |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 1490 | /* should be removed by ceph_try_drop_cap_snap() */ |
| 1491 | BUG_ON(!capsnap->need_flush); |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 1492 | |
Sage Weil | e835124 | 2010-09-17 08:03:08 -0700 | [diff] [blame] | 1493 | /* only flush each capsnap once */ |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 1494 | if (capsnap->cap_flush.tid > 0) { |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1495 | dout(" already flushed %p, skipping\n", capsnap); |
Sage Weil | e835124 | 2010-09-17 08:03:08 -0700 | [diff] [blame] | 1496 | continue; |
| 1497 | } |
| 1498 | |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 1499 | spin_lock(&mdsc->cap_dirty_lock); |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 1500 | capsnap->cap_flush.tid = ++mdsc->last_cap_flush_tid; |
| 1501 | list_add_tail(&capsnap->cap_flush.g_list, |
| 1502 | &mdsc->cap_flush_list); |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1503 | if (oldest_flush_tid == 0) |
| 1504 | oldest_flush_tid = __get_oldest_flush_tid(mdsc); |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 1505 | if (list_empty(&ci->i_flushing_item)) { |
| 1506 | list_add_tail(&ci->i_flushing_item, |
| 1507 | &session->s_cap_flushing); |
| 1508 | } |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 1509 | spin_unlock(&mdsc->cap_dirty_lock); |
| 1510 | |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 1511 | list_add_tail(&capsnap->cap_flush.i_list, |
| 1512 | &ci->i_cap_flush_list); |
| 1513 | |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1514 | if (first_tid == 1) |
| 1515 | first_tid = capsnap->cap_flush.tid; |
| 1516 | last_tid = capsnap->cap_flush.tid; |
| 1517 | } |
| 1518 | |
| 1519 | ci->i_ceph_flags &= ~CEPH_I_FLUSH_SNAPS; |
| 1520 | |
| 1521 | while (first_tid <= last_tid) { |
| 1522 | struct ceph_cap *cap = ci->i_auth_cap; |
| 1523 | struct ceph_cap_flush *cf; |
| 1524 | int ret; |
| 1525 | |
| 1526 | if (!(cap && cap->session == session)) { |
| 1527 | dout("__flush_snaps %p auth cap %p not mds%d, " |
| 1528 | "stop\n", inode, cap, session->s_mds); |
| 1529 | break; |
| 1530 | } |
| 1531 | |
| 1532 | ret = -ENOENT; |
| 1533 | list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) { |
| 1534 | if (cf->tid >= first_tid) { |
| 1535 | ret = 0; |
| 1536 | break; |
| 1537 | } |
| 1538 | } |
| 1539 | if (ret < 0) |
| 1540 | break; |
| 1541 | |
| 1542 | first_tid = cf->tid + 1; |
| 1543 | |
| 1544 | capsnap = container_of(cf, struct ceph_cap_snap, cap_flush); |
Elena Reshetova | 805692d | 2017-03-03 11:15:07 +0200 | [diff] [blame] | 1545 | refcount_inc(&capsnap->nref); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1546 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1547 | |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1548 | dout("__flush_snaps %p capsnap %p tid %llu %s\n", |
| 1549 | inode, capsnap, cf->tid, ceph_cap_string(capsnap->dirty)); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1550 | |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1551 | ret = __send_flush_snap(inode, session, capsnap, cap->mseq, |
| 1552 | oldest_flush_tid); |
| 1553 | if (ret < 0) { |
| 1554 | pr_err("__flush_snaps: error sending cap flushsnap, " |
| 1555 | "ino (%llx.%llx) tid %llu follows %llu\n", |
| 1556 | ceph_vinop(inode), cf->tid, capsnap->follows); |
| 1557 | } |
| 1558 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1559 | ceph_put_cap_snap(capsnap); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1560 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | void ceph_flush_snaps(struct ceph_inode_info *ci, |
| 1565 | struct ceph_mds_session **psession) |
| 1566 | { |
| 1567 | struct inode *inode = &ci->vfs_inode; |
| 1568 | struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc; |
Yan, Zheng | e4d2b16 | 2016-08-04 08:43:33 +0800 | [diff] [blame] | 1569 | struct ceph_mds_session *session = NULL; |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1570 | int mds; |
Yan, Zheng | e4d2b16 | 2016-08-04 08:43:33 +0800 | [diff] [blame] | 1571 | |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1572 | dout("ceph_flush_snaps %p\n", inode); |
Yan, Zheng | e4d2b16 | 2016-08-04 08:43:33 +0800 | [diff] [blame] | 1573 | if (psession) |
| 1574 | session = *psession; |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1575 | retry: |
| 1576 | spin_lock(&ci->i_ceph_lock); |
| 1577 | if (!(ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS)) { |
| 1578 | dout(" no capsnap needs flush, doing nothing\n"); |
| 1579 | goto out; |
| 1580 | } |
| 1581 | if (!ci->i_auth_cap) { |
| 1582 | dout(" no auth cap (migrating?), doing nothing\n"); |
| 1583 | goto out; |
| 1584 | } |
| 1585 | |
| 1586 | mds = ci->i_auth_cap->session->s_mds; |
| 1587 | if (session && session->s_mds != mds) { |
| 1588 | dout(" oops, wrong session %p mutex\n", session); |
| 1589 | mutex_unlock(&session->s_mutex); |
| 1590 | ceph_put_mds_session(session); |
| 1591 | session = NULL; |
| 1592 | } |
| 1593 | if (!session) { |
| 1594 | spin_unlock(&ci->i_ceph_lock); |
| 1595 | mutex_lock(&mdsc->mutex); |
| 1596 | session = __ceph_lookup_mds_session(mdsc, mds); |
| 1597 | mutex_unlock(&mdsc->mutex); |
| 1598 | if (session) { |
| 1599 | dout(" inverting session/ino locks on %p\n", session); |
| 1600 | mutex_lock(&session->s_mutex); |
| 1601 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1602 | goto retry; |
| 1603 | } |
| 1604 | |
Yan, Zheng | 24d063a | 2017-08-15 11:37:32 +0800 | [diff] [blame] | 1605 | // make sure flushsnap messages are sent in proper order. |
| 1606 | if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) { |
| 1607 | __kick_flushing_caps(mdsc, session, ci, 0); |
| 1608 | ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH; |
| 1609 | } |
| 1610 | |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1611 | __ceph_flush_snaps(ci, session); |
| 1612 | out: |
| 1613 | spin_unlock(&ci->i_ceph_lock); |
| 1614 | |
| 1615 | if (psession) { |
| 1616 | *psession = session; |
Yan, Zheng | c858a07 | 2017-08-28 15:02:42 +0800 | [diff] [blame] | 1617 | } else if (session) { |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 1618 | mutex_unlock(&session->s_mutex); |
| 1619 | ceph_put_mds_session(session); |
| 1620 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1621 | /* we flushed them all; remove this inode from the queue */ |
| 1622 | spin_lock(&mdsc->snap_flush_lock); |
| 1623 | list_del_init(&ci->i_snap_flush_item); |
| 1624 | spin_unlock(&mdsc->snap_flush_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | /* |
Sage Weil | fca65b4 | 2011-05-04 11:33:47 -0700 | [diff] [blame] | 1628 | * Mark caps dirty. If inode is newly dirty, return the dirty flags. |
| 1629 | * Caller is then responsible for calling __mark_inode_dirty with the |
| 1630 | * returned flags value. |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1631 | */ |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1632 | int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask, |
| 1633 | struct ceph_cap_flush **pcf) |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1634 | { |
Cheng Renquan | 640ef79 | 2010-03-26 17:40:33 +0800 | [diff] [blame] | 1635 | struct ceph_mds_client *mdsc = |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1636 | ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc; |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1637 | struct inode *inode = &ci->vfs_inode; |
| 1638 | int was = ci->i_dirty_caps; |
| 1639 | int dirty = 0; |
| 1640 | |
Yan, Zheng | 571ade3 | 2015-03-24 11:36:08 +0800 | [diff] [blame] | 1641 | if (!ci->i_auth_cap) { |
| 1642 | pr_warn("__mark_dirty_caps %p %llx mask %s, " |
| 1643 | "but no auth cap (session was closed?)\n", |
| 1644 | inode, ceph_ino(inode), ceph_cap_string(mask)); |
| 1645 | return 0; |
| 1646 | } |
| 1647 | |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1648 | dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode, |
| 1649 | ceph_cap_string(mask), ceph_cap_string(was), |
| 1650 | ceph_cap_string(was | mask)); |
| 1651 | ci->i_dirty_caps |= mask; |
| 1652 | if (was == 0) { |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1653 | WARN_ON_ONCE(ci->i_prealloc_cap_flush); |
| 1654 | swap(ci->i_prealloc_cap_flush, *pcf); |
| 1655 | |
Yan, Zheng | 604d1b0 | 2015-05-01 17:49:16 +0800 | [diff] [blame] | 1656 | if (!ci->i_head_snapc) { |
| 1657 | WARN_ON_ONCE(!rwsem_is_locked(&mdsc->snap_rwsem)); |
Sage Weil | 7d8cb26 | 2010-08-24 08:44:16 -0700 | [diff] [blame] | 1658 | ci->i_head_snapc = ceph_get_snap_context( |
| 1659 | ci->i_snap_realm->cached_context); |
Yan, Zheng | 604d1b0 | 2015-05-01 17:49:16 +0800 | [diff] [blame] | 1660 | } |
Yan, Zheng | 0685235 | 2012-11-19 10:49:07 +0800 | [diff] [blame] | 1661 | dout(" inode %p now dirty snapc %p auth cap %p\n", |
| 1662 | &ci->vfs_inode, ci->i_head_snapc, ci->i_auth_cap); |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1663 | BUG_ON(!list_empty(&ci->i_dirty_item)); |
| 1664 | spin_lock(&mdsc->cap_dirty_lock); |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 1665 | list_add(&ci->i_dirty_item, &mdsc->cap_dirty); |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1666 | spin_unlock(&mdsc->cap_dirty_lock); |
| 1667 | if (ci->i_flushing_caps == 0) { |
Sage Weil | 3772d26 | 2011-05-03 09:28:08 -0700 | [diff] [blame] | 1668 | ihold(inode); |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1669 | dirty |= I_DIRTY_SYNC; |
| 1670 | } |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1671 | } else { |
| 1672 | WARN_ON_ONCE(!ci->i_prealloc_cap_flush); |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1673 | } |
| 1674 | BUG_ON(list_empty(&ci->i_dirty_item)); |
| 1675 | if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) && |
| 1676 | (mask & CEPH_CAP_FILE_BUFFER)) |
| 1677 | dirty |= I_DIRTY_DATASYNC; |
Xuehan Xu | 6680288 | 2018-10-11 17:55:39 +0800 | [diff] [blame] | 1678 | __cap_delay_requeue(mdsc, ci, true); |
Sage Weil | fca65b4 | 2011-05-04 11:33:47 -0700 | [diff] [blame] | 1679 | return dirty; |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1680 | } |
| 1681 | |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1682 | struct ceph_cap_flush *ceph_alloc_cap_flush(void) |
| 1683 | { |
| 1684 | return kmem_cache_alloc(ceph_cap_flush_cachep, GFP_KERNEL); |
| 1685 | } |
| 1686 | |
| 1687 | void ceph_free_cap_flush(struct ceph_cap_flush *cf) |
| 1688 | { |
| 1689 | if (cf) |
| 1690 | kmem_cache_free(ceph_cap_flush_cachep, cf); |
| 1691 | } |
| 1692 | |
Yan, Zheng | a2971c8 | 2015-06-10 11:09:32 +0800 | [diff] [blame] | 1693 | static u64 __get_oldest_flush_tid(struct ceph_mds_client *mdsc) |
| 1694 | { |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 1695 | if (!list_empty(&mdsc->cap_flush_list)) { |
Yan, Zheng | a2971c8 | 2015-06-10 11:09:32 +0800 | [diff] [blame] | 1696 | struct ceph_cap_flush *cf = |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 1697 | list_first_entry(&mdsc->cap_flush_list, |
| 1698 | struct ceph_cap_flush, g_list); |
Yan, Zheng | a2971c8 | 2015-06-10 11:09:32 +0800 | [diff] [blame] | 1699 | return cf->tid; |
| 1700 | } |
| 1701 | return 0; |
| 1702 | } |
| 1703 | |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1704 | /* |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 1705 | * Remove cap_flush from the mdsc's or inode's flushing cap list. |
| 1706 | * Return true if caller needs to wake up flush waiters. |
| 1707 | */ |
| 1708 | static bool __finish_cap_flush(struct ceph_mds_client *mdsc, |
| 1709 | struct ceph_inode_info *ci, |
| 1710 | struct ceph_cap_flush *cf) |
| 1711 | { |
| 1712 | struct ceph_cap_flush *prev; |
| 1713 | bool wake = cf->wake; |
| 1714 | if (mdsc) { |
| 1715 | /* are there older pending cap flushes? */ |
| 1716 | if (wake && cf->g_list.prev != &mdsc->cap_flush_list) { |
| 1717 | prev = list_prev_entry(cf, g_list); |
| 1718 | prev->wake = true; |
| 1719 | wake = false; |
| 1720 | } |
| 1721 | list_del(&cf->g_list); |
| 1722 | } else if (ci) { |
| 1723 | if (wake && cf->i_list.prev != &ci->i_cap_flush_list) { |
| 1724 | prev = list_prev_entry(cf, i_list); |
| 1725 | prev->wake = true; |
| 1726 | wake = false; |
| 1727 | } |
| 1728 | list_del(&cf->i_list); |
| 1729 | } else { |
| 1730 | BUG_ON(1); |
| 1731 | } |
| 1732 | return wake; |
| 1733 | } |
| 1734 | |
| 1735 | /* |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1736 | * Add dirty inode to the flushing list. Assigned a seq number so we |
| 1737 | * can wait for caps to flush without starving. |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1738 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1739 | * Called under i_ceph_lock. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1740 | */ |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1741 | static int __mark_caps_flushing(struct inode *inode, |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 1742 | struct ceph_mds_session *session, bool wake, |
Yan, Zheng | a2971c8 | 2015-06-10 11:09:32 +0800 | [diff] [blame] | 1743 | u64 *flush_tid, u64 *oldest_flush_tid) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1744 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1745 | struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1746 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1747 | struct ceph_cap_flush *cf = NULL; |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1748 | int flushing; |
Sage Weil | 50b885b | 2009-12-01 14:12:07 -0800 | [diff] [blame] | 1749 | |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1750 | BUG_ON(ci->i_dirty_caps == 0); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1751 | BUG_ON(list_empty(&ci->i_dirty_item)); |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1752 | BUG_ON(!ci->i_prealloc_cap_flush); |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1753 | |
| 1754 | flushing = ci->i_dirty_caps; |
| 1755 | dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n", |
| 1756 | ceph_cap_string(flushing), |
| 1757 | ceph_cap_string(ci->i_flushing_caps), |
| 1758 | ceph_cap_string(ci->i_flushing_caps | flushing)); |
| 1759 | ci->i_flushing_caps |= flushing; |
| 1760 | ci->i_dirty_caps = 0; |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 1761 | dout(" inode %p now !dirty\n", inode); |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1762 | |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 1763 | swap(cf, ci->i_prealloc_cap_flush); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 1764 | cf->caps = flushing; |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 1765 | cf->wake = wake; |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 1766 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1767 | spin_lock(&mdsc->cap_dirty_lock); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 1768 | list_del_init(&ci->i_dirty_item); |
| 1769 | |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 1770 | cf->tid = ++mdsc->last_cap_flush_tid; |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 1771 | list_add_tail(&cf->g_list, &mdsc->cap_flush_list); |
Yan, Zheng | a2971c8 | 2015-06-10 11:09:32 +0800 | [diff] [blame] | 1772 | *oldest_flush_tid = __get_oldest_flush_tid(mdsc); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 1773 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1774 | if (list_empty(&ci->i_flushing_item)) { |
| 1775 | list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing); |
| 1776 | mdsc->num_cap_flushing++; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1777 | } |
| 1778 | spin_unlock(&mdsc->cap_dirty_lock); |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1779 | |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 1780 | list_add_tail(&cf->i_list, &ci->i_cap_flush_list); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 1781 | |
| 1782 | *flush_tid = cf->tid; |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1783 | return flushing; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | /* |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 1787 | * try to invalidate mapping pages without blocking. |
| 1788 | */ |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 1789 | static int try_nonblocking_invalidate(struct inode *inode) |
| 1790 | { |
| 1791 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1792 | u32 invalidating_gen = ci->i_rdcache_gen; |
| 1793 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1794 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 1795 | invalidate_mapping_pages(&inode->i_data, 0, -1); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1796 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 1797 | |
Sage Weil | 18a3819 | 2010-09-17 10:46:44 -0700 | [diff] [blame] | 1798 | if (inode->i_data.nrpages == 0 && |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 1799 | invalidating_gen == ci->i_rdcache_gen) { |
| 1800 | /* success. */ |
| 1801 | dout("try_nonblocking_invalidate %p success\n", inode); |
Sage Weil | cd045cb | 2010-11-04 11:05:05 -0700 | [diff] [blame] | 1802 | /* save any racing async invalidate some trouble */ |
| 1803 | ci->i_rdcache_revoking = ci->i_rdcache_gen - 1; |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 1804 | return 0; |
| 1805 | } |
| 1806 | dout("try_nonblocking_invalidate %p failed\n", inode); |
| 1807 | return -1; |
| 1808 | } |
| 1809 | |
Yan, Zheng | efb0ca7 | 2017-05-22 12:03:32 +0800 | [diff] [blame] | 1810 | bool __ceph_should_report_size(struct ceph_inode_info *ci) |
| 1811 | { |
| 1812 | loff_t size = ci->vfs_inode.i_size; |
| 1813 | /* mds will adjust max size according to the reported size */ |
| 1814 | if (ci->i_flushing_caps & CEPH_CAP_FILE_WR) |
| 1815 | return false; |
| 1816 | if (size >= ci->i_max_size) |
| 1817 | return true; |
| 1818 | /* half of previous max_size increment has been used */ |
| 1819 | if (ci->i_max_size > ci->i_reported_size && |
| 1820 | (size << 1) >= ci->i_max_size + ci->i_reported_size) |
| 1821 | return true; |
| 1822 | return false; |
| 1823 | } |
| 1824 | |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 1825 | /* |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1826 | * Swiss army knife function to examine currently used and wanted |
| 1827 | * versus held caps. Release, flush, ack revoked caps to mds as |
| 1828 | * appropriate. |
| 1829 | * |
| 1830 | * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay |
| 1831 | * cap release further. |
| 1832 | * CHECK_CAPS_AUTHONLY - we should only check the auth cap |
| 1833 | * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without |
| 1834 | * further delay. |
| 1835 | */ |
| 1836 | void ceph_check_caps(struct ceph_inode_info *ci, int flags, |
| 1837 | struct ceph_mds_session *session) |
| 1838 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1839 | struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode); |
| 1840 | struct ceph_mds_client *mdsc = fsc->mdsc; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1841 | struct inode *inode = &ci->vfs_inode; |
| 1842 | struct ceph_cap *cap; |
Yan, Zheng | a2971c8 | 2015-06-10 11:09:32 +0800 | [diff] [blame] | 1843 | u64 flush_tid, oldest_flush_tid; |
Yan, Zheng | 395c312 | 2013-01-04 14:37:57 +0800 | [diff] [blame] | 1844 | int file_wanted, used, cap_used; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1845 | int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */ |
Sage Weil | cbd0363 | 2010-02-09 13:41:18 -0800 | [diff] [blame] | 1846 | int issued, implemented, want, retain, revoking, flushing = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1847 | int mds = -1; /* keep track of how far we've gone through i_caps list |
| 1848 | to avoid an infinite loop on retry */ |
| 1849 | struct rb_node *p; |
Yan, Zheng | 0f439c7 | 2018-01-08 14:44:10 +0800 | [diff] [blame] | 1850 | int delayed = 0, sent = 0; |
| 1851 | bool no_delay = flags & CHECK_CAPS_NODELAY; |
Yan, Zheng | 3609404 | 2016-07-06 15:37:42 +0800 | [diff] [blame] | 1852 | bool queue_invalidate = false; |
Yan, Zheng | 3609404 | 2016-07-06 15:37:42 +0800 | [diff] [blame] | 1853 | bool tried_invalidate = false; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1854 | |
| 1855 | /* if we are unmounting, flush any unused caps immediately. */ |
| 1856 | if (mdsc->stopping) |
Yan, Zheng | 0f439c7 | 2018-01-08 14:44:10 +0800 | [diff] [blame] | 1857 | no_delay = true; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1858 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1859 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1860 | |
| 1861 | if (ci->i_ceph_flags & CEPH_I_FLUSH) |
| 1862 | flags |= CHECK_CAPS_FLUSH; |
| 1863 | |
Yan, Zheng | 0f439c7 | 2018-01-08 14:44:10 +0800 | [diff] [blame] | 1864 | if (!(flags & CHECK_CAPS_AUTHONLY) || |
| 1865 | (ci->i_auth_cap && __ceph_is_single_caps(ci))) |
| 1866 | __cap_delay_cancel(mdsc, ci); |
| 1867 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1868 | goto retry_locked; |
| 1869 | retry: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1870 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1871 | retry_locked: |
| 1872 | file_wanted = __ceph_caps_file_wanted(ci); |
| 1873 | used = __ceph_caps_used(ci); |
Sage Weil | cbd0363 | 2010-02-09 13:41:18 -0800 | [diff] [blame] | 1874 | issued = __ceph_caps_issued(ci, &implemented); |
| 1875 | revoking = implemented & ~issued; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1876 | |
Yan, Zheng | 4144599 | 2015-05-25 17:36:42 +0800 | [diff] [blame] | 1877 | want = file_wanted; |
| 1878 | retain = file_wanted | used | CEPH_CAP_PIN; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1879 | if (!mdsc->stopping && inode->i_nlink > 0) { |
Yan, Zheng | 4144599 | 2015-05-25 17:36:42 +0800 | [diff] [blame] | 1880 | if (file_wanted) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1881 | retain |= CEPH_CAP_ANY; /* be greedy */ |
Yan, Zheng | 32ec439 | 2015-03-26 19:06:00 +0800 | [diff] [blame] | 1882 | } else if (S_ISDIR(inode->i_mode) && |
| 1883 | (issued & CEPH_CAP_FILE_SHARED) && |
Yan, Zheng | 8a2ac3a | 2018-12-05 11:29:35 +0800 | [diff] [blame] | 1884 | __ceph_dir_is_complete(ci)) { |
Yan, Zheng | 32ec439 | 2015-03-26 19:06:00 +0800 | [diff] [blame] | 1885 | /* |
| 1886 | * If a directory is complete, we want to keep |
| 1887 | * the exclusive cap. So that MDS does not end up |
| 1888 | * revoking the shared cap on every create/unlink |
| 1889 | * operation. |
| 1890 | */ |
Yan, Zheng | 8a2ac3a | 2018-12-05 11:29:35 +0800 | [diff] [blame] | 1891 | if (IS_RDONLY(inode)) |
| 1892 | want = CEPH_CAP_ANY_SHARED; |
| 1893 | else |
| 1894 | want = CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_EXCL; |
Yan, Zheng | 32ec439 | 2015-03-26 19:06:00 +0800 | [diff] [blame] | 1895 | retain |= want; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1896 | } else { |
Yan, Zheng | 32ec439 | 2015-03-26 19:06:00 +0800 | [diff] [blame] | 1897 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1898 | retain |= CEPH_CAP_ANY_SHARED; |
| 1899 | /* |
| 1900 | * keep RD only if we didn't have the file open RW, |
| 1901 | * because then the mds would revoke it anyway to |
| 1902 | * journal max_size=0. |
| 1903 | */ |
| 1904 | if (ci->i_max_size == 0) |
| 1905 | retain |= CEPH_CAP_ANY_RD; |
| 1906 | } |
| 1907 | } |
| 1908 | |
| 1909 | dout("check_caps %p file_want %s used %s dirty %s flushing %s" |
Sage Weil | cbd0363 | 2010-02-09 13:41:18 -0800 | [diff] [blame] | 1910 | " issued %s revoking %s retain %s %s%s%s\n", inode, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1911 | ceph_cap_string(file_wanted), |
| 1912 | ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps), |
| 1913 | ceph_cap_string(ci->i_flushing_caps), |
Sage Weil | cbd0363 | 2010-02-09 13:41:18 -0800 | [diff] [blame] | 1914 | ceph_cap_string(issued), ceph_cap_string(revoking), |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1915 | ceph_cap_string(retain), |
| 1916 | (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "", |
| 1917 | (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "", |
| 1918 | (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : ""); |
| 1919 | |
| 1920 | /* |
| 1921 | * If we no longer need to hold onto old our caps, and we may |
| 1922 | * have cached pages, but don't want them, then try to invalidate. |
| 1923 | * If we fail, it's because pages are locked.... try again later. |
| 1924 | */ |
Yan, Zheng | 0f439c7 | 2018-01-08 14:44:10 +0800 | [diff] [blame] | 1925 | if ((!no_delay || mdsc->stopping) && |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 1926 | !S_ISDIR(inode->i_mode) && /* ignore readdir cache */ |
Yan, Zheng | 9abd4db | 2016-05-18 20:58:26 +0800 | [diff] [blame] | 1927 | !(ci->i_wb_ref || ci->i_wrbuffer_ref) && /* no dirty pages... */ |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 1928 | inode->i_data.nrpages && /* have cached pages */ |
Yan, Zheng | 5e804ac | 2015-10-26 16:08:43 +0800 | [diff] [blame] | 1929 | (revoking & (CEPH_CAP_FILE_CACHE| |
| 1930 | CEPH_CAP_FILE_LAZYIO)) && /* or revoking cache */ |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1931 | !tried_invalidate) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1932 | dout("check_caps trying to invalidate on %p\n", inode); |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 1933 | if (try_nonblocking_invalidate(inode) < 0) { |
Yan, Zheng | ee612d9 | 2018-01-08 14:36:25 +0800 | [diff] [blame] | 1934 | dout("check_caps queuing invalidate\n"); |
| 1935 | queue_invalidate = true; |
| 1936 | ci->i_rdcache_revoking = ci->i_rdcache_gen; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1937 | } |
Yan, Zheng | 3609404 | 2016-07-06 15:37:42 +0800 | [diff] [blame] | 1938 | tried_invalidate = true; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1939 | goto retry_locked; |
| 1940 | } |
| 1941 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1942 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 1943 | cap = rb_entry(p, struct ceph_cap, ci_node); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1944 | |
| 1945 | /* avoid looping forever */ |
| 1946 | if (mds >= cap->mds || |
| 1947 | ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap)) |
| 1948 | continue; |
| 1949 | |
| 1950 | /* NOTE: no side-effects allowed, until we take s_mutex */ |
| 1951 | |
Yan, Zheng | 395c312 | 2013-01-04 14:37:57 +0800 | [diff] [blame] | 1952 | cap_used = used; |
| 1953 | if (ci->i_auth_cap && cap != ci->i_auth_cap) |
| 1954 | cap_used &= ~ci->i_auth_cap->issued; |
| 1955 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1956 | revoking = cap->implemented & ~cap->issued; |
Yan, Zheng | 395c312 | 2013-01-04 14:37:57 +0800 | [diff] [blame] | 1957 | dout(" mds%d cap %p used %s issued %s implemented %s revoking %s\n", |
Yan, Zheng | 9abd4db | 2016-05-18 20:58:26 +0800 | [diff] [blame] | 1958 | cap->mds, cap, ceph_cap_string(cap_used), |
| 1959 | ceph_cap_string(cap->issued), |
Sage Weil | 088b3f5 | 2011-01-18 08:56:01 -0800 | [diff] [blame] | 1960 | ceph_cap_string(cap->implemented), |
| 1961 | ceph_cap_string(revoking)); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1962 | |
| 1963 | if (cap == ci->i_auth_cap && |
| 1964 | (cap->issued & CEPH_CAP_FILE_WR)) { |
| 1965 | /* request larger max_size from MDS? */ |
| 1966 | if (ci->i_wanted_max_size > ci->i_max_size && |
| 1967 | ci->i_wanted_max_size > ci->i_requested_max_size) { |
| 1968 | dout("requesting new max_size\n"); |
| 1969 | goto ack; |
| 1970 | } |
| 1971 | |
| 1972 | /* approaching file_max? */ |
Yan, Zheng | efb0ca7 | 2017-05-22 12:03:32 +0800 | [diff] [blame] | 1973 | if (__ceph_should_report_size(ci)) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1974 | dout("i_size approaching max_size\n"); |
| 1975 | goto ack; |
| 1976 | } |
| 1977 | } |
| 1978 | /* flush anything dirty? */ |
Yan, Zheng | 7bc00fd | 2016-07-07 18:34:45 +0800 | [diff] [blame] | 1979 | if (cap == ci->i_auth_cap) { |
| 1980 | if ((flags & CHECK_CAPS_FLUSH) && ci->i_dirty_caps) { |
| 1981 | dout("flushing dirty caps\n"); |
| 1982 | goto ack; |
| 1983 | } |
| 1984 | if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS) { |
| 1985 | dout("flushing snap caps\n"); |
| 1986 | goto ack; |
| 1987 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | /* completed revocation? going down and there are no caps? */ |
Yan, Zheng | 395c312 | 2013-01-04 14:37:57 +0800 | [diff] [blame] | 1991 | if (revoking && (revoking & cap_used) == 0) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1992 | dout("completed revocation of %s\n", |
| 1993 | ceph_cap_string(cap->implemented & ~cap->issued)); |
| 1994 | goto ack; |
| 1995 | } |
| 1996 | |
| 1997 | /* want more caps from mds? */ |
| 1998 | if (want & ~(cap->mds_wanted | cap->issued)) |
| 1999 | goto ack; |
| 2000 | |
| 2001 | /* things we might delay */ |
Yan, Zheng | fdac94f | 2018-11-22 15:26:01 +0800 | [diff] [blame] | 2002 | if ((cap->issued & ~retain) == 0) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2003 | continue; /* nope, all good */ |
| 2004 | |
Yan, Zheng | 0f439c7 | 2018-01-08 14:44:10 +0800 | [diff] [blame] | 2005 | if (no_delay) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2006 | goto ack; |
| 2007 | |
| 2008 | /* delay? */ |
| 2009 | if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 && |
| 2010 | time_before(jiffies, ci->i_hold_caps_max)) { |
| 2011 | dout(" delaying issued %s -> %s, wanted %s -> %s\n", |
| 2012 | ceph_cap_string(cap->issued), |
| 2013 | ceph_cap_string(cap->issued & retain), |
| 2014 | ceph_cap_string(cap->mds_wanted), |
| 2015 | ceph_cap_string(want)); |
| 2016 | delayed++; |
| 2017 | continue; |
| 2018 | } |
| 2019 | |
| 2020 | ack: |
Sage Weil | e9964c1 | 2010-03-01 15:16:56 -0800 | [diff] [blame] | 2021 | if (ci->i_ceph_flags & CEPH_I_NOFLUSH) { |
| 2022 | dout(" skipping %p I_NOFLUSH set\n", inode); |
| 2023 | continue; |
| 2024 | } |
| 2025 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2026 | if (session && session != cap->session) { |
| 2027 | dout("oops, wrong session %p mutex\n", session); |
| 2028 | mutex_unlock(&session->s_mutex); |
| 2029 | session = NULL; |
| 2030 | } |
| 2031 | if (!session) { |
| 2032 | session = cap->session; |
| 2033 | if (mutex_trylock(&session->s_mutex) == 0) { |
| 2034 | dout("inverting session/ino locks on %p\n", |
| 2035 | session); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2036 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2037 | if (took_snap_rwsem) { |
| 2038 | up_read(&mdsc->snap_rwsem); |
| 2039 | took_snap_rwsem = 0; |
| 2040 | } |
| 2041 | mutex_lock(&session->s_mutex); |
| 2042 | goto retry; |
| 2043 | } |
| 2044 | } |
Yan, Zheng | 7bc00fd | 2016-07-07 18:34:45 +0800 | [diff] [blame] | 2045 | |
| 2046 | /* kick flushing and flush snaps before sending normal |
| 2047 | * cap message */ |
| 2048 | if (cap == ci->i_auth_cap && |
| 2049 | (ci->i_ceph_flags & |
| 2050 | (CEPH_I_KICK_FLUSH | CEPH_I_FLUSH_SNAPS))) { |
| 2051 | if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) { |
Yan, Zheng | 24d063a | 2017-08-15 11:37:32 +0800 | [diff] [blame] | 2052 | __kick_flushing_caps(mdsc, session, ci, 0); |
Yan, Zheng | 7bc00fd | 2016-07-07 18:34:45 +0800 | [diff] [blame] | 2053 | ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH; |
| 2054 | } |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 2055 | if (ci->i_ceph_flags & CEPH_I_FLUSH_SNAPS) |
| 2056 | __ceph_flush_snaps(ci, session); |
| 2057 | |
Yan, Zheng | 7bc00fd | 2016-07-07 18:34:45 +0800 | [diff] [blame] | 2058 | goto retry_locked; |
| 2059 | } |
| 2060 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2061 | /* take snap_rwsem after session mutex */ |
| 2062 | if (!took_snap_rwsem) { |
| 2063 | if (down_read_trylock(&mdsc->snap_rwsem) == 0) { |
| 2064 | dout("inverting snap/in locks on %p\n", |
| 2065 | inode); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2066 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2067 | down_read(&mdsc->snap_rwsem); |
| 2068 | took_snap_rwsem = 1; |
| 2069 | goto retry; |
| 2070 | } |
| 2071 | took_snap_rwsem = 1; |
| 2072 | } |
| 2073 | |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2074 | if (cap == ci->i_auth_cap && ci->i_dirty_caps) { |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 2075 | flushing = __mark_caps_flushing(inode, session, false, |
Yan, Zheng | a2971c8 | 2015-06-10 11:09:32 +0800 | [diff] [blame] | 2076 | &flush_tid, |
| 2077 | &oldest_flush_tid); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2078 | } else { |
Sage Weil | 24be0c4 | 2011-01-18 08:48:06 -0800 | [diff] [blame] | 2079 | flushing = 0; |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2080 | flush_tid = 0; |
Yan, Zheng | a2971c8 | 2015-06-10 11:09:32 +0800 | [diff] [blame] | 2081 | spin_lock(&mdsc->cap_dirty_lock); |
| 2082 | oldest_flush_tid = __get_oldest_flush_tid(mdsc); |
| 2083 | spin_unlock(&mdsc->cap_dirty_lock); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2084 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2085 | |
| 2086 | mds = cap->mds; /* remember mds, so we don't repeat */ |
| 2087 | sent++; |
| 2088 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2089 | /* __send_cap drops i_ceph_lock */ |
Jeff Layton | 1e4ef0c | 2016-11-10 07:42:06 -0500 | [diff] [blame] | 2090 | delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, false, |
| 2091 | cap_used, want, retain, flushing, |
| 2092 | flush_tid, oldest_flush_tid); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2093 | goto retry; /* retake i_ceph_lock and restart our cap scan. */ |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2094 | } |
| 2095 | |
Yan, Zheng | 0f439c7 | 2018-01-08 14:44:10 +0800 | [diff] [blame] | 2096 | /* Reschedule delayed caps release if we delayed anything */ |
| 2097 | if (delayed) |
Xuehan Xu | 6680288 | 2018-10-11 17:55:39 +0800 | [diff] [blame] | 2098 | __cap_delay_requeue(mdsc, ci, false); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2099 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2100 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2101 | |
Sage Weil | cbd0363 | 2010-02-09 13:41:18 -0800 | [diff] [blame] | 2102 | if (queue_invalidate) |
Sage Weil | 3c6f6b7 | 2010-02-09 15:24:44 -0800 | [diff] [blame] | 2103 | ceph_queue_invalidate(inode); |
Sage Weil | cbd0363 | 2010-02-09 13:41:18 -0800 | [diff] [blame] | 2104 | |
Sage Weil | cdc2ce0 | 2010-03-16 13:39:28 -0700 | [diff] [blame] | 2105 | if (session) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2106 | mutex_unlock(&session->s_mutex); |
| 2107 | if (took_snap_rwsem) |
| 2108 | up_read(&mdsc->snap_rwsem); |
| 2109 | } |
| 2110 | |
| 2111 | /* |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2112 | * Try to flush dirty caps back to the auth mds. |
| 2113 | */ |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2114 | static int try_flush_caps(struct inode *inode, u64 *ptid) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2115 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2116 | struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2117 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | 4fe5978 | 2013-10-31 16:44:14 +0800 | [diff] [blame] | 2118 | struct ceph_mds_session *session = NULL; |
Yan, Zheng | 89b52fe | 2015-05-27 09:59:48 +0800 | [diff] [blame] | 2119 | int flushing = 0; |
Yan, Zheng | a2971c8 | 2015-06-10 11:09:32 +0800 | [diff] [blame] | 2120 | u64 flush_tid = 0, oldest_flush_tid = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2121 | |
| 2122 | retry: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2123 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | e9964c1 | 2010-03-01 15:16:56 -0800 | [diff] [blame] | 2124 | if (ci->i_ceph_flags & CEPH_I_NOFLUSH) { |
Jeff Layton | 6c2838f | 2017-10-19 08:52:58 -0400 | [diff] [blame] | 2125 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | e9964c1 | 2010-03-01 15:16:56 -0800 | [diff] [blame] | 2126 | dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode); |
| 2127 | goto out; |
| 2128 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2129 | if (ci->i_dirty_caps && ci->i_auth_cap) { |
| 2130 | struct ceph_cap *cap = ci->i_auth_cap; |
| 2131 | int used = __ceph_caps_used(ci); |
| 2132 | int want = __ceph_caps_wanted(ci); |
| 2133 | int delayed; |
| 2134 | |
Yan, Zheng | 4fe5978 | 2013-10-31 16:44:14 +0800 | [diff] [blame] | 2135 | if (!session || session != cap->session) { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2136 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | 4fe5978 | 2013-10-31 16:44:14 +0800 | [diff] [blame] | 2137 | if (session) |
| 2138 | mutex_unlock(&session->s_mutex); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2139 | session = cap->session; |
| 2140 | mutex_lock(&session->s_mutex); |
| 2141 | goto retry; |
| 2142 | } |
Jeff Layton | 6c2838f | 2017-10-19 08:52:58 -0400 | [diff] [blame] | 2143 | if (cap->session->s_state < CEPH_MDS_SESSION_OPEN) { |
| 2144 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2145 | goto out; |
Jeff Layton | 6c2838f | 2017-10-19 08:52:58 -0400 | [diff] [blame] | 2146 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2147 | |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 2148 | flushing = __mark_caps_flushing(inode, session, true, |
| 2149 | &flush_tid, &oldest_flush_tid); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2150 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2151 | /* __send_cap drops i_ceph_lock */ |
Jeff Layton | 1e4ef0c | 2016-11-10 07:42:06 -0500 | [diff] [blame] | 2152 | delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, true, |
| 2153 | used, want, (cap->issued | cap->implemented), |
| 2154 | flushing, flush_tid, oldest_flush_tid); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2155 | |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2156 | if (delayed) { |
| 2157 | spin_lock(&ci->i_ceph_lock); |
Xuehan Xu | 6680288 | 2018-10-11 17:55:39 +0800 | [diff] [blame] | 2158 | __cap_delay_requeue(mdsc, ci, true); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2159 | spin_unlock(&ci->i_ceph_lock); |
| 2160 | } |
| 2161 | } else { |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 2162 | if (!list_empty(&ci->i_cap_flush_list)) { |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2163 | struct ceph_cap_flush *cf = |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 2164 | list_last_entry(&ci->i_cap_flush_list, |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 2165 | struct ceph_cap_flush, i_list); |
| 2166 | cf->wake = true; |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2167 | flush_tid = cf->tid; |
| 2168 | } |
| 2169 | flushing = ci->i_flushing_caps; |
| 2170 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2171 | } |
| 2172 | out: |
Yan, Zheng | 4fe5978 | 2013-10-31 16:44:14 +0800 | [diff] [blame] | 2173 | if (session) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2174 | mutex_unlock(&session->s_mutex); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2175 | |
| 2176 | *ptid = flush_tid; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2177 | return flushing; |
| 2178 | } |
| 2179 | |
| 2180 | /* |
| 2181 | * Return true if we've flushed caps through the given flush_tid. |
| 2182 | */ |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2183 | static int caps_are_flushed(struct inode *inode, u64 flush_tid) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2184 | { |
| 2185 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2186 | int ret = 1; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2187 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2188 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 2189 | if (!list_empty(&ci->i_cap_flush_list)) { |
| 2190 | struct ceph_cap_flush * cf = |
| 2191 | list_first_entry(&ci->i_cap_flush_list, |
| 2192 | struct ceph_cap_flush, i_list); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2193 | if (cf->tid <= flush_tid) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2194 | ret = 0; |
Yan, Zheng | 89b52fe | 2015-05-27 09:59:48 +0800 | [diff] [blame] | 2195 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2196 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2197 | return ret; |
| 2198 | } |
| 2199 | |
| 2200 | /* |
Yan, Zheng | 68cd5b4 | 2015-10-27 18:36:06 +0800 | [diff] [blame] | 2201 | * wait for any unsafe requests to complete. |
Yan, Zheng | da819c8 | 2015-05-27 11:19:34 +0800 | [diff] [blame] | 2202 | */ |
Yan, Zheng | 68cd5b4 | 2015-10-27 18:36:06 +0800 | [diff] [blame] | 2203 | static int unsafe_request_wait(struct inode *inode) |
Yan, Zheng | da819c8 | 2015-05-27 11:19:34 +0800 | [diff] [blame] | 2204 | { |
| 2205 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | 68cd5b4 | 2015-10-27 18:36:06 +0800 | [diff] [blame] | 2206 | struct ceph_mds_request *req1 = NULL, *req2 = NULL; |
| 2207 | int ret, err = 0; |
Yan, Zheng | da819c8 | 2015-05-27 11:19:34 +0800 | [diff] [blame] | 2208 | |
| 2209 | spin_lock(&ci->i_unsafe_lock); |
Yan, Zheng | 68cd5b4 | 2015-10-27 18:36:06 +0800 | [diff] [blame] | 2210 | if (S_ISDIR(inode->i_mode) && !list_empty(&ci->i_unsafe_dirops)) { |
| 2211 | req1 = list_last_entry(&ci->i_unsafe_dirops, |
| 2212 | struct ceph_mds_request, |
| 2213 | r_unsafe_dir_item); |
| 2214 | ceph_mdsc_get_request(req1); |
| 2215 | } |
| 2216 | if (!list_empty(&ci->i_unsafe_iops)) { |
| 2217 | req2 = list_last_entry(&ci->i_unsafe_iops, |
| 2218 | struct ceph_mds_request, |
| 2219 | r_unsafe_target_item); |
| 2220 | ceph_mdsc_get_request(req2); |
| 2221 | } |
Yan, Zheng | da819c8 | 2015-05-27 11:19:34 +0800 | [diff] [blame] | 2222 | spin_unlock(&ci->i_unsafe_lock); |
Yan, Zheng | 68cd5b4 | 2015-10-27 18:36:06 +0800 | [diff] [blame] | 2223 | |
Jeff Layton | 4945a08 | 2016-11-16 09:45:22 -0500 | [diff] [blame] | 2224 | dout("unsafe_request_wait %p wait on tid %llu %llu\n", |
Yan, Zheng | 68cd5b4 | 2015-10-27 18:36:06 +0800 | [diff] [blame] | 2225 | inode, req1 ? req1->r_tid : 0ULL, req2 ? req2->r_tid : 0ULL); |
| 2226 | if (req1) { |
| 2227 | ret = !wait_for_completion_timeout(&req1->r_safe_completion, |
| 2228 | ceph_timeout_jiffies(req1->r_timeout)); |
| 2229 | if (ret) |
| 2230 | err = -EIO; |
| 2231 | ceph_mdsc_put_request(req1); |
| 2232 | } |
| 2233 | if (req2) { |
| 2234 | ret = !wait_for_completion_timeout(&req2->r_safe_completion, |
| 2235 | ceph_timeout_jiffies(req2->r_timeout)); |
| 2236 | if (ret) |
| 2237 | err = -EIO; |
| 2238 | ceph_mdsc_put_request(req2); |
| 2239 | } |
| 2240 | return err; |
Yan, Zheng | da819c8 | 2015-05-27 11:19:34 +0800 | [diff] [blame] | 2241 | } |
| 2242 | |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 2243 | int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2244 | { |
Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 2245 | struct inode *inode = file->f_mapping->host; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2246 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2247 | u64 flush_tid; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2248 | int ret; |
| 2249 | int dirty; |
| 2250 | |
| 2251 | dout("fsync %p%s\n", inode, datasync ? " datasync" : ""); |
Yan, Zheng | 9a5530c | 2016-06-15 16:29:18 +0800 | [diff] [blame] | 2252 | |
Jeff Layton | b74fcea | 2017-07-25 10:50:41 -0400 | [diff] [blame] | 2253 | ret = file_write_and_wait_range(file, start, end); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2254 | if (ret < 0) |
Yan, Zheng | da819c8 | 2015-05-27 11:19:34 +0800 | [diff] [blame] | 2255 | goto out; |
| 2256 | |
| 2257 | if (datasync) |
| 2258 | goto out; |
| 2259 | |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2260 | dirty = try_flush_caps(inode, &flush_tid); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2261 | dout("fsync dirty caps are %s\n", ceph_cap_string(dirty)); |
| 2262 | |
Yan, Zheng | 68cd5b4 | 2015-10-27 18:36:06 +0800 | [diff] [blame] | 2263 | ret = unsafe_request_wait(inode); |
Yan, Zheng | da819c8 | 2015-05-27 11:19:34 +0800 | [diff] [blame] | 2264 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2265 | /* |
| 2266 | * only wait on non-file metadata writeback (the mds |
| 2267 | * can recover size and mtime, so we don't need to |
| 2268 | * wait for that) |
| 2269 | */ |
Yan, Zheng | da819c8 | 2015-05-27 11:19:34 +0800 | [diff] [blame] | 2270 | if (!ret && (dirty & ~CEPH_CAP_ANY_FILE_WR)) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2271 | ret = wait_event_interruptible(ci->i_cap_wq, |
Yan, Zheng | da819c8 | 2015-05-27 11:19:34 +0800 | [diff] [blame] | 2272 | caps_are_flushed(inode, flush_tid)); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2273 | } |
Yan, Zheng | da819c8 | 2015-05-27 11:19:34 +0800 | [diff] [blame] | 2274 | out: |
| 2275 | dout("fsync %p%s result=%d\n", inode, datasync ? " datasync" : "", ret); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2276 | return ret; |
| 2277 | } |
| 2278 | |
| 2279 | /* |
| 2280 | * Flush any dirty caps back to the mds. If we aren't asked to wait, |
| 2281 | * queue inode for flush but don't do so immediately, because we can |
| 2282 | * get by with fewer MDS messages if we wait for data writeback to |
| 2283 | * complete first. |
| 2284 | */ |
Stephen Rothwell | f1a3d57 | 2010-01-18 11:53:08 +1100 | [diff] [blame] | 2285 | int ceph_write_inode(struct inode *inode, struct writeback_control *wbc) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2286 | { |
| 2287 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2288 | u64 flush_tid; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2289 | int err = 0; |
| 2290 | int dirty; |
Chengguang Xu | 16515a6 | 2018-01-30 10:02:30 +0800 | [diff] [blame] | 2291 | int wait = (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2292 | |
| 2293 | dout("write_inode %p wait=%d\n", inode, wait); |
| 2294 | if (wait) { |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2295 | dirty = try_flush_caps(inode, &flush_tid); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2296 | if (dirty) |
| 2297 | err = wait_event_interruptible(ci->i_cap_wq, |
| 2298 | caps_are_flushed(inode, flush_tid)); |
| 2299 | } else { |
Cheng Renquan | 640ef79 | 2010-03-26 17:40:33 +0800 | [diff] [blame] | 2300 | struct ceph_mds_client *mdsc = |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2301 | ceph_sb_to_client(inode->i_sb)->mdsc; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2302 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2303 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2304 | if (__ceph_caps_dirty(ci)) |
| 2305 | __cap_delay_requeue_front(mdsc, ci); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2306 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2307 | } |
| 2308 | return err; |
| 2309 | } |
| 2310 | |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2311 | static void __kick_flushing_caps(struct ceph_mds_client *mdsc, |
| 2312 | struct ceph_mds_session *session, |
| 2313 | struct ceph_inode_info *ci, |
| 2314 | u64 oldest_flush_tid) |
| 2315 | __releases(ci->i_ceph_lock) |
| 2316 | __acquires(ci->i_ceph_lock) |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2317 | { |
| 2318 | struct inode *inode = &ci->vfs_inode; |
| 2319 | struct ceph_cap *cap; |
| 2320 | struct ceph_cap_flush *cf; |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2321 | int ret; |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2322 | u64 first_tid = 0; |
Yan, Zheng | a2971c8 | 2015-06-10 11:09:32 +0800 | [diff] [blame] | 2323 | |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 2324 | list_for_each_entry(cf, &ci->i_cap_flush_list, i_list) { |
| 2325 | if (cf->tid < first_tid) |
| 2326 | continue; |
| 2327 | |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2328 | cap = ci->i_auth_cap; |
| 2329 | if (!(cap && cap->session == session)) { |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2330 | pr_err("%p auth cap %p not mds%d ???\n", |
| 2331 | inode, cap, session->s_mds); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2332 | break; |
| 2333 | } |
| 2334 | |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2335 | first_tid = cf->tid + 1; |
| 2336 | |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2337 | if (cf->caps) { |
| 2338 | dout("kick_flushing_caps %p cap %p tid %llu %s\n", |
| 2339 | inode, cap, cf->tid, ceph_cap_string(cf->caps)); |
| 2340 | ci->i_ceph_flags |= CEPH_I_NODELAY; |
| 2341 | ret = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, |
Jeff Layton | 1e4ef0c | 2016-11-10 07:42:06 -0500 | [diff] [blame] | 2342 | false, __ceph_caps_used(ci), |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2343 | __ceph_caps_wanted(ci), |
| 2344 | cap->issued | cap->implemented, |
| 2345 | cf->caps, cf->tid, oldest_flush_tid); |
| 2346 | if (ret) { |
| 2347 | pr_err("kick_flushing_caps: error sending " |
| 2348 | "cap flush, ino (%llx.%llx) " |
| 2349 | "tid %llu flushing %s\n", |
| 2350 | ceph_vinop(inode), cf->tid, |
| 2351 | ceph_cap_string(cf->caps)); |
| 2352 | } |
| 2353 | } else { |
| 2354 | struct ceph_cap_snap *capsnap = |
| 2355 | container_of(cf, struct ceph_cap_snap, |
| 2356 | cap_flush); |
| 2357 | dout("kick_flushing_caps %p capsnap %p tid %llu %s\n", |
| 2358 | inode, capsnap, cf->tid, |
| 2359 | ceph_cap_string(capsnap->dirty)); |
| 2360 | |
Elena Reshetova | 805692d | 2017-03-03 11:15:07 +0200 | [diff] [blame] | 2361 | refcount_inc(&capsnap->nref); |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2362 | spin_unlock(&ci->i_ceph_lock); |
| 2363 | |
| 2364 | ret = __send_flush_snap(inode, session, capsnap, cap->mseq, |
| 2365 | oldest_flush_tid); |
| 2366 | if (ret < 0) { |
| 2367 | pr_err("kick_flushing_caps: error sending " |
| 2368 | "cap flushsnap, ino (%llx.%llx) " |
| 2369 | "tid %llu follows %llu\n", |
| 2370 | ceph_vinop(inode), cf->tid, |
| 2371 | capsnap->follows); |
| 2372 | } |
| 2373 | |
| 2374 | ceph_put_cap_snap(capsnap); |
| 2375 | } |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 2376 | |
| 2377 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2378 | } |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2379 | } |
| 2380 | |
Yan, Zheng | e548e9b | 2015-06-10 15:17:56 +0800 | [diff] [blame] | 2381 | void ceph_early_kick_flushing_caps(struct ceph_mds_client *mdsc, |
| 2382 | struct ceph_mds_session *session) |
| 2383 | { |
| 2384 | struct ceph_inode_info *ci; |
| 2385 | struct ceph_cap *cap; |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2386 | u64 oldest_flush_tid; |
Yan, Zheng | e548e9b | 2015-06-10 15:17:56 +0800 | [diff] [blame] | 2387 | |
| 2388 | dout("early_kick_flushing_caps mds%d\n", session->s_mds); |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2389 | |
| 2390 | spin_lock(&mdsc->cap_dirty_lock); |
| 2391 | oldest_flush_tid = __get_oldest_flush_tid(mdsc); |
| 2392 | spin_unlock(&mdsc->cap_dirty_lock); |
| 2393 | |
Yan, Zheng | e548e9b | 2015-06-10 15:17:56 +0800 | [diff] [blame] | 2394 | list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) { |
| 2395 | spin_lock(&ci->i_ceph_lock); |
| 2396 | cap = ci->i_auth_cap; |
| 2397 | if (!(cap && cap->session == session)) { |
| 2398 | pr_err("%p auth cap %p not mds%d ???\n", |
| 2399 | &ci->vfs_inode, cap, session->s_mds); |
| 2400 | spin_unlock(&ci->i_ceph_lock); |
| 2401 | continue; |
| 2402 | } |
| 2403 | |
| 2404 | |
| 2405 | /* |
| 2406 | * if flushing caps were revoked, we re-send the cap flush |
| 2407 | * in client reconnect stage. This guarantees MDS * processes |
| 2408 | * the cap flush message before issuing the flushing caps to |
| 2409 | * other client. |
| 2410 | */ |
| 2411 | if ((cap->issued & ci->i_flushing_caps) != |
| 2412 | ci->i_flushing_caps) { |
Yan, Zheng | 13c2b57 | 2016-07-05 16:45:21 +0800 | [diff] [blame] | 2413 | ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH; |
Yan, Zheng | 81c5a14 | 2019-01-01 16:28:33 +0800 | [diff] [blame] | 2414 | /* encode_caps_cb() also will reset these sequence |
| 2415 | * numbers. make sure sequence numbers in cap flush |
| 2416 | * message match later reconnect message */ |
| 2417 | cap->seq = 0; |
| 2418 | cap->issue_seq = 0; |
| 2419 | cap->mseq = 0; |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2420 | __kick_flushing_caps(mdsc, session, ci, |
| 2421 | oldest_flush_tid); |
Yan, Zheng | 13c2b57 | 2016-07-05 16:45:21 +0800 | [diff] [blame] | 2422 | } else { |
| 2423 | ci->i_ceph_flags |= CEPH_I_KICK_FLUSH; |
Yan, Zheng | e548e9b | 2015-06-10 15:17:56 +0800 | [diff] [blame] | 2424 | } |
| 2425 | |
Yan, Zheng | e548e9b | 2015-06-10 15:17:56 +0800 | [diff] [blame] | 2426 | spin_unlock(&ci->i_ceph_lock); |
| 2427 | } |
| 2428 | } |
| 2429 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2430 | void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc, |
| 2431 | struct ceph_mds_session *session) |
| 2432 | { |
| 2433 | struct ceph_inode_info *ci; |
Yan, Zheng | 13c2b57 | 2016-07-05 16:45:21 +0800 | [diff] [blame] | 2434 | struct ceph_cap *cap; |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2435 | u64 oldest_flush_tid; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2436 | |
| 2437 | dout("kick_flushing_caps mds%d\n", session->s_mds); |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2438 | |
| 2439 | spin_lock(&mdsc->cap_dirty_lock); |
| 2440 | oldest_flush_tid = __get_oldest_flush_tid(mdsc); |
| 2441 | spin_unlock(&mdsc->cap_dirty_lock); |
| 2442 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2443 | list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) { |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2444 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | 13c2b57 | 2016-07-05 16:45:21 +0800 | [diff] [blame] | 2445 | cap = ci->i_auth_cap; |
| 2446 | if (!(cap && cap->session == session)) { |
| 2447 | pr_err("%p auth cap %p not mds%d ???\n", |
| 2448 | &ci->vfs_inode, cap, session->s_mds); |
| 2449 | spin_unlock(&ci->i_ceph_lock); |
| 2450 | continue; |
| 2451 | } |
| 2452 | if (ci->i_ceph_flags & CEPH_I_KICK_FLUSH) { |
| 2453 | ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH; |
| 2454 | __kick_flushing_caps(mdsc, session, ci, |
| 2455 | oldest_flush_tid); |
| 2456 | } |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2457 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2458 | } |
| 2459 | } |
| 2460 | |
Sage Weil | 088b3f5 | 2011-01-18 08:56:01 -0800 | [diff] [blame] | 2461 | static void kick_flushing_inode_caps(struct ceph_mds_client *mdsc, |
| 2462 | struct ceph_mds_session *session, |
| 2463 | struct inode *inode) |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2464 | __releases(ci->i_ceph_lock) |
Sage Weil | 088b3f5 | 2011-01-18 08:56:01 -0800 | [diff] [blame] | 2465 | { |
| 2466 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 2467 | struct ceph_cap *cap; |
Sage Weil | 088b3f5 | 2011-01-18 08:56:01 -0800 | [diff] [blame] | 2468 | |
Sage Weil | 088b3f5 | 2011-01-18 08:56:01 -0800 | [diff] [blame] | 2469 | cap = ci->i_auth_cap; |
Yan, Zheng | 8310b08 | 2015-06-09 17:20:12 +0800 | [diff] [blame] | 2470 | dout("kick_flushing_inode_caps %p flushing %s\n", inode, |
| 2471 | ceph_cap_string(ci->i_flushing_caps)); |
Yan, Zheng | 005c469 | 2013-05-31 16:40:24 +0800 | [diff] [blame] | 2472 | |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2473 | if (!list_empty(&ci->i_cap_flush_list)) { |
| 2474 | u64 oldest_flush_tid; |
Yan, Zheng | 005c469 | 2013-05-31 16:40:24 +0800 | [diff] [blame] | 2475 | spin_lock(&mdsc->cap_dirty_lock); |
| 2476 | list_move_tail(&ci->i_flushing_item, |
| 2477 | &cap->session->s_cap_flushing); |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2478 | oldest_flush_tid = __get_oldest_flush_tid(mdsc); |
Yan, Zheng | 005c469 | 2013-05-31 16:40:24 +0800 | [diff] [blame] | 2479 | spin_unlock(&mdsc->cap_dirty_lock); |
| 2480 | |
Yan, Zheng | 13c2b57 | 2016-07-05 16:45:21 +0800 | [diff] [blame] | 2481 | ci->i_ceph_flags &= ~CEPH_I_KICK_FLUSH; |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2482 | __kick_flushing_caps(mdsc, session, ci, oldest_flush_tid); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 2483 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 088b3f5 | 2011-01-18 08:56:01 -0800 | [diff] [blame] | 2484 | } else { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2485 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 088b3f5 | 2011-01-18 08:56:01 -0800 | [diff] [blame] | 2486 | } |
| 2487 | } |
| 2488 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2489 | |
| 2490 | /* |
| 2491 | * Take references to capabilities we hold, so that we don't release |
| 2492 | * them to the MDS prematurely. |
| 2493 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2494 | * Protected by i_ceph_lock. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2495 | */ |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2496 | static void __take_cap_refs(struct ceph_inode_info *ci, int got, |
| 2497 | bool snap_rwsem_locked) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2498 | { |
| 2499 | if (got & CEPH_CAP_PIN) |
| 2500 | ci->i_pin_ref++; |
| 2501 | if (got & CEPH_CAP_FILE_RD) |
| 2502 | ci->i_rd_ref++; |
| 2503 | if (got & CEPH_CAP_FILE_CACHE) |
| 2504 | ci->i_rdcache_ref++; |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2505 | if (got & CEPH_CAP_FILE_WR) { |
| 2506 | if (ci->i_wr_ref == 0 && !ci->i_head_snapc) { |
| 2507 | BUG_ON(!snap_rwsem_locked); |
| 2508 | ci->i_head_snapc = ceph_get_snap_context( |
| 2509 | ci->i_snap_realm->cached_context); |
| 2510 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2511 | ci->i_wr_ref++; |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2512 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2513 | if (got & CEPH_CAP_FILE_BUFFER) { |
Henry C Chang | d3d0720 | 2011-05-11 10:29:54 +0000 | [diff] [blame] | 2514 | if (ci->i_wb_ref == 0) |
Sage Weil | 3772d26 | 2011-05-03 09:28:08 -0700 | [diff] [blame] | 2515 | ihold(&ci->vfs_inode); |
Henry C Chang | d3d0720 | 2011-05-11 10:29:54 +0000 | [diff] [blame] | 2516 | ci->i_wb_ref++; |
| 2517 | dout("__take_cap_refs %p wb %d -> %d (?)\n", |
| 2518 | &ci->vfs_inode, ci->i_wb_ref-1, ci->i_wb_ref); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2519 | } |
| 2520 | } |
| 2521 | |
| 2522 | /* |
| 2523 | * Try to grab cap references. Specify those refs we @want, and the |
| 2524 | * minimal set we @need. Also include the larger offset we are writing |
| 2525 | * to (when applicable), and check against max_size here as well. |
| 2526 | * Note that caller is responsible for ensuring max_size increases are |
| 2527 | * requested from the MDS. |
Jeff Layton | 1199d7d | 2019-04-02 15:58:05 -0400 | [diff] [blame] | 2528 | * |
| 2529 | * Returns 0 if caps were not able to be acquired (yet), a 1 if they were, |
| 2530 | * or a negative error code. |
| 2531 | * |
| 2532 | * FIXME: how does a 0 return differ from -EAGAIN? |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2533 | */ |
| 2534 | static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want, |
Jeff Layton | 1199d7d | 2019-04-02 15:58:05 -0400 | [diff] [blame] | 2535 | loff_t endoff, bool nonblock, int *got) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2536 | { |
| 2537 | struct inode *inode = &ci->vfs_inode; |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2538 | struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2539 | int ret = 0; |
Yan, Zheng | c4d4a58 | 2015-01-09 15:56:18 +0800 | [diff] [blame] | 2540 | int have, implemented; |
Sage Weil | 195d3ce | 2010-03-01 09:57:54 -0800 | [diff] [blame] | 2541 | int file_wanted; |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2542 | bool snap_rwsem_locked = false; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2543 | |
| 2544 | dout("get_cap_refs %p need %s want %s\n", inode, |
| 2545 | ceph_cap_string(need), ceph_cap_string(want)); |
Yan, Zheng | c4d4a58 | 2015-01-09 15:56:18 +0800 | [diff] [blame] | 2546 | |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2547 | again: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2548 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2549 | |
Sage Weil | 195d3ce | 2010-03-01 09:57:54 -0800 | [diff] [blame] | 2550 | /* make sure file is actually open */ |
| 2551 | file_wanted = __ceph_caps_file_wanted(ci); |
Yan, Zheng | 7731032 | 2016-04-08 15:27:16 +0800 | [diff] [blame] | 2552 | if ((file_wanted & need) != need) { |
Sage Weil | 195d3ce | 2010-03-01 09:57:54 -0800 | [diff] [blame] | 2553 | dout("try_get_cap_refs need %s file_wanted %s, EBADF\n", |
| 2554 | ceph_cap_string(need), ceph_cap_string(file_wanted)); |
Jeff Layton | 1199d7d | 2019-04-02 15:58:05 -0400 | [diff] [blame] | 2555 | ret = -EBADF; |
Yan, Zheng | 3738daa | 2014-11-14 22:10:07 +0800 | [diff] [blame] | 2556 | goto out_unlock; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2557 | } |
| 2558 | |
Yan, Zheng | 37505d5 | 2013-04-12 16:11:10 +0800 | [diff] [blame] | 2559 | /* finish pending truncate */ |
| 2560 | while (ci->i_truncate_pending) { |
| 2561 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2562 | if (snap_rwsem_locked) { |
| 2563 | up_read(&mdsc->snap_rwsem); |
| 2564 | snap_rwsem_locked = false; |
| 2565 | } |
Yan, Zheng | b415bf4 | 2013-07-02 12:40:19 +0800 | [diff] [blame] | 2566 | __ceph_do_pending_vmtruncate(inode); |
Yan, Zheng | 37505d5 | 2013-04-12 16:11:10 +0800 | [diff] [blame] | 2567 | spin_lock(&ci->i_ceph_lock); |
| 2568 | } |
| 2569 | |
Yan, Zheng | 3871cbb | 2013-08-05 14:10:29 +0800 | [diff] [blame] | 2570 | have = __ceph_caps_issued(ci, &implemented); |
| 2571 | |
| 2572 | if (have & need & CEPH_CAP_FILE_WR) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2573 | if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) { |
| 2574 | dout("get_cap_refs %p endoff %llu > maxsize %llu\n", |
| 2575 | inode, endoff, ci->i_max_size); |
Jeff Layton | 1199d7d | 2019-04-02 15:58:05 -0400 | [diff] [blame] | 2576 | if (endoff > ci->i_requested_max_size) |
| 2577 | ret = -EAGAIN; |
Yan, Zheng | 3738daa | 2014-11-14 22:10:07 +0800 | [diff] [blame] | 2578 | goto out_unlock; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2579 | } |
| 2580 | /* |
| 2581 | * If a sync write is in progress, we must wait, so that we |
| 2582 | * can get a final snapshot value for size+mtime. |
| 2583 | */ |
| 2584 | if (__ceph_have_pending_cap_snap(ci)) { |
| 2585 | dout("get_cap_refs %p cap_snap_pending\n", inode); |
Yan, Zheng | 3738daa | 2014-11-14 22:10:07 +0800 | [diff] [blame] | 2586 | goto out_unlock; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2587 | } |
| 2588 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2589 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2590 | if ((have & need) == need) { |
| 2591 | /* |
| 2592 | * Look at (implemented & ~have & not) so that we keep waiting |
| 2593 | * on transition from wanted -> needed caps. This is needed |
| 2594 | * for WRBUFFER|WR -> WR to avoid a new WR sync write from |
| 2595 | * going before a prior buffered writeback happens. |
| 2596 | */ |
| 2597 | int not = want & ~(have & need); |
| 2598 | int revoking = implemented & ~have; |
| 2599 | dout("get_cap_refs %p have %s but not %s (revoking %s)\n", |
| 2600 | inode, ceph_cap_string(have), ceph_cap_string(not), |
| 2601 | ceph_cap_string(revoking)); |
| 2602 | if ((revoking & not) == 0) { |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2603 | if (!snap_rwsem_locked && |
| 2604 | !ci->i_head_snapc && |
| 2605 | (need & CEPH_CAP_FILE_WR)) { |
| 2606 | if (!down_read_trylock(&mdsc->snap_rwsem)) { |
| 2607 | /* |
| 2608 | * we can not call down_read() when |
| 2609 | * task isn't in TASK_RUNNING state |
| 2610 | */ |
| 2611 | if (nonblock) { |
Jeff Layton | 1199d7d | 2019-04-02 15:58:05 -0400 | [diff] [blame] | 2612 | ret = -EAGAIN; |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2613 | goto out_unlock; |
| 2614 | } |
| 2615 | |
| 2616 | spin_unlock(&ci->i_ceph_lock); |
| 2617 | down_read(&mdsc->snap_rwsem); |
| 2618 | snap_rwsem_locked = true; |
| 2619 | goto again; |
| 2620 | } |
| 2621 | snap_rwsem_locked = true; |
| 2622 | } |
Yan, Zheng | c4d4a58 | 2015-01-09 15:56:18 +0800 | [diff] [blame] | 2623 | *got = need | (have & want); |
Yan, Zheng | f7f7e7a | 2016-05-18 20:31:55 +0800 | [diff] [blame] | 2624 | if ((need & CEPH_CAP_FILE_RD) && |
| 2625 | !(*got & CEPH_CAP_FILE_CACHE)) |
| 2626 | ceph_disable_fscache_readpage(ci); |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2627 | __take_cap_refs(ci, *got, true); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2628 | ret = 1; |
| 2629 | } |
| 2630 | } else { |
Yan, Zheng | 03f4fcb | 2015-01-05 11:04:04 +0800 | [diff] [blame] | 2631 | int session_readonly = false; |
| 2632 | if ((need & CEPH_CAP_FILE_WR) && ci->i_auth_cap) { |
| 2633 | struct ceph_mds_session *s = ci->i_auth_cap->session; |
| 2634 | spin_lock(&s->s_cap_lock); |
| 2635 | session_readonly = s->s_readonly; |
| 2636 | spin_unlock(&s->s_cap_lock); |
| 2637 | } |
| 2638 | if (session_readonly) { |
| 2639 | dout("get_cap_refs %p needed %s but mds%d readonly\n", |
| 2640 | inode, ceph_cap_string(need), ci->i_auth_cap->mds); |
Jeff Layton | 1199d7d | 2019-04-02 15:58:05 -0400 | [diff] [blame] | 2641 | ret = -EROFS; |
Yan, Zheng | 03f4fcb | 2015-01-05 11:04:04 +0800 | [diff] [blame] | 2642 | goto out_unlock; |
| 2643 | } |
| 2644 | |
Yan, Zheng | 7731032 | 2016-04-08 15:27:16 +0800 | [diff] [blame] | 2645 | if (ci->i_ceph_flags & CEPH_I_CAP_DROPPED) { |
| 2646 | int mds_wanted; |
Seraphime Kirkovski | 52953d5 | 2016-12-26 10:26:34 +0100 | [diff] [blame] | 2647 | if (READ_ONCE(mdsc->fsc->mount_state) == |
Yan, Zheng | 7731032 | 2016-04-08 15:27:16 +0800 | [diff] [blame] | 2648 | CEPH_MOUNT_SHUTDOWN) { |
| 2649 | dout("get_cap_refs %p forced umount\n", inode); |
Jeff Layton | 1199d7d | 2019-04-02 15:58:05 -0400 | [diff] [blame] | 2650 | ret = -EIO; |
Yan, Zheng | 7731032 | 2016-04-08 15:27:16 +0800 | [diff] [blame] | 2651 | goto out_unlock; |
| 2652 | } |
Yan, Zheng | c1944fe | 2017-01-29 22:15:47 +0800 | [diff] [blame] | 2653 | mds_wanted = __ceph_caps_mds_wanted(ci, false); |
Yan, Zheng | eb65b91 | 2017-01-12 17:18:00 +0800 | [diff] [blame] | 2654 | if (need & ~(mds_wanted & need)) { |
Yan, Zheng | 7731032 | 2016-04-08 15:27:16 +0800 | [diff] [blame] | 2655 | dout("get_cap_refs %p caps were dropped" |
| 2656 | " (session killed?)\n", inode); |
Jeff Layton | 1199d7d | 2019-04-02 15:58:05 -0400 | [diff] [blame] | 2657 | ret = -ESTALE; |
Yan, Zheng | 7731032 | 2016-04-08 15:27:16 +0800 | [diff] [blame] | 2658 | goto out_unlock; |
| 2659 | } |
Yan, Zheng | eb65b91 | 2017-01-12 17:18:00 +0800 | [diff] [blame] | 2660 | if (!(file_wanted & ~mds_wanted)) |
Yan, Zheng | 7731032 | 2016-04-08 15:27:16 +0800 | [diff] [blame] | 2661 | ci->i_ceph_flags &= ~CEPH_I_CAP_DROPPED; |
Yan, Zheng | 48fec5d | 2015-07-01 16:27:46 +0800 | [diff] [blame] | 2662 | } |
| 2663 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2664 | dout("get_cap_refs %p have %s needed %s\n", inode, |
| 2665 | ceph_cap_string(have), ceph_cap_string(need)); |
| 2666 | } |
Yan, Zheng | 3738daa | 2014-11-14 22:10:07 +0800 | [diff] [blame] | 2667 | out_unlock: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2668 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2669 | if (snap_rwsem_locked) |
| 2670 | up_read(&mdsc->snap_rwsem); |
Yan, Zheng | 3738daa | 2014-11-14 22:10:07 +0800 | [diff] [blame] | 2671 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2672 | dout("get_cap_refs %p ret %d got %s\n", inode, |
Yan, Zheng | c4d4a58 | 2015-01-09 15:56:18 +0800 | [diff] [blame] | 2673 | ret, ceph_cap_string(*got)); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2674 | return ret; |
| 2675 | } |
| 2676 | |
| 2677 | /* |
| 2678 | * Check the offset we are writing up to against our current |
| 2679 | * max_size. If necessary, tell the MDS we want to write to |
| 2680 | * a larger offset. |
| 2681 | */ |
| 2682 | static void check_max_size(struct inode *inode, loff_t endoff) |
| 2683 | { |
| 2684 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 2685 | int check = 0; |
| 2686 | |
| 2687 | /* do we need to explicitly request a larger max_size? */ |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2688 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | 3871cbb | 2013-08-05 14:10:29 +0800 | [diff] [blame] | 2689 | if (endoff >= ci->i_max_size && endoff > ci->i_wanted_max_size) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2690 | dout("write %p at large endoff %llu, req max_size\n", |
| 2691 | inode, endoff); |
| 2692 | ci->i_wanted_max_size = endoff; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2693 | } |
Yan, Zheng | 3871cbb | 2013-08-05 14:10:29 +0800 | [diff] [blame] | 2694 | /* duplicate ceph_check_caps()'s logic */ |
| 2695 | if (ci->i_auth_cap && |
| 2696 | (ci->i_auth_cap->issued & CEPH_CAP_FILE_WR) && |
| 2697 | ci->i_wanted_max_size > ci->i_max_size && |
| 2698 | ci->i_wanted_max_size > ci->i_requested_max_size) |
| 2699 | check = 1; |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2700 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2701 | if (check) |
| 2702 | ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL); |
| 2703 | } |
| 2704 | |
Luis Henriques | 2ee9dd9 | 2018-10-15 16:45:57 +0100 | [diff] [blame] | 2705 | int ceph_try_get_caps(struct ceph_inode_info *ci, int need, int want, |
| 2706 | bool nonblock, int *got) |
Yan, Zheng | 2b1ac85 | 2016-10-25 10:51:55 +0800 | [diff] [blame] | 2707 | { |
Jeff Layton | 1199d7d | 2019-04-02 15:58:05 -0400 | [diff] [blame] | 2708 | int ret; |
Yan, Zheng | 2b1ac85 | 2016-10-25 10:51:55 +0800 | [diff] [blame] | 2709 | |
| 2710 | BUG_ON(need & ~CEPH_CAP_FILE_RD); |
Luis Henriques | 2ee9dd9 | 2018-10-15 16:45:57 +0100 | [diff] [blame] | 2711 | BUG_ON(want & ~(CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO|CEPH_CAP_FILE_SHARED)); |
Yan, Zheng | 2b1ac85 | 2016-10-25 10:51:55 +0800 | [diff] [blame] | 2712 | ret = ceph_pool_perm_check(ci, need); |
| 2713 | if (ret < 0) |
| 2714 | return ret; |
| 2715 | |
Jeff Layton | 1199d7d | 2019-04-02 15:58:05 -0400 | [diff] [blame] | 2716 | ret = try_get_cap_refs(ci, need, want, 0, nonblock, got); |
| 2717 | return ret == -EAGAIN ? 0 : ret; |
Yan, Zheng | 2b1ac85 | 2016-10-25 10:51:55 +0800 | [diff] [blame] | 2718 | } |
| 2719 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2720 | /* |
| 2721 | * Wait for caps, and take cap references. If we can't get a WR cap |
| 2722 | * due to a small max_size, make sure we check_max_size (and possibly |
| 2723 | * ask the mds) so we don't get hung up indefinitely. |
| 2724 | */ |
Yan, Zheng | 3738daa | 2014-11-14 22:10:07 +0800 | [diff] [blame] | 2725 | int ceph_get_caps(struct ceph_inode_info *ci, int need, int want, |
| 2726 | loff_t endoff, int *got, struct page **pinned_page) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2727 | { |
Jeff Layton | 1199d7d | 2019-04-02 15:58:05 -0400 | [diff] [blame] | 2728 | int _got, ret; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2729 | |
Yan, Zheng | 10183a6 | 2015-04-27 15:33:28 +0800 | [diff] [blame] | 2730 | ret = ceph_pool_perm_check(ci, need); |
| 2731 | if (ret < 0) |
| 2732 | return ret; |
| 2733 | |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2734 | while (true) { |
| 2735 | if (endoff > 0) |
| 2736 | check_max_size(&ci->vfs_inode, endoff); |
Yan, Zheng | c4d4a58 | 2015-01-09 15:56:18 +0800 | [diff] [blame] | 2737 | |
Yan, Zheng | c4d4a58 | 2015-01-09 15:56:18 +0800 | [diff] [blame] | 2738 | _got = 0; |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2739 | ret = try_get_cap_refs(ci, need, want, endoff, |
Jeff Layton | 1199d7d | 2019-04-02 15:58:05 -0400 | [diff] [blame] | 2740 | false, &_got); |
| 2741 | if (ret == -EAGAIN) { |
| 2742 | continue; |
| 2743 | } else if (!ret) { |
| 2744 | int err; |
| 2745 | |
Nikolay Borisov | 5c341ee3 | 2016-10-11 12:04:09 +0300 | [diff] [blame] | 2746 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
| 2747 | add_wait_queue(&ci->i_cap_wq, &wait); |
| 2748 | |
Jeff Layton | 1199d7d | 2019-04-02 15:58:05 -0400 | [diff] [blame] | 2749 | while (!(err = try_get_cap_refs(ci, need, want, endoff, |
| 2750 | true, &_got))) { |
Yan, Zheng | 6e09d0f | 2016-12-22 16:05:43 +0800 | [diff] [blame] | 2751 | if (signal_pending(current)) { |
| 2752 | ret = -ERESTARTSYS; |
| 2753 | break; |
| 2754 | } |
Nikolay Borisov | 5c341ee3 | 2016-10-11 12:04:09 +0300 | [diff] [blame] | 2755 | wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
Yan, Zheng | 6e09d0f | 2016-12-22 16:05:43 +0800 | [diff] [blame] | 2756 | } |
Nikolay Borisov | 5c341ee3 | 2016-10-11 12:04:09 +0300 | [diff] [blame] | 2757 | |
| 2758 | remove_wait_queue(&ci->i_cap_wq, &wait); |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2759 | if (err == -EAGAIN) |
| 2760 | continue; |
Yan, Zheng | 7731032 | 2016-04-08 15:27:16 +0800 | [diff] [blame] | 2761 | } |
Jeff Layton | 1199d7d | 2019-04-02 15:58:05 -0400 | [diff] [blame] | 2762 | if (ret == -ESTALE) { |
| 2763 | /* session was killed, try renew caps */ |
| 2764 | ret = ceph_renew_caps(&ci->vfs_inode); |
| 2765 | if (ret == 0) |
| 2766 | continue; |
Yan, Zheng | 7731032 | 2016-04-08 15:27:16 +0800 | [diff] [blame] | 2767 | return ret; |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2768 | } |
Yan, Zheng | c4d4a58 | 2015-01-09 15:56:18 +0800 | [diff] [blame] | 2769 | |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2770 | if (ci->i_inline_version != CEPH_INLINE_NONE && |
| 2771 | (_got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) && |
| 2772 | i_size_read(&ci->vfs_inode) > 0) { |
| 2773 | struct page *page = |
| 2774 | find_get_page(ci->vfs_inode.i_mapping, 0); |
| 2775 | if (page) { |
| 2776 | if (PageUptodate(page)) { |
| 2777 | *pinned_page = page; |
| 2778 | break; |
| 2779 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2780 | put_page(page); |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2781 | } |
| 2782 | /* |
| 2783 | * drop cap refs first because getattr while |
| 2784 | * holding * caps refs can cause deadlock. |
| 2785 | */ |
| 2786 | ceph_put_cap_refs(ci, _got); |
| 2787 | _got = 0; |
| 2788 | |
| 2789 | /* |
| 2790 | * getattr request will bring inline data into |
| 2791 | * page cache |
| 2792 | */ |
| 2793 | ret = __ceph_do_getattr(&ci->vfs_inode, NULL, |
| 2794 | CEPH_STAT_CAP_INLINE_DATA, |
| 2795 | true); |
| 2796 | if (ret < 0) |
| 2797 | return ret; |
| 2798 | continue; |
| 2799 | } |
| 2800 | break; |
Yan, Zheng | c4d4a58 | 2015-01-09 15:56:18 +0800 | [diff] [blame] | 2801 | } |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2802 | |
Yan, Zheng | f7f7e7a | 2016-05-18 20:31:55 +0800 | [diff] [blame] | 2803 | if ((_got & CEPH_CAP_FILE_RD) && (_got & CEPH_CAP_FILE_CACHE)) |
| 2804 | ceph_fscache_revalidate_cookie(ci); |
| 2805 | |
Yan, Zheng | c4d4a58 | 2015-01-09 15:56:18 +0800 | [diff] [blame] | 2806 | *got = _got; |
| 2807 | return 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2808 | } |
| 2809 | |
| 2810 | /* |
| 2811 | * Take cap refs. Caller must already know we hold at least one ref |
| 2812 | * on the caps in question or we don't know this is safe. |
| 2813 | */ |
| 2814 | void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps) |
| 2815 | { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2816 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2817 | __take_cap_refs(ci, caps, false); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2818 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2819 | } |
| 2820 | |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 2821 | |
| 2822 | /* |
| 2823 | * drop cap_snap that is not associated with any snapshot. |
| 2824 | * we don't need to send FLUSHSNAP message for it. |
| 2825 | */ |
Yan, Zheng | 70220ac | 2016-07-06 16:21:30 +0800 | [diff] [blame] | 2826 | static int ceph_try_drop_cap_snap(struct ceph_inode_info *ci, |
| 2827 | struct ceph_cap_snap *capsnap) |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 2828 | { |
| 2829 | if (!capsnap->need_flush && |
| 2830 | !capsnap->writing && !capsnap->dirty_pages) { |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 2831 | dout("dropping cap_snap %p follows %llu\n", |
| 2832 | capsnap, capsnap->follows); |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 2833 | BUG_ON(capsnap->cap_flush.tid > 0); |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 2834 | ceph_put_snap_context(capsnap->context); |
Yan, Zheng | 70220ac | 2016-07-06 16:21:30 +0800 | [diff] [blame] | 2835 | if (!list_is_last(&capsnap->ci_item, &ci->i_cap_snaps)) |
| 2836 | ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS; |
| 2837 | |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 2838 | list_del(&capsnap->ci_item); |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 2839 | ceph_put_cap_snap(capsnap); |
| 2840 | return 1; |
| 2841 | } |
| 2842 | return 0; |
| 2843 | } |
| 2844 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2845 | /* |
| 2846 | * Release cap refs. |
| 2847 | * |
| 2848 | * If we released the last ref on any given cap, call ceph_check_caps |
| 2849 | * to release (or schedule a release). |
| 2850 | * |
| 2851 | * If we are releasing a WR cap (from a sync write), finalize any affected |
| 2852 | * cap_snap, and wake up any waiters. |
| 2853 | */ |
| 2854 | void ceph_put_cap_refs(struct ceph_inode_info *ci, int had) |
| 2855 | { |
| 2856 | struct inode *inode = &ci->vfs_inode; |
| 2857 | int last = 0, put = 0, flushsnaps = 0, wake = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2858 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2859 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2860 | if (had & CEPH_CAP_PIN) |
| 2861 | --ci->i_pin_ref; |
| 2862 | if (had & CEPH_CAP_FILE_RD) |
| 2863 | if (--ci->i_rd_ref == 0) |
| 2864 | last++; |
| 2865 | if (had & CEPH_CAP_FILE_CACHE) |
| 2866 | if (--ci->i_rdcache_ref == 0) |
| 2867 | last++; |
| 2868 | if (had & CEPH_CAP_FILE_BUFFER) { |
Henry C Chang | d3d0720 | 2011-05-11 10:29:54 +0000 | [diff] [blame] | 2869 | if (--ci->i_wb_ref == 0) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2870 | last++; |
| 2871 | put++; |
| 2872 | } |
Henry C Chang | d3d0720 | 2011-05-11 10:29:54 +0000 | [diff] [blame] | 2873 | dout("put_cap_refs %p wb %d -> %d (?)\n", |
| 2874 | inode, ci->i_wb_ref+1, ci->i_wb_ref); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2875 | } |
| 2876 | if (had & CEPH_CAP_FILE_WR) |
| 2877 | if (--ci->i_wr_ref == 0) { |
| 2878 | last++; |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 2879 | if (__ceph_have_pending_cap_snap(ci)) { |
| 2880 | struct ceph_cap_snap *capsnap = |
| 2881 | list_last_entry(&ci->i_cap_snaps, |
| 2882 | struct ceph_cap_snap, |
| 2883 | ci_item); |
| 2884 | capsnap->writing = 0; |
Yan, Zheng | 70220ac | 2016-07-06 16:21:30 +0800 | [diff] [blame] | 2885 | if (ceph_try_drop_cap_snap(ci, capsnap)) |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 2886 | put++; |
| 2887 | else if (__ceph_finish_cap_snap(ci, capsnap)) |
| 2888 | flushsnaps = 1; |
| 2889 | wake = 1; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2890 | } |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2891 | if (ci->i_wrbuffer_ref_head == 0 && |
| 2892 | ci->i_dirty_caps == 0 && |
| 2893 | ci->i_flushing_caps == 0) { |
| 2894 | BUG_ON(!ci->i_head_snapc); |
| 2895 | ceph_put_snap_context(ci->i_head_snapc); |
| 2896 | ci->i_head_snapc = NULL; |
| 2897 | } |
Yan, Zheng | db40cc1 | 2015-03-23 20:12:20 +0800 | [diff] [blame] | 2898 | /* see comment in __ceph_remove_cap() */ |
| 2899 | if (!__ceph_is_any_caps(ci) && ci->i_snap_realm) |
| 2900 | drop_inode_snap_realm(ci); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2901 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2902 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2903 | |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 2904 | dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had), |
| 2905 | last ? " last" : "", put ? " put" : ""); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2906 | |
| 2907 | if (last && !flushsnaps) |
| 2908 | ceph_check_caps(ci, 0, NULL); |
| 2909 | else if (flushsnaps) |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 2910 | ceph_flush_snaps(ci, NULL); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2911 | if (wake) |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 2912 | wake_up_all(&ci->i_cap_wq); |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 2913 | while (put-- > 0) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2914 | iput(inode); |
| 2915 | } |
| 2916 | |
| 2917 | /* |
| 2918 | * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap |
| 2919 | * context. Adjust per-snap dirty page accounting as appropriate. |
| 2920 | * Once all dirty data for a cap_snap is flushed, flush snapped file |
| 2921 | * metadata back to the MDS. If we dropped the last ref, call |
| 2922 | * ceph_check_caps. |
| 2923 | */ |
| 2924 | void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr, |
| 2925 | struct ceph_snap_context *snapc) |
| 2926 | { |
| 2927 | struct inode *inode = &ci->vfs_inode; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2928 | struct ceph_cap_snap *capsnap = NULL; |
Yan, Zheng | 70220ac | 2016-07-06 16:21:30 +0800 | [diff] [blame] | 2929 | int put = 0; |
| 2930 | bool last = false; |
| 2931 | bool found = false; |
| 2932 | bool flush_snaps = false; |
| 2933 | bool complete_capsnap = false; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2934 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2935 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2936 | ci->i_wrbuffer_ref -= nr; |
Yan, Zheng | 70220ac | 2016-07-06 16:21:30 +0800 | [diff] [blame] | 2937 | if (ci->i_wrbuffer_ref == 0) { |
| 2938 | last = true; |
| 2939 | put++; |
| 2940 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2941 | |
| 2942 | if (ci->i_head_snapc == snapc) { |
| 2943 | ci->i_wrbuffer_ref_head -= nr; |
Sage Weil | 7d8cb26 | 2010-08-24 08:44:16 -0700 | [diff] [blame] | 2944 | if (ci->i_wrbuffer_ref_head == 0 && |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 2945 | ci->i_wr_ref == 0 && |
| 2946 | ci->i_dirty_caps == 0 && |
| 2947 | ci->i_flushing_caps == 0) { |
Sage Weil | 7d8cb26 | 2010-08-24 08:44:16 -0700 | [diff] [blame] | 2948 | BUG_ON(!ci->i_head_snapc); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2949 | ceph_put_snap_context(ci->i_head_snapc); |
| 2950 | ci->i_head_snapc = NULL; |
| 2951 | } |
| 2952 | dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n", |
| 2953 | inode, |
| 2954 | ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr, |
| 2955 | ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head, |
| 2956 | last ? " LAST" : ""); |
| 2957 | } else { |
| 2958 | list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { |
| 2959 | if (capsnap->context == snapc) { |
Yan, Zheng | 70220ac | 2016-07-06 16:21:30 +0800 | [diff] [blame] | 2960 | found = true; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2961 | break; |
| 2962 | } |
| 2963 | } |
| 2964 | BUG_ON(!found); |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 2965 | capsnap->dirty_pages -= nr; |
| 2966 | if (capsnap->dirty_pages == 0) { |
Yan, Zheng | 70220ac | 2016-07-06 16:21:30 +0800 | [diff] [blame] | 2967 | complete_capsnap = true; |
| 2968 | if (!capsnap->writing) { |
| 2969 | if (ceph_try_drop_cap_snap(ci, capsnap)) { |
| 2970 | put++; |
| 2971 | } else { |
| 2972 | ci->i_ceph_flags |= CEPH_I_FLUSH_SNAPS; |
| 2973 | flush_snaps = true; |
| 2974 | } |
| 2975 | } |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 2976 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2977 | dout("put_wrbuffer_cap_refs on %p cap_snap %p " |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 2978 | " snap %lld %d/%d -> %d/%d %s%s\n", |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2979 | inode, capsnap, capsnap->context->seq, |
| 2980 | ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr, |
| 2981 | ci->i_wrbuffer_ref, capsnap->dirty_pages, |
| 2982 | last ? " (wrbuffer last)" : "", |
Yan, Zheng | 8605609 | 2015-05-01 16:57:16 +0800 | [diff] [blame] | 2983 | complete_capsnap ? " (complete capsnap)" : ""); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2984 | } |
| 2985 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 2986 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2987 | |
| 2988 | if (last) { |
| 2989 | ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL); |
Yan, Zheng | 70220ac | 2016-07-06 16:21:30 +0800 | [diff] [blame] | 2990 | } else if (flush_snaps) { |
Yan, Zheng | ed9b430 | 2016-07-05 21:08:07 +0800 | [diff] [blame] | 2991 | ceph_flush_snaps(ci, NULL); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2992 | } |
Yan, Zheng | 70220ac | 2016-07-06 16:21:30 +0800 | [diff] [blame] | 2993 | if (complete_capsnap) |
| 2994 | wake_up_all(&ci->i_cap_wq); |
| 2995 | while (put-- > 0) |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 2996 | iput(inode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2997 | } |
| 2998 | |
| 2999 | /* |
Yan, Zheng | ca20c99 | 2013-07-21 10:07:51 +0800 | [diff] [blame] | 3000 | * Invalidate unlinked inode's aliases, so we can drop the inode ASAP. |
| 3001 | */ |
| 3002 | static void invalidate_aliases(struct inode *inode) |
| 3003 | { |
| 3004 | struct dentry *dn, *prev = NULL; |
| 3005 | |
| 3006 | dout("invalidate_aliases inode %p\n", inode); |
| 3007 | d_prune_aliases(inode); |
| 3008 | /* |
| 3009 | * For non-directory inode, d_find_alias() only returns |
J. Bruce Fields | fc12c80 | 2014-01-16 17:42:53 -0500 | [diff] [blame] | 3010 | * hashed dentry. After calling d_invalidate(), the |
| 3011 | * dentry becomes unhashed. |
Yan, Zheng | ca20c99 | 2013-07-21 10:07:51 +0800 | [diff] [blame] | 3012 | * |
Yan, Zheng | a8d436f | 2013-09-02 15:19:54 +0800 | [diff] [blame] | 3013 | * For directory inode, d_find_alias() can return |
J. Bruce Fields | fc12c80 | 2014-01-16 17:42:53 -0500 | [diff] [blame] | 3014 | * unhashed dentry. But directory inode should have |
Yan, Zheng | ca20c99 | 2013-07-21 10:07:51 +0800 | [diff] [blame] | 3015 | * one alias at most. |
| 3016 | */ |
| 3017 | while ((dn = d_find_alias(inode))) { |
| 3018 | if (dn == prev) { |
| 3019 | dput(dn); |
| 3020 | break; |
| 3021 | } |
Yan, Zheng | a8d436f | 2013-09-02 15:19:54 +0800 | [diff] [blame] | 3022 | d_invalidate(dn); |
Yan, Zheng | ca20c99 | 2013-07-21 10:07:51 +0800 | [diff] [blame] | 3023 | if (prev) |
| 3024 | dput(prev); |
| 3025 | prev = dn; |
| 3026 | } |
| 3027 | if (prev) |
| 3028 | dput(prev); |
| 3029 | } |
| 3030 | |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3031 | struct cap_extra_info { |
| 3032 | struct ceph_string *pool_ns; |
| 3033 | /* inline data */ |
| 3034 | u64 inline_version; |
| 3035 | void *inline_data; |
| 3036 | u32 inline_len; |
Yan, Zheng | 4985d6f | 2018-04-27 11:11:31 +0800 | [diff] [blame] | 3037 | /* dirstat */ |
| 3038 | bool dirstat_valid; |
| 3039 | u64 nfiles; |
| 3040 | u64 nsubdirs; |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3041 | /* currently issued */ |
| 3042 | int issued; |
| 3043 | }; |
| 3044 | |
Yan, Zheng | ca20c99 | 2013-07-21 10:07:51 +0800 | [diff] [blame] | 3045 | /* |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3046 | * Handle a cap GRANT message from the MDS. (Note that a GRANT may |
| 3047 | * actually be a revocation if it specifies a smaller cap set.) |
| 3048 | * |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3049 | * caller holds s_mutex and i_ceph_lock, we drop both. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3050 | */ |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3051 | static void handle_cap_grant(struct inode *inode, |
Sage Weil | 15637c8 | 2010-03-16 13:42:00 -0700 | [diff] [blame] | 3052 | struct ceph_mds_session *session, |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3053 | struct ceph_cap *cap, |
| 3054 | struct ceph_mds_caps *grant, |
| 3055 | struct ceph_buffer *xattr_buf, |
| 3056 | struct cap_extra_info *extra_info) |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3057 | __releases(ci->i_ceph_lock) |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3058 | __releases(session->s_mdsc->snap_rwsem) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3059 | { |
| 3060 | struct ceph_inode_info *ci = ceph_inode(inode); |
Sage Weil | 2f56f56 | 2010-10-27 20:59:49 -0700 | [diff] [blame] | 3061 | int seq = le32_to_cpu(grant->seq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3062 | int newcaps = le32_to_cpu(grant->caps); |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3063 | int used, wanted, dirty; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3064 | u64 size = le64_to_cpu(grant->size); |
| 3065 | u64 max_size = le64_to_cpu(grant->max_size); |
Yan, Zheng | fdac94f | 2018-11-22 15:26:01 +0800 | [diff] [blame] | 3066 | unsigned char check_caps = 0; |
| 3067 | bool was_stale = cap->cap_gen < session->s_cap_gen; |
Fabian Frederick | ab6c2c3 | 2014-10-09 23:16:35 +0200 | [diff] [blame] | 3068 | bool wake = false; |
| 3069 | bool writeback = false; |
| 3070 | bool queue_trunc = false; |
| 3071 | bool queue_invalidate = false; |
Fabian Frederick | ab6c2c3 | 2014-10-09 23:16:35 +0200 | [diff] [blame] | 3072 | bool deleted_inode = false; |
Yan, Zheng | 31c542a | 2014-11-14 21:41:55 +0800 | [diff] [blame] | 3073 | bool fill_inline = false; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3074 | |
Sage Weil | 2f56f56 | 2010-10-27 20:59:49 -0700 | [diff] [blame] | 3075 | dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n", |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3076 | inode, cap, session->s_mds, seq, ceph_cap_string(newcaps)); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3077 | dout(" size %llu max_size %llu, i_size %llu\n", size, max_size, |
| 3078 | inode->i_size); |
| 3079 | |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3080 | |
| 3081 | /* |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3082 | * If CACHE is being revoked, and we have no dirty buffers, |
| 3083 | * try to invalidate (once). (If there are dirty buffers, we |
| 3084 | * will invalidate _after_ writeback.) |
| 3085 | */ |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 3086 | if (!S_ISDIR(inode->i_mode) && /* don't invalidate readdir cache */ |
| 3087 | ((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) && |
Sage Weil | 3b454c4 | 2010-06-10 13:20:33 -0700 | [diff] [blame] | 3088 | (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 && |
Yan, Zheng | 9abd4db | 2016-05-18 20:58:26 +0800 | [diff] [blame] | 3089 | !(ci->i_wrbuffer_ref || ci->i_wb_ref)) { |
Li Wang | e907574 | 2013-08-15 22:00:25 -0700 | [diff] [blame] | 3090 | if (try_nonblocking_invalidate(inode)) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3091 | /* there were locked pages.. invalidate later |
| 3092 | in a separate thread. */ |
| 3093 | if (ci->i_rdcache_revoking != ci->i_rdcache_gen) { |
Fabian Frederick | ab6c2c3 | 2014-10-09 23:16:35 +0200 | [diff] [blame] | 3094 | queue_invalidate = true; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3095 | ci->i_rdcache_revoking = ci->i_rdcache_gen; |
| 3096 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3097 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3098 | } |
| 3099 | |
Yan, Zheng | d2f8bb2 | 2018-12-10 16:35:09 +0800 | [diff] [blame] | 3100 | if (was_stale) |
| 3101 | cap->issued = cap->implemented = CEPH_CAP_PIN; |
| 3102 | |
| 3103 | /* |
| 3104 | * auth mds of the inode changed. we received the cap export message, |
| 3105 | * but still haven't received the cap import message. handle_cap_export |
| 3106 | * updated the new auth MDS' cap. |
| 3107 | * |
| 3108 | * "ceph_seq_cmp(seq, cap->seq) <= 0" means we are processing a message |
| 3109 | * that was sent before the cap import message. So don't remove caps. |
| 3110 | */ |
| 3111 | if (ceph_seq_cmp(seq, cap->seq) <= 0) { |
| 3112 | WARN_ON(cap != ci->i_auth_cap); |
| 3113 | WARN_ON(cap->cap_id != le64_to_cpu(grant->cap_id)); |
| 3114 | seq = cap->seq; |
| 3115 | newcaps |= cap->issued; |
| 3116 | } |
| 3117 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3118 | /* side effects now are allowed */ |
Sage Weil | 685f9a5d | 2009-11-09 12:05:48 -0800 | [diff] [blame] | 3119 | cap->cap_gen = session->s_cap_gen; |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3120 | cap->seq = seq; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3121 | |
| 3122 | __check_cap_issue(ci, cap, newcaps); |
| 3123 | |
Yan, Zheng | f98a128 | 2014-04-17 08:55:50 +0800 | [diff] [blame] | 3124 | if ((newcaps & CEPH_CAP_AUTH_SHARED) && |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3125 | (extra_info->issued & CEPH_CAP_AUTH_EXCL) == 0) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3126 | inode->i_mode = le32_to_cpu(grant->mode); |
Eric W. Biederman | 05cb11c | 2013-01-31 02:56:19 -0800 | [diff] [blame] | 3127 | inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid)); |
| 3128 | inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid)); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3129 | dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode, |
Eric W. Biederman | bd2bae6 | 2013-01-31 04:05:39 -0800 | [diff] [blame] | 3130 | from_kuid(&init_user_ns, inode->i_uid), |
| 3131 | from_kgid(&init_user_ns, inode->i_gid)); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3132 | } |
| 3133 | |
Yan, Zheng | fa46674 | 2018-05-25 11:22:56 +0800 | [diff] [blame] | 3134 | if ((newcaps & CEPH_CAP_LINK_SHARED) && |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3135 | (extra_info->issued & CEPH_CAP_LINK_EXCL) == 0) { |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3136 | set_nlink(inode, le32_to_cpu(grant->nlink)); |
Yan, Zheng | ca20c99 | 2013-07-21 10:07:51 +0800 | [diff] [blame] | 3137 | if (inode->i_nlink == 0 && |
| 3138 | (newcaps & (CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL))) |
Fabian Frederick | ab6c2c3 | 2014-10-09 23:16:35 +0200 | [diff] [blame] | 3139 | deleted_inode = true; |
Yan, Zheng | ca20c99 | 2013-07-21 10:07:51 +0800 | [diff] [blame] | 3140 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3141 | |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3142 | if ((extra_info->issued & CEPH_CAP_XATTR_EXCL) == 0 && |
| 3143 | grant->xattr_len) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3144 | int len = le32_to_cpu(grant->xattr_len); |
| 3145 | u64 version = le64_to_cpu(grant->xattr_version); |
| 3146 | |
| 3147 | if (version > ci->i_xattrs.version) { |
| 3148 | dout(" got new xattrs v%llu on %p len %d\n", |
| 3149 | version, inode, len); |
| 3150 | if (ci->i_xattrs.blob) |
| 3151 | ceph_buffer_put(ci->i_xattrs.blob); |
| 3152 | ci->i_xattrs.blob = ceph_buffer_get(xattr_buf); |
| 3153 | ci->i_xattrs.version = version; |
Guangliang Zhao | 7221fe4 | 2013-11-11 15:18:03 +0800 | [diff] [blame] | 3154 | ceph_forget_all_cached_acls(inode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3155 | } |
| 3156 | } |
| 3157 | |
Yan, Zheng | f98a128 | 2014-04-17 08:55:50 +0800 | [diff] [blame] | 3158 | if (newcaps & CEPH_CAP_ANY_RD) { |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 3159 | struct timespec64 mtime, atime, ctime; |
Yan, Zheng | f98a128 | 2014-04-17 08:55:50 +0800 | [diff] [blame] | 3160 | /* ctime/mtime/atime? */ |
Arnd Bergmann | 9bbeab4 | 2018-07-13 22:18:36 +0200 | [diff] [blame] | 3161 | ceph_decode_timespec64(&mtime, &grant->mtime); |
| 3162 | ceph_decode_timespec64(&atime, &grant->atime); |
| 3163 | ceph_decode_timespec64(&ctime, &grant->ctime); |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3164 | ceph_fill_file_time(inode, extra_info->issued, |
Yan, Zheng | f98a128 | 2014-04-17 08:55:50 +0800 | [diff] [blame] | 3165 | le32_to_cpu(grant->time_warp_seq), |
| 3166 | &ctime, &mtime, &atime); |
| 3167 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3168 | |
Yan, Zheng | 4985d6f | 2018-04-27 11:11:31 +0800 | [diff] [blame] | 3169 | if ((newcaps & CEPH_CAP_FILE_SHARED) && extra_info->dirstat_valid) { |
| 3170 | ci->i_files = extra_info->nfiles; |
| 3171 | ci->i_subdirs = extra_info->nsubdirs; |
| 3172 | } |
| 3173 | |
Yan, Zheng | f98a128 | 2014-04-17 08:55:50 +0800 | [diff] [blame] | 3174 | if (newcaps & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR)) { |
| 3175 | /* file layout may have changed */ |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 3176 | s64 old_pool = ci->i_layout.pool_id; |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 3177 | struct ceph_string *old_ns; |
| 3178 | |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 3179 | ceph_file_layout_from_legacy(&ci->i_layout, &grant->layout); |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 3180 | old_ns = rcu_dereference_protected(ci->i_layout.pool_ns, |
| 3181 | lockdep_is_held(&ci->i_ceph_lock)); |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3182 | rcu_assign_pointer(ci->i_layout.pool_ns, extra_info->pool_ns); |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 3183 | |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3184 | if (ci->i_layout.pool_id != old_pool || |
| 3185 | extra_info->pool_ns != old_ns) |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 3186 | ci->i_ceph_flags &= ~CEPH_I_POOL_PERM; |
Yan, Zheng | 5ea5c5e | 2016-02-14 18:06:41 +0800 | [diff] [blame] | 3187 | |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3188 | extra_info->pool_ns = old_ns; |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 3189 | |
Yan, Zheng | f98a128 | 2014-04-17 08:55:50 +0800 | [diff] [blame] | 3190 | /* size/truncate_seq? */ |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3191 | queue_trunc = ceph_fill_file_size(inode, extra_info->issued, |
Yan, Zheng | f98a128 | 2014-04-17 08:55:50 +0800 | [diff] [blame] | 3192 | le32_to_cpu(grant->truncate_seq), |
| 3193 | le64_to_cpu(grant->truncate_size), |
| 3194 | size); |
Yan, Zheng | 84eea8c | 2017-05-16 08:55:34 +0800 | [diff] [blame] | 3195 | } |
| 3196 | |
| 3197 | if (ci->i_auth_cap == cap && (newcaps & CEPH_CAP_ANY_FILE_WR)) { |
| 3198 | if (max_size != ci->i_max_size) { |
Yan, Zheng | f98a128 | 2014-04-17 08:55:50 +0800 | [diff] [blame] | 3199 | dout("max_size %lld -> %llu\n", |
| 3200 | ci->i_max_size, max_size); |
| 3201 | ci->i_max_size = max_size; |
| 3202 | if (max_size >= ci->i_wanted_max_size) { |
| 3203 | ci->i_wanted_max_size = 0; /* reset */ |
| 3204 | ci->i_requested_max_size = 0; |
| 3205 | } |
Fabian Frederick | ab6c2c3 | 2014-10-09 23:16:35 +0200 | [diff] [blame] | 3206 | wake = true; |
Yan, Zheng | 84eea8c | 2017-05-16 08:55:34 +0800 | [diff] [blame] | 3207 | } else if (ci->i_wanted_max_size > ci->i_max_size && |
| 3208 | ci->i_wanted_max_size > ci->i_requested_max_size) { |
| 3209 | /* CEPH_CAP_OP_IMPORT */ |
| 3210 | wake = true; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3211 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3212 | } |
| 3213 | |
| 3214 | /* check cap bits */ |
| 3215 | wanted = __ceph_caps_wanted(ci); |
| 3216 | used = __ceph_caps_used(ci); |
| 3217 | dirty = __ceph_caps_dirty(ci); |
| 3218 | dout(" my wanted = %s, used = %s, dirty %s\n", |
| 3219 | ceph_cap_string(wanted), |
| 3220 | ceph_cap_string(used), |
| 3221 | ceph_cap_string(dirty)); |
Yan, Zheng | fdac94f | 2018-11-22 15:26:01 +0800 | [diff] [blame] | 3222 | |
| 3223 | if ((was_stale || le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) && |
| 3224 | (wanted & ~(cap->mds_wanted | newcaps))) { |
| 3225 | /* |
| 3226 | * If mds is importing cap, prior cap messages that update |
| 3227 | * 'wanted' may get dropped by mds (migrate seq mismatch). |
| 3228 | * |
| 3229 | * We don't send cap message to update 'wanted' if what we |
| 3230 | * want are already issued. If mds revokes caps, cap message |
| 3231 | * that releases caps also tells mds what we want. But if |
| 3232 | * caps got revoked by mds forcedly (session stale). We may |
| 3233 | * haven't told mds what we want. |
| 3234 | */ |
| 3235 | check_caps = 1; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3236 | } |
| 3237 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3238 | /* revocation, grant, or no-op? */ |
| 3239 | if (cap->issued & ~newcaps) { |
Sage Weil | 3b454c4 | 2010-06-10 13:20:33 -0700 | [diff] [blame] | 3240 | int revoking = cap->issued & ~newcaps; |
| 3241 | |
| 3242 | dout("revocation: %s -> %s (revoking %s)\n", |
| 3243 | ceph_cap_string(cap->issued), |
| 3244 | ceph_cap_string(newcaps), |
| 3245 | ceph_cap_string(revoking)); |
Sage Weil | 0eb6cd4 | 2010-08-05 13:53:18 -0700 | [diff] [blame] | 3246 | if (revoking & used & CEPH_CAP_FILE_BUFFER) |
Fabian Frederick | ab6c2c3 | 2014-10-09 23:16:35 +0200 | [diff] [blame] | 3247 | writeback = true; /* initiate writeback; will delay ack */ |
Sage Weil | 3b454c4 | 2010-06-10 13:20:33 -0700 | [diff] [blame] | 3248 | else if (revoking == CEPH_CAP_FILE_CACHE && |
| 3249 | (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 && |
| 3250 | queue_invalidate) |
| 3251 | ; /* do nothing yet, invalidation will be queued */ |
| 3252 | else if (cap == ci->i_auth_cap) |
| 3253 | check_caps = 1; /* check auth cap only */ |
| 3254 | else |
| 3255 | check_caps = 2; /* check all caps */ |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3256 | cap->issued = newcaps; |
Sage Weil | 978097c | 2010-03-08 15:27:53 -0800 | [diff] [blame] | 3257 | cap->implemented |= newcaps; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3258 | } else if (cap->issued == newcaps) { |
| 3259 | dout("caps unchanged: %s -> %s\n", |
| 3260 | ceph_cap_string(cap->issued), ceph_cap_string(newcaps)); |
| 3261 | } else { |
| 3262 | dout("grant: %s -> %s\n", ceph_cap_string(cap->issued), |
| 3263 | ceph_cap_string(newcaps)); |
Yan, Zheng | 6ee6b953 | 2013-07-02 12:40:21 +0800 | [diff] [blame] | 3264 | /* non-auth MDS is revoking the newly grant caps ? */ |
| 3265 | if (cap == ci->i_auth_cap && |
| 3266 | __ceph_caps_revoking_other(ci, cap, newcaps)) |
| 3267 | check_caps = 2; |
| 3268 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3269 | cap->issued = newcaps; |
| 3270 | cap->implemented |= newcaps; /* add bits only, to |
| 3271 | * avoid stepping on a |
| 3272 | * pending revocation */ |
Fabian Frederick | ab6c2c3 | 2014-10-09 23:16:35 +0200 | [diff] [blame] | 3273 | wake = true; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3274 | } |
Sage Weil | 978097c | 2010-03-08 15:27:53 -0800 | [diff] [blame] | 3275 | BUG_ON(cap->issued & ~cap->implemented); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3276 | |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3277 | if (extra_info->inline_version > 0 && |
| 3278 | extra_info->inline_version >= ci->i_inline_version) { |
| 3279 | ci->i_inline_version = extra_info->inline_version; |
Yan, Zheng | 31c542a | 2014-11-14 21:41:55 +0800 | [diff] [blame] | 3280 | if (ci->i_inline_version != CEPH_INLINE_NONE && |
| 3281 | (newcaps & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO))) |
| 3282 | fill_inline = true; |
| 3283 | } |
| 3284 | |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3285 | if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) { |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3286 | if (newcaps & ~extra_info->issued) |
Fabian Frederick | ab6c2c3 | 2014-10-09 23:16:35 +0200 | [diff] [blame] | 3287 | wake = true; |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3288 | kick_flushing_inode_caps(session->s_mdsc, session, inode); |
| 3289 | up_read(&session->s_mdsc->snap_rwsem); |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3290 | } else { |
| 3291 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3292 | } |
| 3293 | |
Yan, Zheng | 31c542a | 2014-11-14 21:41:55 +0800 | [diff] [blame] | 3294 | if (fill_inline) |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3295 | ceph_fill_inline_data(inode, NULL, extra_info->inline_data, |
| 3296 | extra_info->inline_len); |
Yan, Zheng | 31c542a | 2014-11-14 21:41:55 +0800 | [diff] [blame] | 3297 | |
Yan, Zheng | 1464975 | 2016-05-20 15:41:20 +0800 | [diff] [blame] | 3298 | if (queue_trunc) |
Yan, Zheng | c6bcda6 | 2014-04-11 10:18:07 +0800 | [diff] [blame] | 3299 | ceph_queue_vmtruncate(inode); |
Yan, Zheng | c6bcda6 | 2014-04-11 10:18:07 +0800 | [diff] [blame] | 3300 | |
Sage Weil | 3c6f6b7 | 2010-02-09 15:24:44 -0800 | [diff] [blame] | 3301 | if (writeback) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3302 | /* |
| 3303 | * queue inode for writeback: we can't actually call |
| 3304 | * filemap_write_and_wait, etc. from message handler |
| 3305 | * context. |
| 3306 | */ |
Sage Weil | 3c6f6b7 | 2010-02-09 15:24:44 -0800 | [diff] [blame] | 3307 | ceph_queue_writeback(inode); |
| 3308 | if (queue_invalidate) |
| 3309 | ceph_queue_invalidate(inode); |
Yan, Zheng | ca20c99 | 2013-07-21 10:07:51 +0800 | [diff] [blame] | 3310 | if (deleted_inode) |
| 3311 | invalidate_aliases(inode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3312 | if (wake) |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 3313 | wake_up_all(&ci->i_cap_wq); |
Sage Weil | 15637c8 | 2010-03-16 13:42:00 -0700 | [diff] [blame] | 3314 | |
| 3315 | if (check_caps == 1) |
| 3316 | ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY, |
| 3317 | session); |
| 3318 | else if (check_caps == 2) |
| 3319 | ceph_check_caps(ci, CHECK_CAPS_NODELAY, session); |
| 3320 | else |
| 3321 | mutex_unlock(&session->s_mutex); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3322 | } |
| 3323 | |
| 3324 | /* |
| 3325 | * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the |
| 3326 | * MDS has been safely committed. |
| 3327 | */ |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 3328 | static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3329 | struct ceph_mds_caps *m, |
| 3330 | struct ceph_mds_session *session, |
| 3331 | struct ceph_cap *cap) |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3332 | __releases(ci->i_ceph_lock) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3333 | { |
| 3334 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 3335 | struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc; |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 3336 | struct ceph_cap_flush *cf, *tmp_cf; |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 3337 | LIST_HEAD(to_remove); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3338 | unsigned seq = le32_to_cpu(m->seq); |
| 3339 | int dirty = le32_to_cpu(m->dirty); |
| 3340 | int cleaned = 0; |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 3341 | bool drop = false; |
Thomas Meyer | 7271efa | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 3342 | bool wake_ci = false; |
| 3343 | bool wake_mdsc = false; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3344 | |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 3345 | list_for_each_entry_safe(cf, tmp_cf, &ci->i_cap_flush_list, i_list) { |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 3346 | if (cf->tid == flush_tid) |
| 3347 | cleaned = cf->caps; |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3348 | if (cf->caps == 0) /* capsnap */ |
| 3349 | continue; |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 3350 | if (cf->tid <= flush_tid) { |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 3351 | if (__finish_cap_flush(NULL, ci, cf)) |
| 3352 | wake_ci = true; |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 3353 | list_add_tail(&cf->i_list, &to_remove); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 3354 | } else { |
| 3355 | cleaned &= ~cf->caps; |
| 3356 | if (!cleaned) |
| 3357 | break; |
| 3358 | } |
| 3359 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3360 | |
| 3361 | dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s," |
| 3362 | " flushing %s -> %s\n", |
| 3363 | inode, session->s_mds, seq, ceph_cap_string(dirty), |
| 3364 | ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps), |
| 3365 | ceph_cap_string(ci->i_flushing_caps & ~cleaned)); |
| 3366 | |
Yan, Zheng | 8310b08 | 2015-06-09 17:20:12 +0800 | [diff] [blame] | 3367 | if (list_empty(&to_remove) && !cleaned) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3368 | goto out; |
| 3369 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3370 | ci->i_flushing_caps &= ~cleaned; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3371 | |
| 3372 | spin_lock(&mdsc->cap_dirty_lock); |
Yan, Zheng | 8310b08 | 2015-06-09 17:20:12 +0800 | [diff] [blame] | 3373 | |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 3374 | list_for_each_entry(cf, &to_remove, i_list) { |
| 3375 | if (__finish_cap_flush(mdsc, NULL, cf)) |
| 3376 | wake_mdsc = true; |
Yan, Zheng | 8310b08 | 2015-06-09 17:20:12 +0800 | [diff] [blame] | 3377 | } |
| 3378 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3379 | if (ci->i_flushing_caps == 0) { |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3380 | if (list_empty(&ci->i_cap_flush_list)) { |
| 3381 | list_del_init(&ci->i_flushing_item); |
| 3382 | if (!list_empty(&session->s_cap_flushing)) { |
| 3383 | dout(" mds%d still flushing cap on %p\n", |
| 3384 | session->s_mds, |
| 3385 | &list_first_entry(&session->s_cap_flushing, |
| 3386 | struct ceph_inode_info, |
| 3387 | i_flushing_item)->vfs_inode); |
| 3388 | } |
| 3389 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3390 | mdsc->num_cap_flushing--; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3391 | dout(" inode %p now !flushing\n", inode); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 3392 | |
| 3393 | if (ci->i_dirty_caps == 0) { |
| 3394 | dout(" inode %p now clean\n", inode); |
| 3395 | BUG_ON(!list_empty(&ci->i_dirty_item)); |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 3396 | drop = true; |
Yan, Zheng | 5dda377c | 2015-04-30 14:40:54 +0800 | [diff] [blame] | 3397 | if (ci->i_wr_ref == 0 && |
| 3398 | ci->i_wrbuffer_ref_head == 0) { |
Sage Weil | 7d8cb26 | 2010-08-24 08:44:16 -0700 | [diff] [blame] | 3399 | BUG_ON(!ci->i_head_snapc); |
| 3400 | ceph_put_snap_context(ci->i_head_snapc); |
| 3401 | ci->i_head_snapc = NULL; |
| 3402 | } |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 3403 | } else { |
| 3404 | BUG_ON(list_empty(&ci->i_dirty_item)); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 3405 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3406 | } |
| 3407 | spin_unlock(&mdsc->cap_dirty_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3408 | |
| 3409 | out: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3410 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 3411 | |
| 3412 | while (!list_empty(&to_remove)) { |
| 3413 | cf = list_first_entry(&to_remove, |
Yan, Zheng | e4500b5 | 2016-07-06 11:12:56 +0800 | [diff] [blame] | 3414 | struct ceph_cap_flush, i_list); |
| 3415 | list_del(&cf->i_list); |
Yan, Zheng | f66fd9f | 2015-06-10 17:26:13 +0800 | [diff] [blame] | 3416 | ceph_free_cap_flush(cf); |
Yan, Zheng | 553adfd | 2015-06-09 15:48:57 +0800 | [diff] [blame] | 3417 | } |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 3418 | |
| 3419 | if (wake_ci) |
| 3420 | wake_up_all(&ci->i_cap_wq); |
| 3421 | if (wake_mdsc) |
| 3422 | wake_up_all(&mdsc->cap_flushing_wq); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 3423 | if (drop) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3424 | iput(inode); |
| 3425 | } |
| 3426 | |
| 3427 | /* |
| 3428 | * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can |
| 3429 | * throw away our cap_snap. |
| 3430 | * |
| 3431 | * Caller hold s_mutex. |
| 3432 | */ |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 3433 | static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3434 | struct ceph_mds_caps *m, |
| 3435 | struct ceph_mds_session *session) |
| 3436 | { |
| 3437 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | affbc19 | 2015-05-05 21:22:13 +0800 | [diff] [blame] | 3438 | struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3439 | u64 follows = le64_to_cpu(m->snap_follows); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3440 | struct ceph_cap_snap *capsnap; |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 3441 | bool flushed = false; |
| 3442 | bool wake_ci = false; |
| 3443 | bool wake_mdsc = false; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3444 | |
| 3445 | dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n", |
| 3446 | inode, ci, session->s_mds, follows); |
| 3447 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3448 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3449 | list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { |
| 3450 | if (capsnap->follows == follows) { |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3451 | if (capsnap->cap_flush.tid != flush_tid) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3452 | dout(" cap_snap %p follows %lld tid %lld !=" |
| 3453 | " %lld\n", capsnap, follows, |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3454 | flush_tid, capsnap->cap_flush.tid); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3455 | break; |
| 3456 | } |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 3457 | flushed = true; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3458 | break; |
| 3459 | } else { |
| 3460 | dout(" skipping cap_snap %p follows %lld\n", |
| 3461 | capsnap, capsnap->follows); |
| 3462 | } |
| 3463 | } |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3464 | if (flushed) { |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3465 | WARN_ON(capsnap->dirty_pages || capsnap->writing); |
| 3466 | dout(" removing %p cap_snap %p follows %lld\n", |
| 3467 | inode, capsnap, follows); |
| 3468 | list_del(&capsnap->ci_item); |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 3469 | if (__finish_cap_flush(NULL, ci, &capsnap->cap_flush)) |
| 3470 | wake_ci = true; |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3471 | |
| 3472 | spin_lock(&mdsc->cap_dirty_lock); |
| 3473 | |
| 3474 | if (list_empty(&ci->i_cap_flush_list)) |
| 3475 | list_del_init(&ci->i_flushing_item); |
| 3476 | |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 3477 | if (__finish_cap_flush(mdsc, NULL, &capsnap->cap_flush)) |
| 3478 | wake_mdsc = true; |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3479 | |
| 3480 | spin_unlock(&mdsc->cap_dirty_lock); |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3481 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3482 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3483 | if (flushed) { |
| 3484 | ceph_put_snap_context(capsnap->context); |
| 3485 | ceph_put_cap_snap(capsnap); |
Yan, Zheng | c8799fc | 2016-07-07 15:22:38 +0800 | [diff] [blame] | 3486 | if (wake_ci) |
| 3487 | wake_up_all(&ci->i_cap_wq); |
| 3488 | if (wake_mdsc) |
| 3489 | wake_up_all(&mdsc->cap_flushing_wq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3490 | iput(inode); |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3491 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3492 | } |
| 3493 | |
| 3494 | /* |
| 3495 | * Handle TRUNC from MDS, indicating file truncation. |
| 3496 | * |
| 3497 | * caller hold s_mutex. |
| 3498 | */ |
| 3499 | static void handle_cap_trunc(struct inode *inode, |
| 3500 | struct ceph_mds_caps *trunc, |
| 3501 | struct ceph_mds_session *session) |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3502 | __releases(ci->i_ceph_lock) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3503 | { |
| 3504 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 3505 | int mds = session->s_mds; |
| 3506 | int seq = le32_to_cpu(trunc->seq); |
| 3507 | u32 truncate_seq = le32_to_cpu(trunc->truncate_seq); |
| 3508 | u64 truncate_size = le64_to_cpu(trunc->truncate_size); |
| 3509 | u64 size = le64_to_cpu(trunc->size); |
| 3510 | int implemented = 0; |
| 3511 | int dirty = __ceph_caps_dirty(ci); |
| 3512 | int issued = __ceph_caps_issued(ceph_inode(inode), &implemented); |
| 3513 | int queue_trunc = 0; |
| 3514 | |
| 3515 | issued |= implemented | dirty; |
| 3516 | |
| 3517 | dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n", |
| 3518 | inode, mds, seq, truncate_size, truncate_seq); |
| 3519 | queue_trunc = ceph_fill_file_size(inode, issued, |
| 3520 | truncate_seq, truncate_size, size); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3521 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3522 | |
Yan, Zheng | 1464975 | 2016-05-20 15:41:20 +0800 | [diff] [blame] | 3523 | if (queue_trunc) |
Sage Weil | 3c6f6b7 | 2010-02-09 15:24:44 -0800 | [diff] [blame] | 3524 | ceph_queue_vmtruncate(inode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3525 | } |
| 3526 | |
| 3527 | /* |
| 3528 | * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a |
| 3529 | * different one. If we are the most recent migration we've seen (as |
| 3530 | * indicated by mseq), make note of the migrating cap bits for the |
| 3531 | * duration (until we see the corresponding IMPORT). |
| 3532 | * |
| 3533 | * caller holds s_mutex |
| 3534 | */ |
| 3535 | static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex, |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3536 | struct ceph_mds_cap_peer *ph, |
| 3537 | struct ceph_mds_session *session) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3538 | { |
Sage Weil | db35405 | 2011-05-24 11:46:31 -0700 | [diff] [blame] | 3539 | struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc; |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3540 | struct ceph_mds_session *tsession = NULL; |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 3541 | struct ceph_cap *cap, *tcap, *new_cap = NULL; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3542 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3543 | u64 t_cap_id; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3544 | unsigned mseq = le32_to_cpu(ex->migrate_seq); |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3545 | unsigned t_seq, t_mseq; |
| 3546 | int target, issued; |
| 3547 | int mds = session->s_mds; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3548 | |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3549 | if (ph) { |
| 3550 | t_cap_id = le64_to_cpu(ph->cap_id); |
| 3551 | t_seq = le32_to_cpu(ph->seq); |
| 3552 | t_mseq = le32_to_cpu(ph->mseq); |
| 3553 | target = le32_to_cpu(ph->mds); |
| 3554 | } else { |
| 3555 | t_cap_id = t_seq = t_mseq = 0; |
| 3556 | target = -1; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3557 | } |
| 3558 | |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3559 | dout("handle_cap_export inode %p ci %p mds%d mseq %d target %d\n", |
| 3560 | inode, ci, mds, mseq, target); |
| 3561 | retry: |
| 3562 | spin_lock(&ci->i_ceph_lock); |
| 3563 | cap = __get_cap_for_mds(ci, mds); |
Yan, Zheng | ca665e0 | 2014-04-21 15:46:37 +0800 | [diff] [blame] | 3564 | if (!cap || cap->cap_id != le64_to_cpu(ex->cap_id)) |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3565 | goto out_unlock; |
Sage Weil | 154f42c | 2010-06-21 13:45:04 -0700 | [diff] [blame] | 3566 | |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3567 | if (target < 0) { |
Yan, Zheng | d2f8bb2 | 2018-12-10 16:35:09 +0800 | [diff] [blame] | 3568 | if (cap->mds_wanted | cap->issued) |
Yan, Zheng | 7731032 | 2016-04-08 15:27:16 +0800 | [diff] [blame] | 3569 | ci->i_ceph_flags |= CEPH_I_CAP_DROPPED; |
Yan, Zheng | d2f8bb2 | 2018-12-10 16:35:09 +0800 | [diff] [blame] | 3570 | __ceph_remove_cap(cap, false); |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3571 | goto out_unlock; |
| 3572 | } |
Sage Weil | db35405 | 2011-05-24 11:46:31 -0700 | [diff] [blame] | 3573 | |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3574 | /* |
| 3575 | * now we know we haven't received the cap import message yet |
| 3576 | * because the exported cap still exist. |
| 3577 | */ |
| 3578 | |
| 3579 | issued = cap->issued; |
Yan, Zheng | d84b37f | 2018-01-03 11:16:27 +0800 | [diff] [blame] | 3580 | if (issued != cap->implemented) |
| 3581 | pr_err_ratelimited("handle_cap_export: issued != implemented: " |
| 3582 | "ino (%llx.%llx) mds%d seq %d mseq %d " |
| 3583 | "issued %s implemented %s\n", |
| 3584 | ceph_vinop(inode), mds, cap->seq, cap->mseq, |
| 3585 | ceph_cap_string(issued), |
| 3586 | ceph_cap_string(cap->implemented)); |
| 3587 | |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3588 | |
| 3589 | tcap = __get_cap_for_mds(ci, target); |
| 3590 | if (tcap) { |
| 3591 | /* already have caps from the target */ |
Yan, Zheng | fa0aa3b | 2017-08-28 15:07:42 +0800 | [diff] [blame] | 3592 | if (tcap->cap_id == t_cap_id && |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3593 | ceph_seq_cmp(tcap->seq, t_seq) < 0) { |
| 3594 | dout(" updating import cap %p mds%d\n", tcap, target); |
| 3595 | tcap->cap_id = t_cap_id; |
| 3596 | tcap->seq = t_seq - 1; |
| 3597 | tcap->issue_seq = t_seq - 1; |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3598 | tcap->issued |= issued; |
| 3599 | tcap->implemented |= issued; |
| 3600 | if (cap == ci->i_auth_cap) |
| 3601 | ci->i_auth_cap = tcap; |
Yan, Zheng | 00f06cb | 2017-01-24 10:02:32 +0800 | [diff] [blame] | 3602 | |
Yan, Zheng | 0e29438 | 2016-07-04 18:06:41 +0800 | [diff] [blame] | 3603 | if (!list_empty(&ci->i_cap_flush_list) && |
| 3604 | ci->i_auth_cap == tcap) { |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3605 | spin_lock(&mdsc->cap_dirty_lock); |
| 3606 | list_move_tail(&ci->i_flushing_item, |
| 3607 | &tcap->session->s_cap_flushing); |
| 3608 | spin_unlock(&mdsc->cap_dirty_lock); |
Sage Weil | db35405 | 2011-05-24 11:46:31 -0700 | [diff] [blame] | 3609 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3610 | } |
Yan, Zheng | a096b09 | 2013-09-22 10:15:58 +0800 | [diff] [blame] | 3611 | __ceph_remove_cap(cap, false); |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3612 | goto out_unlock; |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 3613 | } else if (tsession) { |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3614 | /* add placeholder for the export tagert */ |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 3615 | int flag = (cap == ci->i_auth_cap) ? CEPH_CAP_FLAG_AUTH : 0; |
Yan, Zheng | 00f06cb | 2017-01-24 10:02:32 +0800 | [diff] [blame] | 3616 | tcap = new_cap; |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3617 | ceph_add_cap(inode, tsession, t_cap_id, -1, issued, 0, |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 3618 | t_seq - 1, t_mseq, (u64)-1, flag, &new_cap); |
| 3619 | |
Yan, Zheng | 00f06cb | 2017-01-24 10:02:32 +0800 | [diff] [blame] | 3620 | if (!list_empty(&ci->i_cap_flush_list) && |
| 3621 | ci->i_auth_cap == tcap) { |
| 3622 | spin_lock(&mdsc->cap_dirty_lock); |
| 3623 | list_move_tail(&ci->i_flushing_item, |
| 3624 | &tcap->session->s_cap_flushing); |
| 3625 | spin_unlock(&mdsc->cap_dirty_lock); |
| 3626 | } |
| 3627 | |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 3628 | __ceph_remove_cap(cap, false); |
| 3629 | goto out_unlock; |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3630 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3631 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3632 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3633 | mutex_unlock(&session->s_mutex); |
| 3634 | |
| 3635 | /* open target session */ |
| 3636 | tsession = ceph_mdsc_open_export_target_session(mdsc, target); |
| 3637 | if (!IS_ERR(tsession)) { |
| 3638 | if (mds > target) { |
| 3639 | mutex_lock(&session->s_mutex); |
| 3640 | mutex_lock_nested(&tsession->s_mutex, |
| 3641 | SINGLE_DEPTH_NESTING); |
| 3642 | } else { |
| 3643 | mutex_lock(&tsession->s_mutex); |
| 3644 | mutex_lock_nested(&session->s_mutex, |
| 3645 | SINGLE_DEPTH_NESTING); |
| 3646 | } |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 3647 | new_cap = ceph_get_cap(mdsc, NULL); |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3648 | } else { |
| 3649 | WARN_ON(1); |
| 3650 | tsession = NULL; |
| 3651 | target = -1; |
| 3652 | } |
| 3653 | goto retry; |
| 3654 | |
| 3655 | out_unlock: |
| 3656 | spin_unlock(&ci->i_ceph_lock); |
| 3657 | mutex_unlock(&session->s_mutex); |
| 3658 | if (tsession) { |
| 3659 | mutex_unlock(&tsession->s_mutex); |
| 3660 | ceph_put_mds_session(tsession); |
| 3661 | } |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 3662 | if (new_cap) |
| 3663 | ceph_put_cap(mdsc, new_cap); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3664 | } |
| 3665 | |
| 3666 | /* |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3667 | * Handle cap IMPORT. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3668 | * |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3669 | * caller holds s_mutex. acquires i_ceph_lock |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3670 | */ |
| 3671 | static void handle_cap_import(struct ceph_mds_client *mdsc, |
| 3672 | struct inode *inode, struct ceph_mds_caps *im, |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3673 | struct ceph_mds_cap_peer *ph, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3674 | struct ceph_mds_session *session, |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3675 | struct ceph_cap **target_cap, int *old_issued) |
| 3676 | __acquires(ci->i_ceph_lock) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3677 | { |
| 3678 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3679 | struct ceph_cap *cap, *ocap, *new_cap = NULL; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3680 | int mds = session->s_mds; |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3681 | int issued; |
| 3682 | unsigned caps = le32_to_cpu(im->caps); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3683 | unsigned wanted = le32_to_cpu(im->wanted); |
| 3684 | unsigned seq = le32_to_cpu(im->seq); |
| 3685 | unsigned mseq = le32_to_cpu(im->migrate_seq); |
| 3686 | u64 realmino = le64_to_cpu(im->realm); |
| 3687 | u64 cap_id = le64_to_cpu(im->cap_id); |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3688 | u64 p_cap_id; |
| 3689 | int peer; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3690 | |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3691 | if (ph) { |
| 3692 | p_cap_id = le64_to_cpu(ph->cap_id); |
| 3693 | peer = le32_to_cpu(ph->mds); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3694 | } else { |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3695 | p_cap_id = 0; |
| 3696 | peer = -1; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3697 | } |
| 3698 | |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3699 | dout("handle_cap_import inode %p ci %p mds%d mseq %d peer %d\n", |
| 3700 | inode, ci, mds, mseq, peer); |
| 3701 | |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 3702 | retry: |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3703 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 3704 | cap = __get_cap_for_mds(ci, mds); |
| 3705 | if (!cap) { |
| 3706 | if (!new_cap) { |
| 3707 | spin_unlock(&ci->i_ceph_lock); |
| 3708 | new_cap = ceph_get_cap(mdsc, NULL); |
| 3709 | goto retry; |
| 3710 | } |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3711 | cap = new_cap; |
| 3712 | } else { |
| 3713 | if (new_cap) { |
| 3714 | ceph_put_cap(mdsc, new_cap); |
| 3715 | new_cap = NULL; |
| 3716 | } |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 3717 | } |
| 3718 | |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3719 | __ceph_caps_issued(ci, &issued); |
| 3720 | issued |= __ceph_caps_dirty(ci); |
| 3721 | |
| 3722 | ceph_add_cap(inode, session, cap_id, -1, caps, wanted, seq, mseq, |
Yan, Zheng | d9df278 | 2014-04-18 09:57:11 +0800 | [diff] [blame] | 3723 | realmino, CEPH_CAP_FLAG_AUTH, &new_cap); |
| 3724 | |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3725 | ocap = peer >= 0 ? __get_cap_for_mds(ci, peer) : NULL; |
| 3726 | if (ocap && ocap->cap_id == p_cap_id) { |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3727 | dout(" remove export cap %p mds%d flags %d\n", |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3728 | ocap, peer, ph->flags); |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3729 | if ((ph->flags & CEPH_CAP_FLAG_AUTH) && |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3730 | (ocap->seq != le32_to_cpu(ph->seq) || |
| 3731 | ocap->mseq != le32_to_cpu(ph->mseq))) { |
Yan, Zheng | d84b37f | 2018-01-03 11:16:27 +0800 | [diff] [blame] | 3732 | pr_err_ratelimited("handle_cap_import: " |
| 3733 | "mismatched seq/mseq: ino (%llx.%llx) " |
| 3734 | "mds%d seq %d mseq %d importer mds%d " |
| 3735 | "has peer seq %d mseq %d\n", |
| 3736 | ceph_vinop(inode), peer, ocap->seq, |
| 3737 | ocap->mseq, mds, le32_to_cpu(ph->seq), |
| 3738 | le32_to_cpu(ph->mseq)); |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3739 | } |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3740 | __ceph_remove_cap(ocap, (ph->flags & CEPH_CAP_FLAG_RELEASE)); |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3741 | } |
| 3742 | |
| 3743 | /* make sure we re-request max_size, if necessary */ |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3744 | ci->i_requested_max_size = 0; |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3745 | |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3746 | *old_issued = issued; |
| 3747 | *target_cap = cap; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3748 | } |
| 3749 | |
| 3750 | /* |
| 3751 | * Handle a caps message from the MDS. |
| 3752 | * |
| 3753 | * Identify the appropriate session, inode, and call the right handler |
| 3754 | * based on the cap op. |
| 3755 | */ |
| 3756 | void ceph_handle_caps(struct ceph_mds_session *session, |
| 3757 | struct ceph_msg *msg) |
| 3758 | { |
| 3759 | struct ceph_mds_client *mdsc = session->s_mdsc; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3760 | struct inode *inode; |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3761 | struct ceph_inode_info *ci; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3762 | struct ceph_cap *cap; |
| 3763 | struct ceph_mds_caps *h; |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3764 | struct ceph_mds_cap_peer *peer = NULL; |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 3765 | struct ceph_snap_realm *realm = NULL; |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3766 | int op; |
Yan, Zheng | 4985d6f | 2018-04-27 11:11:31 +0800 | [diff] [blame] | 3767 | int msg_version = le16_to_cpu(msg->hdr.version); |
Sage Weil | 3d7ded4 | 2010-06-09 16:47:10 -0700 | [diff] [blame] | 3768 | u32 seq, mseq; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3769 | struct ceph_vino vino; |
Sage Weil | 70edb55 | 2010-03-01 13:20:50 -0800 | [diff] [blame] | 3770 | void *snaptrace; |
Sage Weil | ce1fbc8 | 2010-08-02 15:09:39 -0700 | [diff] [blame] | 3771 | size_t snaptrace_len; |
Yan, Zheng | fb01d1f | 2014-11-14 21:29:55 +0800 | [diff] [blame] | 3772 | void *p, *end; |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3773 | struct cap_extra_info extra_info = {}; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3774 | |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3775 | dout("handle_caps from mds%d\n", session->s_mds); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3776 | |
| 3777 | /* decode */ |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3778 | end = msg->front.iov_base + msg->front.iov_len; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3779 | if (msg->front.iov_len < sizeof(*h)) |
| 3780 | goto bad; |
| 3781 | h = msg->front.iov_base; |
| 3782 | op = le32_to_cpu(h->op); |
| 3783 | vino.ino = le64_to_cpu(h->ino); |
| 3784 | vino.snap = CEPH_NOSNAP; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3785 | seq = le32_to_cpu(h->seq); |
Sage Weil | 3d7ded4 | 2010-06-09 16:47:10 -0700 | [diff] [blame] | 3786 | mseq = le32_to_cpu(h->migrate_seq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3787 | |
Sage Weil | ce1fbc8 | 2010-08-02 15:09:39 -0700 | [diff] [blame] | 3788 | snaptrace = h + 1; |
| 3789 | snaptrace_len = le32_to_cpu(h->snap_trace_len); |
Yan, Zheng | fb01d1f | 2014-11-14 21:29:55 +0800 | [diff] [blame] | 3790 | p = snaptrace + snaptrace_len; |
Sage Weil | ce1fbc8 | 2010-08-02 15:09:39 -0700 | [diff] [blame] | 3791 | |
Yan, Zheng | 4985d6f | 2018-04-27 11:11:31 +0800 | [diff] [blame] | 3792 | if (msg_version >= 2) { |
Yan, Zheng | fb01d1f | 2014-11-14 21:29:55 +0800 | [diff] [blame] | 3793 | u32 flock_len; |
Sage Weil | ce1fbc8 | 2010-08-02 15:09:39 -0700 | [diff] [blame] | 3794 | ceph_decode_32_safe(&p, end, flock_len, bad); |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3795 | if (p + flock_len > end) |
| 3796 | goto bad; |
Yan, Zheng | fb01d1f | 2014-11-14 21:29:55 +0800 | [diff] [blame] | 3797 | p += flock_len; |
Sage Weil | ce1fbc8 | 2010-08-02 15:09:39 -0700 | [diff] [blame] | 3798 | } |
| 3799 | |
Yan, Zheng | 4985d6f | 2018-04-27 11:11:31 +0800 | [diff] [blame] | 3800 | if (msg_version >= 3) { |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3801 | if (op == CEPH_CAP_OP_IMPORT) { |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3802 | if (p + sizeof(*peer) > end) |
| 3803 | goto bad; |
| 3804 | peer = p; |
Yan, Zheng | fb01d1f | 2014-11-14 21:29:55 +0800 | [diff] [blame] | 3805 | p += sizeof(*peer); |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3806 | } else if (op == CEPH_CAP_OP_EXPORT) { |
| 3807 | /* recorded in unused fields */ |
| 3808 | peer = (void *)&h->size; |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3809 | } |
| 3810 | } |
| 3811 | |
Yan, Zheng | 4985d6f | 2018-04-27 11:11:31 +0800 | [diff] [blame] | 3812 | if (msg_version >= 4) { |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3813 | ceph_decode_64_safe(&p, end, extra_info.inline_version, bad); |
| 3814 | ceph_decode_32_safe(&p, end, extra_info.inline_len, bad); |
| 3815 | if (p + extra_info.inline_len > end) |
Yan, Zheng | fb01d1f | 2014-11-14 21:29:55 +0800 | [diff] [blame] | 3816 | goto bad; |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3817 | extra_info.inline_data = p; |
| 3818 | p += extra_info.inline_len; |
Yan, Zheng | fb01d1f | 2014-11-14 21:29:55 +0800 | [diff] [blame] | 3819 | } |
| 3820 | |
Yan, Zheng | 4985d6f | 2018-04-27 11:11:31 +0800 | [diff] [blame] | 3821 | if (msg_version >= 5) { |
Jeff Layton | 92475f0 | 2017-04-13 11:07:04 -0400 | [diff] [blame] | 3822 | struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc; |
| 3823 | u32 epoch_barrier; |
| 3824 | |
| 3825 | ceph_decode_32_safe(&p, end, epoch_barrier, bad); |
| 3826 | ceph_osdc_update_epoch_barrier(osdc, epoch_barrier); |
| 3827 | } |
| 3828 | |
Yan, Zheng | 4985d6f | 2018-04-27 11:11:31 +0800 | [diff] [blame] | 3829 | if (msg_version >= 8) { |
Yan, Zheng | 5ea5c5e | 2016-02-14 18:06:41 +0800 | [diff] [blame] | 3830 | u64 flush_tid; |
| 3831 | u32 caller_uid, caller_gid; |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 3832 | u32 pool_ns_len; |
Jeff Layton | 92475f0 | 2017-04-13 11:07:04 -0400 | [diff] [blame] | 3833 | |
Yan, Zheng | 5ea5c5e | 2016-02-14 18:06:41 +0800 | [diff] [blame] | 3834 | /* version >= 6 */ |
| 3835 | ceph_decode_64_safe(&p, end, flush_tid, bad); |
| 3836 | /* version >= 7 */ |
| 3837 | ceph_decode_32_safe(&p, end, caller_uid, bad); |
| 3838 | ceph_decode_32_safe(&p, end, caller_gid, bad); |
| 3839 | /* version >= 8 */ |
| 3840 | ceph_decode_32_safe(&p, end, pool_ns_len, bad); |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 3841 | if (pool_ns_len > 0) { |
| 3842 | ceph_decode_need(&p, end, pool_ns_len, bad); |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3843 | extra_info.pool_ns = |
| 3844 | ceph_find_or_create_string(p, pool_ns_len); |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 3845 | p += pool_ns_len; |
| 3846 | } |
Yan, Zheng | 5ea5c5e | 2016-02-14 18:06:41 +0800 | [diff] [blame] | 3847 | } |
| 3848 | |
Yan, Zheng | 4985d6f | 2018-04-27 11:11:31 +0800 | [diff] [blame] | 3849 | if (msg_version >= 11) { |
| 3850 | struct ceph_timespec *btime; |
| 3851 | u64 change_attr; |
| 3852 | u32 flags; |
| 3853 | |
| 3854 | /* version >= 9 */ |
| 3855 | if (p + sizeof(*btime) > end) |
| 3856 | goto bad; |
| 3857 | btime = p; |
| 3858 | p += sizeof(*btime); |
| 3859 | ceph_decode_64_safe(&p, end, change_attr, bad); |
| 3860 | /* version >= 10 */ |
| 3861 | ceph_decode_32_safe(&p, end, flags, bad); |
| 3862 | /* version >= 11 */ |
| 3863 | extra_info.dirstat_valid = true; |
| 3864 | ceph_decode_64_safe(&p, end, extra_info.nfiles, bad); |
| 3865 | ceph_decode_64_safe(&p, end, extra_info.nsubdirs, bad); |
| 3866 | } |
| 3867 | |
Yan, Zheng | 6cd3bca | 2014-09-17 07:45:12 +0800 | [diff] [blame] | 3868 | /* lookup ino */ |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3869 | inode = ceph_find_inode(mdsc->fsc->sb, vino); |
Yan, Zheng | 6cd3bca | 2014-09-17 07:45:12 +0800 | [diff] [blame] | 3870 | ci = ceph_inode(inode); |
| 3871 | dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino, |
| 3872 | vino.snap, inode); |
| 3873 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3874 | mutex_lock(&session->s_mutex); |
| 3875 | session->s_seq++; |
| 3876 | dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq, |
| 3877 | (unsigned)seq); |
| 3878 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3879 | if (!inode) { |
| 3880 | dout(" i don't have ino %llx\n", vino.ino); |
Sage Weil | 3d7ded4 | 2010-06-09 16:47:10 -0700 | [diff] [blame] | 3881 | |
Yan, Zheng | a096b09 | 2013-09-22 10:15:58 +0800 | [diff] [blame] | 3882 | if (op == CEPH_CAP_OP_IMPORT) { |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 3883 | cap = ceph_get_cap(mdsc, NULL); |
| 3884 | cap->cap_ino = vino.ino; |
| 3885 | cap->queue_release = 1; |
Yan, Zheng | 779fe0f | 2016-03-07 09:35:06 +0800 | [diff] [blame] | 3886 | cap->cap_id = le64_to_cpu(h->cap_id); |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 3887 | cap->mseq = mseq; |
| 3888 | cap->seq = seq; |
Yan, Zheng | dc24de8 | 2016-11-17 19:55:30 +0800 | [diff] [blame] | 3889 | cap->issue_seq = seq; |
Yan, Zheng | a096b09 | 2013-09-22 10:15:58 +0800 | [diff] [blame] | 3890 | spin_lock(&session->s_cap_lock); |
Yan, Zheng | e3ec8d6 | 2019-01-14 17:21:19 +0800 | [diff] [blame] | 3891 | __ceph_queue_cap_release(session, cap); |
Yan, Zheng | a096b09 | 2013-09-22 10:15:58 +0800 | [diff] [blame] | 3892 | spin_unlock(&session->s_cap_lock); |
| 3893 | } |
Yan, Zheng | e3ec8d6 | 2019-01-14 17:21:19 +0800 | [diff] [blame] | 3894 | goto done; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3895 | } |
| 3896 | |
| 3897 | /* these will work even if we don't have a cap yet */ |
| 3898 | switch (op) { |
| 3899 | case CEPH_CAP_OP_FLUSHSNAP_ACK: |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3900 | handle_cap_flushsnap_ack(inode, le64_to_cpu(msg->hdr.tid), |
| 3901 | h, session); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3902 | goto done; |
| 3903 | |
| 3904 | case CEPH_CAP_OP_EXPORT: |
Yan, Zheng | 11df2df | 2013-11-24 14:44:38 +0800 | [diff] [blame] | 3905 | handle_cap_export(inode, h, peer, session); |
| 3906 | goto done_unlocked; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3907 | |
| 3908 | case CEPH_CAP_OP_IMPORT: |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 3909 | realm = NULL; |
| 3910 | if (snaptrace_len) { |
| 3911 | down_write(&mdsc->snap_rwsem); |
| 3912 | ceph_update_snap_trace(mdsc, snaptrace, |
| 3913 | snaptrace + snaptrace_len, |
| 3914 | false, &realm); |
| 3915 | downgrade_write(&mdsc->snap_rwsem); |
| 3916 | } else { |
| 3917 | down_read(&mdsc->snap_rwsem); |
| 3918 | } |
Yan, Zheng | 4ee6a91 | 2013-11-24 14:43:46 +0800 | [diff] [blame] | 3919 | handle_cap_import(mdsc, inode, h, peer, session, |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3920 | &cap, &extra_info.issued); |
| 3921 | handle_cap_grant(inode, session, cap, |
| 3922 | h, msg->middle, &extra_info); |
Yan, Zheng | 982d601 | 2014-12-23 15:30:54 +0800 | [diff] [blame] | 3923 | if (realm) |
| 3924 | ceph_put_snap_realm(mdsc, realm); |
Yan, Zheng | 2cd698b | 2014-04-18 13:20:27 +0800 | [diff] [blame] | 3925 | goto done_unlocked; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3926 | } |
| 3927 | |
| 3928 | /* the rest require a cap */ |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3929 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3930 | cap = __get_cap_for_mds(ceph_inode(inode), session->s_mds); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3931 | if (!cap) { |
Sage Weil | 9dbd412 | 2010-06-10 13:21:20 -0700 | [diff] [blame] | 3932 | dout(" no cap on %p ino %llx.%llx from mds%d\n", |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3933 | inode, ceph_ino(inode), ceph_snap(inode), |
| 3934 | session->s_mds); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3935 | spin_unlock(&ci->i_ceph_lock); |
Greg Farnum | 21b559d | 2010-10-06 15:46:30 -0700 | [diff] [blame] | 3936 | goto flush_cap_releases; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3937 | } |
| 3938 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3939 | /* note that each of these drops i_ceph_lock for us */ |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3940 | switch (op) { |
| 3941 | case CEPH_CAP_OP_REVOKE: |
| 3942 | case CEPH_CAP_OP_GRANT: |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3943 | __ceph_caps_issued(ci, &extra_info.issued); |
| 3944 | extra_info.issued |= __ceph_caps_dirty(ci); |
| 3945 | handle_cap_grant(inode, session, cap, |
| 3946 | h, msg->middle, &extra_info); |
Sage Weil | 15637c8 | 2010-03-16 13:42:00 -0700 | [diff] [blame] | 3947 | goto done_unlocked; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3948 | |
| 3949 | case CEPH_CAP_OP_FLUSH_ACK: |
Yan, Zheng | a1c6b83 | 2018-04-27 10:29:44 +0800 | [diff] [blame] | 3950 | handle_cap_flush_ack(inode, le64_to_cpu(msg->hdr.tid), |
| 3951 | h, session, cap); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3952 | break; |
| 3953 | |
| 3954 | case CEPH_CAP_OP_TRUNC: |
| 3955 | handle_cap_trunc(inode, h, session); |
| 3956 | break; |
| 3957 | |
| 3958 | default: |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 3959 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3960 | pr_err("ceph_handle_caps: unknown cap op %d %s\n", op, |
| 3961 | ceph_cap_op_name(op)); |
| 3962 | } |
| 3963 | |
Yan, Zheng | e3ec8d6 | 2019-01-14 17:21:19 +0800 | [diff] [blame] | 3964 | done: |
| 3965 | mutex_unlock(&session->s_mutex); |
| 3966 | done_unlocked: |
| 3967 | iput(inode); |
| 3968 | ceph_put_string(extra_info.pool_ns); |
| 3969 | return; |
Greg Farnum | 21b559d | 2010-10-06 15:46:30 -0700 | [diff] [blame] | 3970 | |
| 3971 | flush_cap_releases: |
| 3972 | /* |
Yan, Zheng | 745a8e3 | 2015-05-14 17:22:42 +0800 | [diff] [blame] | 3973 | * send any cap release message to try to move things |
Greg Farnum | 21b559d | 2010-10-06 15:46:30 -0700 | [diff] [blame] | 3974 | * along for the mds (who clearly thinks we still have this |
| 3975 | * cap). |
| 3976 | */ |
Yan, Zheng | e3ec8d6 | 2019-01-14 17:21:19 +0800 | [diff] [blame] | 3977 | ceph_flush_cap_releases(mdsc, session); |
| 3978 | goto done; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3979 | |
| 3980 | bad: |
| 3981 | pr_err("ceph_handle_caps: corrupt message\n"); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 3982 | ceph_msg_dump(msg); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3983 | return; |
| 3984 | } |
| 3985 | |
| 3986 | /* |
| 3987 | * Delayed work handler to process end of delayed cap release LRU list. |
| 3988 | */ |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 3989 | void ceph_check_delayed_caps(struct ceph_mds_client *mdsc) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3990 | { |
Yan, Zheng | 4b9f204 | 2017-06-27 17:17:24 +0800 | [diff] [blame] | 3991 | struct inode *inode; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3992 | struct ceph_inode_info *ci; |
| 3993 | int flags = CHECK_CAPS_NODELAY; |
| 3994 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3995 | dout("check_delayed_caps\n"); |
| 3996 | while (1) { |
| 3997 | spin_lock(&mdsc->cap_delay_lock); |
| 3998 | if (list_empty(&mdsc->cap_delay_list)) |
| 3999 | break; |
| 4000 | ci = list_first_entry(&mdsc->cap_delay_list, |
| 4001 | struct ceph_inode_info, |
| 4002 | i_cap_delay_list); |
| 4003 | if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 && |
| 4004 | time_before(jiffies, ci->i_hold_caps_max)) |
| 4005 | break; |
| 4006 | list_del_init(&ci->i_cap_delay_list); |
Yan, Zheng | 4b9f204 | 2017-06-27 17:17:24 +0800 | [diff] [blame] | 4007 | |
| 4008 | inode = igrab(&ci->vfs_inode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4009 | spin_unlock(&mdsc->cap_delay_lock); |
Yan, Zheng | 4b9f204 | 2017-06-27 17:17:24 +0800 | [diff] [blame] | 4010 | |
| 4011 | if (inode) { |
| 4012 | dout("check_delayed_caps on %p\n", inode); |
| 4013 | ceph_check_caps(ci, flags, NULL); |
| 4014 | iput(inode); |
| 4015 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4016 | } |
| 4017 | spin_unlock(&mdsc->cap_delay_lock); |
| 4018 | } |
| 4019 | |
| 4020 | /* |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 4021 | * Flush all dirty caps to the mds |
| 4022 | */ |
| 4023 | void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc) |
| 4024 | { |
Sage Weil | db35405 | 2011-05-24 11:46:31 -0700 | [diff] [blame] | 4025 | struct ceph_inode_info *ci; |
| 4026 | struct inode *inode; |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 4027 | |
| 4028 | dout("flush_dirty_caps\n"); |
| 4029 | spin_lock(&mdsc->cap_dirty_lock); |
Sage Weil | db35405 | 2011-05-24 11:46:31 -0700 | [diff] [blame] | 4030 | while (!list_empty(&mdsc->cap_dirty)) { |
| 4031 | ci = list_first_entry(&mdsc->cap_dirty, struct ceph_inode_info, |
| 4032 | i_dirty_item); |
Sage Weil | 70b666c | 2011-05-27 09:24:26 -0700 | [diff] [blame] | 4033 | inode = &ci->vfs_inode; |
| 4034 | ihold(inode); |
Sage Weil | db35405 | 2011-05-24 11:46:31 -0700 | [diff] [blame] | 4035 | dout("flush_dirty_caps %p\n", inode); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 4036 | spin_unlock(&mdsc->cap_dirty_lock); |
Sage Weil | 70b666c | 2011-05-27 09:24:26 -0700 | [diff] [blame] | 4037 | ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH, NULL); |
| 4038 | iput(inode); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 4039 | spin_lock(&mdsc->cap_dirty_lock); |
| 4040 | } |
| 4041 | spin_unlock(&mdsc->cap_dirty_lock); |
Sage Weil | db35405 | 2011-05-24 11:46:31 -0700 | [diff] [blame] | 4042 | dout("flush_dirty_caps done\n"); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 4043 | } |
| 4044 | |
Yan, Zheng | 774a6a1 | 2016-06-06 16:01:39 +0800 | [diff] [blame] | 4045 | void __ceph_get_fmode(struct ceph_inode_info *ci, int fmode) |
| 4046 | { |
| 4047 | int i; |
| 4048 | int bits = (fmode << 1) | 1; |
| 4049 | for (i = 0; i < CEPH_FILE_MODE_BITS; i++) { |
| 4050 | if (bits & (1 << i)) |
| 4051 | ci->i_nr_by_mode[i]++; |
| 4052 | } |
| 4053 | } |
| 4054 | |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 4055 | /* |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4056 | * Drop open file reference. If we were the last open file, |
| 4057 | * we may need to release capabilities to the MDS (or schedule |
| 4058 | * their delayed release). |
| 4059 | */ |
| 4060 | void ceph_put_fmode(struct ceph_inode_info *ci, int fmode) |
| 4061 | { |
Yan, Zheng | 774a6a1 | 2016-06-06 16:01:39 +0800 | [diff] [blame] | 4062 | int i, last = 0; |
| 4063 | int bits = (fmode << 1) | 1; |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 4064 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | 774a6a1 | 2016-06-06 16:01:39 +0800 | [diff] [blame] | 4065 | for (i = 0; i < CEPH_FILE_MODE_BITS; i++) { |
| 4066 | if (bits & (1 << i)) { |
| 4067 | BUG_ON(ci->i_nr_by_mode[i] == 0); |
| 4068 | if (--ci->i_nr_by_mode[i] == 0) |
| 4069 | last++; |
| 4070 | } |
| 4071 | } |
| 4072 | dout("put_fmode %p fmode %d {%d,%d,%d,%d}\n", |
| 4073 | &ci->vfs_inode, fmode, |
| 4074 | ci->i_nr_by_mode[0], ci->i_nr_by_mode[1], |
| 4075 | ci->i_nr_by_mode[2], ci->i_nr_by_mode[3]); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 4076 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4077 | |
| 4078 | if (last && ci->i_vino.snap == CEPH_NOSNAP) |
| 4079 | ceph_check_caps(ci, 0, NULL); |
| 4080 | } |
| 4081 | |
| 4082 | /* |
Jeff Layton | a452bc0 | 2019-04-02 14:20:24 -0400 | [diff] [blame] | 4083 | * For a soon-to-be unlinked file, drop the LINK caps. If it |
Zhi Zhang | 6ef0bc6 | 2018-01-24 21:24:33 +0800 | [diff] [blame] | 4084 | * looks like the link count will hit 0, drop any other caps (other |
| 4085 | * than PIN) we don't specifically want (due to the file still being |
| 4086 | * open). |
| 4087 | */ |
| 4088 | int ceph_drop_caps_for_unlink(struct inode *inode) |
| 4089 | { |
| 4090 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 4091 | int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL; |
| 4092 | |
| 4093 | spin_lock(&ci->i_ceph_lock); |
| 4094 | if (inode->i_nlink == 1) { |
| 4095 | drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN); |
| 4096 | |
| 4097 | ci->i_ceph_flags |= CEPH_I_NODELAY; |
| 4098 | if (__ceph_caps_dirty(ci)) { |
| 4099 | struct ceph_mds_client *mdsc = |
| 4100 | ceph_inode_to_client(inode)->mdsc; |
| 4101 | __cap_delay_requeue_front(mdsc, ci); |
| 4102 | } |
| 4103 | } |
| 4104 | spin_unlock(&ci->i_ceph_lock); |
| 4105 | return drop; |
| 4106 | } |
| 4107 | |
| 4108 | /* |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4109 | * Helpers for embedding cap and dentry lease releases into mds |
| 4110 | * requests. |
| 4111 | * |
| 4112 | * @force is used by dentry_release (below) to force inclusion of a |
| 4113 | * record for the directory inode, even when there aren't any caps to |
| 4114 | * drop. |
| 4115 | */ |
| 4116 | int ceph_encode_inode_release(void **p, struct inode *inode, |
| 4117 | int mds, int drop, int unless, int force) |
| 4118 | { |
| 4119 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 4120 | struct ceph_cap *cap; |
| 4121 | struct ceph_mds_request_release *rel = *p; |
Sage Weil | ec97f88 | 2010-06-24 15:12:37 -0700 | [diff] [blame] | 4122 | int used, dirty; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4123 | int ret = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4124 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 4125 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 916623d | 2010-03-16 15:01:07 -0700 | [diff] [blame] | 4126 | used = __ceph_caps_used(ci); |
Sage Weil | ec97f88 | 2010-06-24 15:12:37 -0700 | [diff] [blame] | 4127 | dirty = __ceph_caps_dirty(ci); |
Sage Weil | 916623d | 2010-03-16 15:01:07 -0700 | [diff] [blame] | 4128 | |
Sage Weil | ec97f88 | 2010-06-24 15:12:37 -0700 | [diff] [blame] | 4129 | dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n", |
| 4130 | inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop), |
Sage Weil | 916623d | 2010-03-16 15:01:07 -0700 | [diff] [blame] | 4131 | ceph_cap_string(unless)); |
| 4132 | |
Sage Weil | ec97f88 | 2010-06-24 15:12:37 -0700 | [diff] [blame] | 4133 | /* only drop unused, clean caps */ |
| 4134 | drop &= ~(used | dirty); |
Sage Weil | 916623d | 2010-03-16 15:01:07 -0700 | [diff] [blame] | 4135 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4136 | cap = __get_cap_for_mds(ci, mds); |
| 4137 | if (cap && __cap_is_valid(cap)) { |
Yan, Zheng | 222b7f9 | 2017-11-23 17:47:15 +0800 | [diff] [blame] | 4138 | unless &= cap->issued; |
| 4139 | if (unless) { |
| 4140 | if (unless & CEPH_CAP_AUTH_EXCL) |
| 4141 | drop &= ~CEPH_CAP_AUTH_SHARED; |
| 4142 | if (unless & CEPH_CAP_LINK_EXCL) |
| 4143 | drop &= ~CEPH_CAP_LINK_SHARED; |
| 4144 | if (unless & CEPH_CAP_XATTR_EXCL) |
| 4145 | drop &= ~CEPH_CAP_XATTR_SHARED; |
| 4146 | if (unless & CEPH_CAP_FILE_EXCL) |
| 4147 | drop &= ~CEPH_CAP_FILE_SHARED; |
| 4148 | } |
| 4149 | |
| 4150 | if (force || (cap->issued & drop)) { |
| 4151 | if (cap->issued & drop) { |
Yan, Zheng | bb137f8 | 2013-06-03 18:22:17 +0800 | [diff] [blame] | 4152 | int wanted = __ceph_caps_wanted(ci); |
| 4153 | if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0) |
| 4154 | wanted |= cap->mds_wanted; |
| 4155 | dout("encode_inode_release %p cap %p " |
| 4156 | "%s -> %s, wanted %s -> %s\n", inode, cap, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4157 | ceph_cap_string(cap->issued), |
Yan, Zheng | bb137f8 | 2013-06-03 18:22:17 +0800 | [diff] [blame] | 4158 | ceph_cap_string(cap->issued & ~drop), |
| 4159 | ceph_cap_string(cap->mds_wanted), |
| 4160 | ceph_cap_string(wanted)); |
| 4161 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4162 | cap->issued &= ~drop; |
| 4163 | cap->implemented &= ~drop; |
Yan, Zheng | bb137f8 | 2013-06-03 18:22:17 +0800 | [diff] [blame] | 4164 | cap->mds_wanted = wanted; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4165 | } else { |
| 4166 | dout("encode_inode_release %p cap %p %s" |
| 4167 | " (force)\n", inode, cap, |
| 4168 | ceph_cap_string(cap->issued)); |
| 4169 | } |
| 4170 | |
| 4171 | rel->ino = cpu_to_le64(ceph_ino(inode)); |
| 4172 | rel->cap_id = cpu_to_le64(cap->cap_id); |
| 4173 | rel->seq = cpu_to_le32(cap->seq); |
Himangi Saraogi | 08a0f24 | 2014-07-23 20:11:11 +0530 | [diff] [blame] | 4174 | rel->issue_seq = cpu_to_le32(cap->issue_seq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4175 | rel->mseq = cpu_to_le32(cap->mseq); |
Yan, Zheng | fd7b95c | 2014-04-17 08:02:02 +0800 | [diff] [blame] | 4176 | rel->caps = cpu_to_le32(cap->implemented); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4177 | rel->wanted = cpu_to_le32(cap->mds_wanted); |
| 4178 | rel->dname_len = 0; |
| 4179 | rel->dname_seq = 0; |
| 4180 | *p += sizeof(*rel); |
| 4181 | ret = 1; |
| 4182 | } else { |
Yan, Zheng | 222b7f9 | 2017-11-23 17:47:15 +0800 | [diff] [blame] | 4183 | dout("encode_inode_release %p cap %p %s (noop)\n", |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4184 | inode, cap, ceph_cap_string(cap->issued)); |
| 4185 | } |
| 4186 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 4187 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4188 | return ret; |
| 4189 | } |
| 4190 | |
| 4191 | int ceph_encode_dentry_release(void **p, struct dentry *dentry, |
Jeff Layton | ca6c8ae | 2016-12-15 08:37:59 -0500 | [diff] [blame] | 4192 | struct inode *dir, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4193 | int mds, int drop, int unless) |
| 4194 | { |
Jeff Layton | ca6c8ae | 2016-12-15 08:37:59 -0500 | [diff] [blame] | 4195 | struct dentry *parent = NULL; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4196 | struct ceph_mds_request_release *rel = *p; |
| 4197 | struct ceph_dentry_info *di = ceph_dentry(dentry); |
| 4198 | int force = 0; |
| 4199 | int ret; |
| 4200 | |
| 4201 | /* |
| 4202 | * force an record for the directory caps if we have a dentry lease. |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 4203 | * this is racy (can't take i_ceph_lock and d_lock together), but it |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4204 | * doesn't have to be perfect; the mds will revoke anything we don't |
| 4205 | * release. |
| 4206 | */ |
| 4207 | spin_lock(&dentry->d_lock); |
| 4208 | if (di->lease_session && di->lease_session->s_mds == mds) |
| 4209 | force = 1; |
Jeff Layton | ca6c8ae | 2016-12-15 08:37:59 -0500 | [diff] [blame] | 4210 | if (!dir) { |
| 4211 | parent = dget(dentry->d_parent); |
| 4212 | dir = d_inode(parent); |
| 4213 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4214 | spin_unlock(&dentry->d_lock); |
| 4215 | |
Jeff Layton | ca6c8ae | 2016-12-15 08:37:59 -0500 | [diff] [blame] | 4216 | ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force); |
Jeff Layton | adf0d68 | 2016-12-15 08:37:58 -0500 | [diff] [blame] | 4217 | dput(parent); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4218 | |
| 4219 | spin_lock(&dentry->d_lock); |
| 4220 | if (ret && di->lease_session && di->lease_session->s_mds == mds) { |
| 4221 | dout("encode_dentry_release %p mds%d seq %d\n", |
| 4222 | dentry, mds, (int)di->lease_seq); |
| 4223 | rel->dname_len = cpu_to_le32(dentry->d_name.len); |
| 4224 | memcpy(*p, dentry->d_name.name, dentry->d_name.len); |
| 4225 | *p += dentry->d_name.len; |
| 4226 | rel->dname_seq = cpu_to_le32(di->lease_seq); |
Sage Weil | 1dadcce | 2010-07-23 13:54:21 -0700 | [diff] [blame] | 4227 | __ceph_mdsc_drop_dentry_lease(dentry); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 4228 | } |
| 4229 | spin_unlock(&dentry->d_lock); |
| 4230 | return ret; |
| 4231 | } |