Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1 | #include "ceph_debug.h" |
| 2 | |
| 3 | #include <linux/fs.h> |
| 4 | #include <linux/kernel.h> |
| 5 | #include <linux/sched.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 6 | #include <linux/slab.h> |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 7 | #include <linux/vmalloc.h> |
| 8 | #include <linux/wait.h> |
Stephen Rothwell | f1a3d57 | 2010-01-18 11:53:08 +1100 | [diff] [blame] | 9 | #include <linux/writeback.h> |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 10 | |
| 11 | #include "super.h" |
| 12 | #include "decode.h" |
| 13 | #include "messenger.h" |
| 14 | |
| 15 | /* |
| 16 | * Capability management |
| 17 | * |
| 18 | * The Ceph metadata servers control client access to inode metadata |
| 19 | * and file data by issuing capabilities, granting clients permission |
| 20 | * to read and/or write both inode field and file data to OSDs |
| 21 | * (storage nodes). Each capability consists of a set of bits |
| 22 | * indicating which operations are allowed. |
| 23 | * |
| 24 | * If the client holds a *_SHARED cap, the client has a coherent value |
| 25 | * that can be safely read from the cached inode. |
| 26 | * |
| 27 | * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the |
| 28 | * client is allowed to change inode attributes (e.g., file size, |
| 29 | * mtime), note its dirty state in the ceph_cap, and asynchronously |
| 30 | * flush that metadata change to the MDS. |
| 31 | * |
| 32 | * In the event of a conflicting operation (perhaps by another |
| 33 | * client), the MDS will revoke the conflicting client capabilities. |
| 34 | * |
| 35 | * In order for a client to cache an inode, it must hold a capability |
| 36 | * with at least one MDS server. When inodes are released, release |
| 37 | * notifications are batched and periodically sent en masse to the MDS |
| 38 | * cluster to release server state. |
| 39 | */ |
| 40 | |
| 41 | |
| 42 | /* |
| 43 | * Generate readable cap strings for debugging output. |
| 44 | */ |
| 45 | #define MAX_CAP_STR 20 |
| 46 | static char cap_str[MAX_CAP_STR][40]; |
| 47 | static DEFINE_SPINLOCK(cap_str_lock); |
| 48 | static int last_cap_str; |
| 49 | |
| 50 | static char *gcap_string(char *s, int c) |
| 51 | { |
| 52 | if (c & CEPH_CAP_GSHARED) |
| 53 | *s++ = 's'; |
| 54 | if (c & CEPH_CAP_GEXCL) |
| 55 | *s++ = 'x'; |
| 56 | if (c & CEPH_CAP_GCACHE) |
| 57 | *s++ = 'c'; |
| 58 | if (c & CEPH_CAP_GRD) |
| 59 | *s++ = 'r'; |
| 60 | if (c & CEPH_CAP_GWR) |
| 61 | *s++ = 'w'; |
| 62 | if (c & CEPH_CAP_GBUFFER) |
| 63 | *s++ = 'b'; |
| 64 | if (c & CEPH_CAP_GLAZYIO) |
| 65 | *s++ = 'l'; |
| 66 | return s; |
| 67 | } |
| 68 | |
| 69 | const char *ceph_cap_string(int caps) |
| 70 | { |
| 71 | int i; |
| 72 | char *s; |
| 73 | int c; |
| 74 | |
| 75 | spin_lock(&cap_str_lock); |
| 76 | i = last_cap_str++; |
| 77 | if (last_cap_str == MAX_CAP_STR) |
| 78 | last_cap_str = 0; |
| 79 | spin_unlock(&cap_str_lock); |
| 80 | |
| 81 | s = cap_str[i]; |
| 82 | |
| 83 | if (caps & CEPH_CAP_PIN) |
| 84 | *s++ = 'p'; |
| 85 | |
| 86 | c = (caps >> CEPH_CAP_SAUTH) & 3; |
| 87 | if (c) { |
| 88 | *s++ = 'A'; |
| 89 | s = gcap_string(s, c); |
| 90 | } |
| 91 | |
| 92 | c = (caps >> CEPH_CAP_SLINK) & 3; |
| 93 | if (c) { |
| 94 | *s++ = 'L'; |
| 95 | s = gcap_string(s, c); |
| 96 | } |
| 97 | |
| 98 | c = (caps >> CEPH_CAP_SXATTR) & 3; |
| 99 | if (c) { |
| 100 | *s++ = 'X'; |
| 101 | s = gcap_string(s, c); |
| 102 | } |
| 103 | |
| 104 | c = caps >> CEPH_CAP_SFILE; |
| 105 | if (c) { |
| 106 | *s++ = 'F'; |
| 107 | s = gcap_string(s, c); |
| 108 | } |
| 109 | |
| 110 | if (s == cap_str[i]) |
| 111 | *s++ = '-'; |
| 112 | *s = 0; |
| 113 | return cap_str[i]; |
| 114 | } |
| 115 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 116 | void ceph_caps_init(struct ceph_mds_client *mdsc) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 117 | { |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 118 | INIT_LIST_HEAD(&mdsc->caps_list); |
| 119 | spin_lock_init(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 122 | void ceph_caps_finalize(struct ceph_mds_client *mdsc) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 123 | { |
| 124 | struct ceph_cap *cap; |
| 125 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 126 | spin_lock(&mdsc->caps_list_lock); |
| 127 | while (!list_empty(&mdsc->caps_list)) { |
| 128 | cap = list_first_entry(&mdsc->caps_list, |
| 129 | struct ceph_cap, caps_item); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 130 | list_del(&cap->caps_item); |
| 131 | kmem_cache_free(ceph_cap_cachep, cap); |
| 132 | } |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 133 | mdsc->caps_total_count = 0; |
| 134 | mdsc->caps_avail_count = 0; |
| 135 | mdsc->caps_use_count = 0; |
| 136 | mdsc->caps_reserve_count = 0; |
| 137 | mdsc->caps_min_count = 0; |
| 138 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | 85ccce4 | 2010-02-17 10:02:43 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 141 | void ceph_adjust_min_caps(struct ceph_mds_client *mdsc, int delta) |
Sage Weil | 85ccce4 | 2010-02-17 10:02:43 -0800 | [diff] [blame] | 142 | { |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 143 | spin_lock(&mdsc->caps_list_lock); |
| 144 | mdsc->caps_min_count += delta; |
| 145 | BUG_ON(mdsc->caps_min_count < 0); |
| 146 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 149 | int ceph_reserve_caps(struct ceph_mds_client *mdsc, |
| 150 | struct ceph_cap_reservation *ctx, int need) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 151 | { |
| 152 | int i; |
| 153 | struct ceph_cap *cap; |
| 154 | int have; |
| 155 | int alloc = 0; |
| 156 | LIST_HEAD(newcaps); |
| 157 | int ret = 0; |
| 158 | |
| 159 | dout("reserve caps ctx=%p need=%d\n", ctx, need); |
| 160 | |
| 161 | /* first reserve any caps that are already allocated */ |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 162 | spin_lock(&mdsc->caps_list_lock); |
| 163 | if (mdsc->caps_avail_count >= need) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 164 | have = need; |
| 165 | else |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 166 | have = mdsc->caps_avail_count; |
| 167 | mdsc->caps_avail_count -= have; |
| 168 | mdsc->caps_reserve_count += have; |
| 169 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
| 170 | mdsc->caps_reserve_count + |
| 171 | mdsc->caps_avail_count); |
| 172 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 173 | |
| 174 | for (i = have; i < need; i++) { |
| 175 | cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS); |
| 176 | if (!cap) { |
| 177 | ret = -ENOMEM; |
| 178 | goto out_alloc_count; |
| 179 | } |
| 180 | list_add(&cap->caps_item, &newcaps); |
| 181 | alloc++; |
| 182 | } |
| 183 | BUG_ON(have + alloc != need); |
| 184 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 185 | spin_lock(&mdsc->caps_list_lock); |
| 186 | mdsc->caps_total_count += alloc; |
| 187 | mdsc->caps_reserve_count += alloc; |
| 188 | list_splice(&newcaps, &mdsc->caps_list); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 189 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 190 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
| 191 | mdsc->caps_reserve_count + |
| 192 | mdsc->caps_avail_count); |
| 193 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 194 | |
| 195 | ctx->count = need; |
| 196 | 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] | 197 | ctx, mdsc->caps_total_count, mdsc->caps_use_count, |
| 198 | mdsc->caps_reserve_count, mdsc->caps_avail_count); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 199 | return 0; |
| 200 | |
| 201 | out_alloc_count: |
| 202 | /* we didn't manage to reserve as much as we needed */ |
| 203 | pr_warning("reserve caps ctx=%p ENOMEM need=%d got=%d\n", |
| 204 | ctx, need, have); |
| 205 | return ret; |
| 206 | } |
| 207 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 208 | int ceph_unreserve_caps(struct ceph_mds_client *mdsc, |
| 209 | struct ceph_cap_reservation *ctx) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 210 | { |
| 211 | dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count); |
| 212 | if (ctx->count) { |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 213 | spin_lock(&mdsc->caps_list_lock); |
| 214 | BUG_ON(mdsc->caps_reserve_count < ctx->count); |
| 215 | mdsc->caps_reserve_count -= ctx->count; |
| 216 | mdsc->caps_avail_count += ctx->count; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 217 | ctx->count = 0; |
| 218 | dout("unreserve caps %d = %d used + %d resv + %d avail\n", |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 219 | mdsc->caps_total_count, mdsc->caps_use_count, |
| 220 | mdsc->caps_reserve_count, mdsc->caps_avail_count); |
| 221 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
| 222 | mdsc->caps_reserve_count + |
| 223 | mdsc->caps_avail_count); |
| 224 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 225 | } |
| 226 | return 0; |
| 227 | } |
| 228 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 229 | static struct ceph_cap *get_cap(struct ceph_mds_client *mdsc, |
| 230 | struct ceph_cap_reservation *ctx) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 231 | { |
| 232 | struct ceph_cap *cap = NULL; |
| 233 | |
| 234 | /* temporary, until we do something about cap import/export */ |
Sage Weil | 443b376 | 2010-06-29 09:28:39 -0700 | [diff] [blame] | 235 | if (!ctx) { |
| 236 | cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS); |
| 237 | if (cap) { |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 238 | mdsc->caps_use_count++; |
| 239 | mdsc->caps_total_count++; |
Sage Weil | 443b376 | 2010-06-29 09:28:39 -0700 | [diff] [blame] | 240 | } |
| 241 | return cap; |
| 242 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 243 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 244 | spin_lock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 245 | 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] | 246 | ctx, ctx->count, mdsc->caps_total_count, mdsc->caps_use_count, |
| 247 | mdsc->caps_reserve_count, mdsc->caps_avail_count); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 248 | BUG_ON(!ctx->count); |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 249 | BUG_ON(ctx->count > mdsc->caps_reserve_count); |
| 250 | BUG_ON(list_empty(&mdsc->caps_list)); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 251 | |
| 252 | ctx->count--; |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 253 | mdsc->caps_reserve_count--; |
| 254 | mdsc->caps_use_count++; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 255 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 256 | cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 257 | list_del(&cap->caps_item); |
| 258 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 259 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
| 260 | mdsc->caps_reserve_count + mdsc->caps_avail_count); |
| 261 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 262 | return cap; |
| 263 | } |
| 264 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 265 | 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] | 266 | { |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 267 | spin_lock(&mdsc->caps_list_lock); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 268 | dout("put_cap %p %d = %d used + %d resv + %d avail\n", |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 269 | cap, mdsc->caps_total_count, mdsc->caps_use_count, |
| 270 | mdsc->caps_reserve_count, mdsc->caps_avail_count); |
| 271 | mdsc->caps_use_count--; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 272 | /* |
Sage Weil | 85ccce4 | 2010-02-17 10:02:43 -0800 | [diff] [blame] | 273 | * Keep some preallocated caps around (ceph_min_count), to |
| 274 | * avoid lots of free/alloc churn. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 275 | */ |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 276 | if (mdsc->caps_avail_count >= mdsc->caps_reserve_count + |
| 277 | mdsc->caps_min_count) { |
| 278 | mdsc->caps_total_count--; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 279 | kmem_cache_free(ceph_cap_cachep, cap); |
| 280 | } else { |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 281 | mdsc->caps_avail_count++; |
| 282 | list_add(&cap->caps_item, &mdsc->caps_list); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 285 | BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + |
| 286 | mdsc->caps_reserve_count + mdsc->caps_avail_count); |
| 287 | spin_unlock(&mdsc->caps_list_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | void ceph_reservation_status(struct ceph_client *client, |
Sage Weil | 85ccce4 | 2010-02-17 10:02:43 -0800 | [diff] [blame] | 291 | int *total, int *avail, int *used, int *reserved, |
| 292 | int *min) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 293 | { |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 294 | struct ceph_mds_client *mdsc = &client->mdsc; |
| 295 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 296 | if (total) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 297 | *total = mdsc->caps_total_count; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 298 | if (avail) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 299 | *avail = mdsc->caps_avail_count; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 300 | if (used) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 301 | *used = mdsc->caps_use_count; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 302 | if (reserved) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 303 | *reserved = mdsc->caps_reserve_count; |
Sage Weil | 85ccce4 | 2010-02-17 10:02:43 -0800 | [diff] [blame] | 304 | if (min) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 305 | *min = mdsc->caps_min_count; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /* |
| 309 | * Find ceph_cap for given mds, if any. |
| 310 | * |
| 311 | * Called with i_lock held. |
| 312 | */ |
| 313 | static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds) |
| 314 | { |
| 315 | struct ceph_cap *cap; |
| 316 | struct rb_node *n = ci->i_caps.rb_node; |
| 317 | |
| 318 | while (n) { |
| 319 | cap = rb_entry(n, struct ceph_cap, ci_node); |
| 320 | if (mds < cap->mds) |
| 321 | n = n->rb_left; |
| 322 | else if (mds > cap->mds) |
| 323 | n = n->rb_right; |
| 324 | else |
| 325 | return cap; |
| 326 | } |
| 327 | return NULL; |
| 328 | } |
| 329 | |
Greg Farnum | 2bc5025 | 2010-06-30 12:44:34 -0700 | [diff] [blame^] | 330 | struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds) |
| 331 | { |
| 332 | struct ceph_cap *cap; |
| 333 | |
| 334 | spin_lock(&ci->vfs_inode.i_lock); |
| 335 | cap = __get_cap_for_mds(ci, mds); |
| 336 | spin_unlock(&ci->vfs_inode.i_lock); |
| 337 | return cap; |
| 338 | } |
| 339 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 340 | /* |
Sage Weil | 33caad3 | 2010-05-26 14:31:27 -0700 | [diff] [blame] | 341 | * 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] | 342 | */ |
Sage Weil | ca81f3f | 2010-06-10 14:21:36 -0700 | [diff] [blame] | 343 | static int __ceph_get_cap_mds(struct ceph_inode_info *ci) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 344 | { |
| 345 | struct ceph_cap *cap; |
| 346 | int mds = -1; |
| 347 | struct rb_node *p; |
| 348 | |
Sage Weil | 33caad3 | 2010-05-26 14:31:27 -0700 | [diff] [blame] | 349 | /* prefer mds with WR|BUFFER|EXCL caps */ |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 350 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 351 | cap = rb_entry(p, struct ceph_cap, ci_node); |
| 352 | mds = cap->mds; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 353 | if (cap->issued & (CEPH_CAP_FILE_WR | |
| 354 | CEPH_CAP_FILE_BUFFER | |
| 355 | CEPH_CAP_FILE_EXCL)) |
| 356 | break; |
| 357 | } |
| 358 | return mds; |
| 359 | } |
| 360 | |
| 361 | int ceph_get_cap_mds(struct inode *inode) |
| 362 | { |
| 363 | int mds; |
| 364 | spin_lock(&inode->i_lock); |
Sage Weil | ca81f3f | 2010-06-10 14:21:36 -0700 | [diff] [blame] | 365 | mds = __ceph_get_cap_mds(ceph_inode(inode)); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 366 | spin_unlock(&inode->i_lock); |
| 367 | return mds; |
| 368 | } |
| 369 | |
| 370 | /* |
| 371 | * Called under i_lock. |
| 372 | */ |
| 373 | static void __insert_cap_node(struct ceph_inode_info *ci, |
| 374 | struct ceph_cap *new) |
| 375 | { |
| 376 | struct rb_node **p = &ci->i_caps.rb_node; |
| 377 | struct rb_node *parent = NULL; |
| 378 | struct ceph_cap *cap = NULL; |
| 379 | |
| 380 | while (*p) { |
| 381 | parent = *p; |
| 382 | cap = rb_entry(parent, struct ceph_cap, ci_node); |
| 383 | if (new->mds < cap->mds) |
| 384 | p = &(*p)->rb_left; |
| 385 | else if (new->mds > cap->mds) |
| 386 | p = &(*p)->rb_right; |
| 387 | else |
| 388 | BUG(); |
| 389 | } |
| 390 | |
| 391 | rb_link_node(&new->ci_node, parent, p); |
| 392 | rb_insert_color(&new->ci_node, &ci->i_caps); |
| 393 | } |
| 394 | |
| 395 | /* |
| 396 | * (re)set cap hold timeouts, which control the delayed release |
| 397 | * of unused caps back to the MDS. Should be called on cap use. |
| 398 | */ |
| 399 | static void __cap_set_timeouts(struct ceph_mds_client *mdsc, |
| 400 | struct ceph_inode_info *ci) |
| 401 | { |
Sage Weil | 6b80518 | 2009-10-27 11:50:50 -0700 | [diff] [blame] | 402 | struct ceph_mount_args *ma = mdsc->client->mount_args; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 403 | |
| 404 | ci->i_hold_caps_min = round_jiffies(jiffies + |
| 405 | ma->caps_wanted_delay_min * HZ); |
| 406 | ci->i_hold_caps_max = round_jiffies(jiffies + |
| 407 | ma->caps_wanted_delay_max * HZ); |
| 408 | dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode, |
| 409 | ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies); |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * (Re)queue cap at the end of the delayed cap release list. |
| 414 | * |
| 415 | * If I_FLUSH is set, leave the inode at the front of the list. |
| 416 | * |
| 417 | * Caller holds i_lock |
| 418 | * -> we take mdsc->cap_delay_lock |
| 419 | */ |
| 420 | static void __cap_delay_requeue(struct ceph_mds_client *mdsc, |
| 421 | struct ceph_inode_info *ci) |
| 422 | { |
| 423 | __cap_set_timeouts(mdsc, ci); |
| 424 | dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode, |
| 425 | ci->i_ceph_flags, ci->i_hold_caps_max); |
| 426 | if (!mdsc->stopping) { |
| 427 | spin_lock(&mdsc->cap_delay_lock); |
| 428 | if (!list_empty(&ci->i_cap_delay_list)) { |
| 429 | if (ci->i_ceph_flags & CEPH_I_FLUSH) |
| 430 | goto no_change; |
| 431 | list_del_init(&ci->i_cap_delay_list); |
| 432 | } |
| 433 | list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list); |
| 434 | no_change: |
| 435 | spin_unlock(&mdsc->cap_delay_lock); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | /* |
| 440 | * Queue an inode for immediate writeback. Mark inode with I_FLUSH, |
| 441 | * indicating we should send a cap message to flush dirty metadata |
| 442 | * asap, and move to the front of the delayed cap list. |
| 443 | */ |
| 444 | static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc, |
| 445 | struct ceph_inode_info *ci) |
| 446 | { |
| 447 | dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode); |
| 448 | spin_lock(&mdsc->cap_delay_lock); |
| 449 | ci->i_ceph_flags |= CEPH_I_FLUSH; |
| 450 | if (!list_empty(&ci->i_cap_delay_list)) |
| 451 | list_del_init(&ci->i_cap_delay_list); |
| 452 | list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list); |
| 453 | spin_unlock(&mdsc->cap_delay_lock); |
| 454 | } |
| 455 | |
| 456 | /* |
| 457 | * Cancel delayed work on cap. |
| 458 | * |
| 459 | * Caller must hold i_lock. |
| 460 | */ |
| 461 | static void __cap_delay_cancel(struct ceph_mds_client *mdsc, |
| 462 | struct ceph_inode_info *ci) |
| 463 | { |
| 464 | dout("__cap_delay_cancel %p\n", &ci->vfs_inode); |
| 465 | if (list_empty(&ci->i_cap_delay_list)) |
| 466 | return; |
| 467 | spin_lock(&mdsc->cap_delay_lock); |
| 468 | list_del_init(&ci->i_cap_delay_list); |
| 469 | spin_unlock(&mdsc->cap_delay_lock); |
| 470 | } |
| 471 | |
| 472 | /* |
| 473 | * Common issue checks for add_cap, handle_cap_grant. |
| 474 | */ |
| 475 | static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap, |
| 476 | unsigned issued) |
| 477 | { |
| 478 | unsigned had = __ceph_caps_issued(ci, NULL); |
| 479 | |
| 480 | /* |
| 481 | * Each time we receive FILE_CACHE anew, we increment |
| 482 | * i_rdcache_gen. |
| 483 | */ |
Sage Weil | 2962507 | 2010-05-27 10:40:43 -0700 | [diff] [blame] | 484 | if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) && |
| 485 | (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 486 | ci->i_rdcache_gen++; |
| 487 | |
| 488 | /* |
| 489 | * if we are newly issued FILE_SHARED, clear I_COMPLETE; we |
| 490 | * don't know what happened to this directory while we didn't |
| 491 | * have the cap. |
| 492 | */ |
| 493 | if ((issued & CEPH_CAP_FILE_SHARED) && |
| 494 | (had & CEPH_CAP_FILE_SHARED) == 0) { |
| 495 | ci->i_shared_gen++; |
| 496 | if (S_ISDIR(ci->vfs_inode.i_mode)) { |
| 497 | dout(" marking %p NOT complete\n", &ci->vfs_inode); |
| 498 | ci->i_ceph_flags &= ~CEPH_I_COMPLETE; |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | * Add a capability under the given MDS session. |
| 505 | * |
| 506 | * Caller should hold session snap_rwsem (read) and s_mutex. |
| 507 | * |
| 508 | * @fmode is the open file mode, if we are opening a file, otherwise |
| 509 | * it is < 0. (This is so we can atomically add the cap and add an |
| 510 | * open file reference to it.) |
| 511 | */ |
| 512 | int ceph_add_cap(struct inode *inode, |
| 513 | struct ceph_mds_session *session, u64 cap_id, |
| 514 | int fmode, unsigned issued, unsigned wanted, |
| 515 | unsigned seq, unsigned mseq, u64 realmino, int flags, |
| 516 | struct ceph_cap_reservation *caps_reservation) |
| 517 | { |
| 518 | struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc; |
| 519 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 520 | struct ceph_cap *new_cap = NULL; |
| 521 | struct ceph_cap *cap; |
| 522 | int mds = session->s_mds; |
| 523 | int actual_wanted; |
| 524 | |
| 525 | dout("add_cap %p mds%d cap %llx %s seq %d\n", inode, |
| 526 | session->s_mds, cap_id, ceph_cap_string(issued), seq); |
| 527 | |
| 528 | /* |
| 529 | * If we are opening the file, include file mode wanted bits |
| 530 | * in wanted. |
| 531 | */ |
| 532 | if (fmode >= 0) |
| 533 | wanted |= ceph_caps_for_mode(fmode); |
| 534 | |
| 535 | retry: |
| 536 | spin_lock(&inode->i_lock); |
| 537 | cap = __get_cap_for_mds(ci, mds); |
| 538 | if (!cap) { |
| 539 | if (new_cap) { |
| 540 | cap = new_cap; |
| 541 | new_cap = NULL; |
| 542 | } else { |
| 543 | spin_unlock(&inode->i_lock); |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 544 | new_cap = get_cap(mdsc, caps_reservation); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 545 | if (new_cap == NULL) |
| 546 | return -ENOMEM; |
| 547 | goto retry; |
| 548 | } |
| 549 | |
| 550 | cap->issued = 0; |
| 551 | cap->implemented = 0; |
| 552 | cap->mds = mds; |
| 553 | cap->mds_wanted = 0; |
| 554 | |
| 555 | cap->ci = ci; |
| 556 | __insert_cap_node(ci, cap); |
| 557 | |
| 558 | /* clear out old exporting info? (i.e. on cap import) */ |
| 559 | if (ci->i_cap_exporting_mds == mds) { |
| 560 | ci->i_cap_exporting_issued = 0; |
| 561 | ci->i_cap_exporting_mseq = 0; |
| 562 | ci->i_cap_exporting_mds = -1; |
| 563 | } |
| 564 | |
| 565 | /* add to session cap list */ |
| 566 | cap->session = session; |
| 567 | spin_lock(&session->s_cap_lock); |
| 568 | list_add_tail(&cap->session_caps, &session->s_caps); |
| 569 | session->s_nr_caps++; |
| 570 | spin_unlock(&session->s_cap_lock); |
| 571 | } |
| 572 | |
| 573 | if (!ci->i_snap_realm) { |
| 574 | /* |
| 575 | * add this inode to the appropriate snap realm |
| 576 | */ |
| 577 | struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc, |
| 578 | realmino); |
| 579 | if (realm) { |
| 580 | ceph_get_snap_realm(mdsc, realm); |
| 581 | spin_lock(&realm->inodes_with_caps_lock); |
| 582 | ci->i_snap_realm = realm; |
| 583 | list_add(&ci->i_snap_realm_item, |
| 584 | &realm->inodes_with_caps); |
| 585 | spin_unlock(&realm->inodes_with_caps_lock); |
| 586 | } else { |
| 587 | pr_err("ceph_add_cap: couldn't find snap realm %llx\n", |
| 588 | realmino); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | __check_cap_issue(ci, cap, issued); |
| 593 | |
| 594 | /* |
| 595 | * If we are issued caps we don't want, or the mds' wanted |
| 596 | * value appears to be off, queue a check so we'll release |
| 597 | * later and/or update the mds wanted value. |
| 598 | */ |
| 599 | actual_wanted = __ceph_caps_wanted(ci); |
| 600 | if ((wanted & ~actual_wanted) || |
| 601 | (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) { |
| 602 | dout(" issued %s, mds wanted %s, actual %s, queueing\n", |
| 603 | ceph_cap_string(issued), ceph_cap_string(wanted), |
| 604 | ceph_cap_string(actual_wanted)); |
| 605 | __cap_delay_requeue(mdsc, ci); |
| 606 | } |
| 607 | |
| 608 | if (flags & CEPH_CAP_FLAG_AUTH) |
| 609 | ci->i_auth_cap = cap; |
| 610 | else if (ci->i_auth_cap == cap) |
| 611 | ci->i_auth_cap = NULL; |
| 612 | |
| 613 | dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n", |
| 614 | inode, ceph_vinop(inode), cap, ceph_cap_string(issued), |
| 615 | ceph_cap_string(issued|cap->issued), seq, mds); |
| 616 | cap->cap_id = cap_id; |
| 617 | cap->issued = issued; |
| 618 | cap->implemented |= issued; |
| 619 | cap->mds_wanted |= wanted; |
| 620 | cap->seq = seq; |
| 621 | cap->issue_seq = seq; |
| 622 | cap->mseq = mseq; |
Sage Weil | 685f9a5d | 2009-11-09 12:05:48 -0800 | [diff] [blame] | 623 | cap->cap_gen = session->s_cap_gen; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 624 | |
| 625 | if (fmode >= 0) |
| 626 | __ceph_get_fmode(ci, fmode); |
| 627 | spin_unlock(&inode->i_lock); |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 628 | wake_up_all(&ci->i_cap_wq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | /* |
| 633 | * Return true if cap has not timed out and belongs to the current |
| 634 | * generation of the MDS session (i.e. has not gone 'stale' due to |
| 635 | * us losing touch with the mds). |
| 636 | */ |
| 637 | static int __cap_is_valid(struct ceph_cap *cap) |
| 638 | { |
| 639 | unsigned long ttl; |
Sage Weil | cdac830 | 2009-11-10 16:02:23 -0800 | [diff] [blame] | 640 | u32 gen; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 641 | |
| 642 | spin_lock(&cap->session->s_cap_lock); |
| 643 | gen = cap->session->s_cap_gen; |
| 644 | ttl = cap->session->s_cap_ttl; |
| 645 | spin_unlock(&cap->session->s_cap_lock); |
| 646 | |
Sage Weil | 685f9a5d | 2009-11-09 12:05:48 -0800 | [diff] [blame] | 647 | if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 648 | dout("__cap_is_valid %p cap %p issued %s " |
| 649 | "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode, |
Sage Weil | 685f9a5d | 2009-11-09 12:05:48 -0800 | [diff] [blame] | 650 | cap, ceph_cap_string(cap->issued), cap->cap_gen, gen); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | return 1; |
| 655 | } |
| 656 | |
| 657 | /* |
| 658 | * Return set of valid cap bits issued to us. Note that caps time |
| 659 | * out, and may be invalidated in bulk if the client session times out |
| 660 | * and session->s_cap_gen is bumped. |
| 661 | */ |
| 662 | int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented) |
| 663 | { |
Sage Weil | 7af8f1e | 2010-03-01 15:17:34 -0800 | [diff] [blame] | 664 | int have = ci->i_snap_caps | ci->i_cap_exporting_issued; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 665 | struct ceph_cap *cap; |
| 666 | struct rb_node *p; |
| 667 | |
| 668 | if (implemented) |
| 669 | *implemented = 0; |
| 670 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 671 | cap = rb_entry(p, struct ceph_cap, ci_node); |
| 672 | if (!__cap_is_valid(cap)) |
| 673 | continue; |
| 674 | dout("__ceph_caps_issued %p cap %p issued %s\n", |
| 675 | &ci->vfs_inode, cap, ceph_cap_string(cap->issued)); |
| 676 | have |= cap->issued; |
| 677 | if (implemented) |
| 678 | *implemented |= cap->implemented; |
| 679 | } |
| 680 | return have; |
| 681 | } |
| 682 | |
| 683 | /* |
| 684 | * Get cap bits issued by caps other than @ocap |
| 685 | */ |
| 686 | int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap) |
| 687 | { |
| 688 | int have = ci->i_snap_caps; |
| 689 | struct ceph_cap *cap; |
| 690 | struct rb_node *p; |
| 691 | |
| 692 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 693 | cap = rb_entry(p, struct ceph_cap, ci_node); |
| 694 | if (cap == ocap) |
| 695 | continue; |
| 696 | if (!__cap_is_valid(cap)) |
| 697 | continue; |
| 698 | have |= cap->issued; |
| 699 | } |
| 700 | return have; |
| 701 | } |
| 702 | |
| 703 | /* |
| 704 | * Move a cap to the end of the LRU (oldest caps at list head, newest |
| 705 | * at list tail). |
| 706 | */ |
| 707 | static void __touch_cap(struct ceph_cap *cap) |
| 708 | { |
| 709 | struct ceph_mds_session *s = cap->session; |
| 710 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 711 | spin_lock(&s->s_cap_lock); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 712 | if (s->s_cap_iterator == NULL) { |
Sage Weil | 5dacf09 | 2009-12-21 20:40:34 -0800 | [diff] [blame] | 713 | dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap, |
| 714 | s->s_mds); |
| 715 | list_move_tail(&cap->session_caps, &s->s_caps); |
| 716 | } else { |
| 717 | dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n", |
| 718 | &cap->ci->vfs_inode, cap, s->s_mds); |
| 719 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 720 | spin_unlock(&s->s_cap_lock); |
| 721 | } |
| 722 | |
| 723 | /* |
| 724 | * Check if we hold the given mask. If so, move the cap(s) to the |
| 725 | * front of their respective LRUs. (This is the preferred way for |
| 726 | * callers to check for caps they want.) |
| 727 | */ |
| 728 | int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch) |
| 729 | { |
| 730 | struct ceph_cap *cap; |
| 731 | struct rb_node *p; |
| 732 | int have = ci->i_snap_caps; |
| 733 | |
| 734 | if ((have & mask) == mask) { |
| 735 | dout("__ceph_caps_issued_mask %p snap issued %s" |
| 736 | " (mask %s)\n", &ci->vfs_inode, |
| 737 | ceph_cap_string(have), |
| 738 | ceph_cap_string(mask)); |
| 739 | return 1; |
| 740 | } |
| 741 | |
| 742 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 743 | cap = rb_entry(p, struct ceph_cap, ci_node); |
| 744 | if (!__cap_is_valid(cap)) |
| 745 | continue; |
| 746 | if ((cap->issued & mask) == mask) { |
| 747 | dout("__ceph_caps_issued_mask %p cap %p issued %s" |
| 748 | " (mask %s)\n", &ci->vfs_inode, cap, |
| 749 | ceph_cap_string(cap->issued), |
| 750 | ceph_cap_string(mask)); |
| 751 | if (touch) |
| 752 | __touch_cap(cap); |
| 753 | return 1; |
| 754 | } |
| 755 | |
| 756 | /* does a combination of caps satisfy mask? */ |
| 757 | have |= cap->issued; |
| 758 | if ((have & mask) == mask) { |
| 759 | dout("__ceph_caps_issued_mask %p combo issued %s" |
| 760 | " (mask %s)\n", &ci->vfs_inode, |
| 761 | ceph_cap_string(cap->issued), |
| 762 | ceph_cap_string(mask)); |
| 763 | if (touch) { |
| 764 | struct rb_node *q; |
| 765 | |
| 766 | /* touch this + preceeding caps */ |
| 767 | __touch_cap(cap); |
| 768 | for (q = rb_first(&ci->i_caps); q != p; |
| 769 | q = rb_next(q)) { |
| 770 | cap = rb_entry(q, struct ceph_cap, |
| 771 | ci_node); |
| 772 | if (!__cap_is_valid(cap)) |
| 773 | continue; |
| 774 | __touch_cap(cap); |
| 775 | } |
| 776 | } |
| 777 | return 1; |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | /* |
| 785 | * Return true if mask caps are currently being revoked by an MDS. |
| 786 | */ |
| 787 | int ceph_caps_revoking(struct ceph_inode_info *ci, int mask) |
| 788 | { |
| 789 | struct inode *inode = &ci->vfs_inode; |
| 790 | struct ceph_cap *cap; |
| 791 | struct rb_node *p; |
| 792 | int ret = 0; |
| 793 | |
| 794 | spin_lock(&inode->i_lock); |
| 795 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 796 | cap = rb_entry(p, struct ceph_cap, ci_node); |
| 797 | if (__cap_is_valid(cap) && |
| 798 | (cap->implemented & ~cap->issued & mask)) { |
| 799 | ret = 1; |
| 800 | break; |
| 801 | } |
| 802 | } |
| 803 | spin_unlock(&inode->i_lock); |
| 804 | dout("ceph_caps_revoking %p %s = %d\n", inode, |
| 805 | ceph_cap_string(mask), ret); |
| 806 | return ret; |
| 807 | } |
| 808 | |
| 809 | int __ceph_caps_used(struct ceph_inode_info *ci) |
| 810 | { |
| 811 | int used = 0; |
| 812 | if (ci->i_pin_ref) |
| 813 | used |= CEPH_CAP_PIN; |
| 814 | if (ci->i_rd_ref) |
| 815 | used |= CEPH_CAP_FILE_RD; |
| 816 | if (ci->i_rdcache_ref || ci->i_rdcache_gen) |
| 817 | used |= CEPH_CAP_FILE_CACHE; |
| 818 | if (ci->i_wr_ref) |
| 819 | used |= CEPH_CAP_FILE_WR; |
| 820 | if (ci->i_wrbuffer_ref) |
| 821 | used |= CEPH_CAP_FILE_BUFFER; |
| 822 | return used; |
| 823 | } |
| 824 | |
| 825 | /* |
| 826 | * wanted, by virtue of open file modes |
| 827 | */ |
| 828 | int __ceph_caps_file_wanted(struct ceph_inode_info *ci) |
| 829 | { |
| 830 | int want = 0; |
| 831 | int mode; |
Sage Weil | 33caad3 | 2010-05-26 14:31:27 -0700 | [diff] [blame] | 832 | for (mode = 0; mode < CEPH_FILE_MODE_NUM; mode++) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 833 | if (ci->i_nr_by_mode[mode]) |
| 834 | want |= ceph_caps_for_mode(mode); |
| 835 | return want; |
| 836 | } |
| 837 | |
| 838 | /* |
| 839 | * Return caps we have registered with the MDS(s) as 'wanted'. |
| 840 | */ |
| 841 | int __ceph_caps_mds_wanted(struct ceph_inode_info *ci) |
| 842 | { |
| 843 | struct ceph_cap *cap; |
| 844 | struct rb_node *p; |
| 845 | int mds_wanted = 0; |
| 846 | |
| 847 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 848 | cap = rb_entry(p, struct ceph_cap, ci_node); |
| 849 | if (!__cap_is_valid(cap)) |
| 850 | continue; |
| 851 | mds_wanted |= cap->mds_wanted; |
| 852 | } |
| 853 | return mds_wanted; |
| 854 | } |
| 855 | |
| 856 | /* |
| 857 | * called under i_lock |
| 858 | */ |
| 859 | static int __ceph_is_any_caps(struct ceph_inode_info *ci) |
| 860 | { |
| 861 | return !RB_EMPTY_ROOT(&ci->i_caps) || ci->i_cap_exporting_mds >= 0; |
| 862 | } |
| 863 | |
| 864 | /* |
Sage Weil | f818a73 | 2010-05-11 20:56:31 -0700 | [diff] [blame] | 865 | * Remove a cap. Take steps to deal with a racing iterate_session_caps. |
| 866 | * |
Sage Weil | a636974 | 2010-02-22 13:59:00 -0800 | [diff] [blame] | 867 | * caller should hold i_lock. |
| 868 | * caller will not hold session s_mutex if called from destroy_inode. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 869 | */ |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 870 | void __ceph_remove_cap(struct ceph_cap *cap) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 871 | { |
| 872 | struct ceph_mds_session *session = cap->session; |
| 873 | struct ceph_inode_info *ci = cap->ci; |
Cheng Renquan | 640ef79 | 2010-03-26 17:40:33 +0800 | [diff] [blame] | 874 | struct ceph_mds_client *mdsc = |
| 875 | &ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc; |
Sage Weil | f818a73 | 2010-05-11 20:56:31 -0700 | [diff] [blame] | 876 | int removed = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 877 | |
| 878 | dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode); |
| 879 | |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 880 | /* remove from session list */ |
| 881 | spin_lock(&session->s_cap_lock); |
| 882 | if (session->s_cap_iterator == cap) { |
| 883 | /* not yet, we are iterating over this very cap */ |
| 884 | dout("__ceph_remove_cap delaying %p removal from session %p\n", |
| 885 | cap, cap->session); |
| 886 | } else { |
| 887 | list_del_init(&cap->session_caps); |
| 888 | session->s_nr_caps--; |
| 889 | cap->session = NULL; |
Sage Weil | f818a73 | 2010-05-11 20:56:31 -0700 | [diff] [blame] | 890 | removed = 1; |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 891 | } |
Sage Weil | f818a73 | 2010-05-11 20:56:31 -0700 | [diff] [blame] | 892 | /* protect backpointer with s_cap_lock: see iterate_session_caps */ |
| 893 | cap->ci = NULL; |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 894 | spin_unlock(&session->s_cap_lock); |
| 895 | |
Sage Weil | f818a73 | 2010-05-11 20:56:31 -0700 | [diff] [blame] | 896 | /* remove from inode list */ |
| 897 | rb_erase(&cap->ci_node, &ci->i_caps); |
| 898 | if (ci->i_auth_cap == cap) |
| 899 | ci->i_auth_cap = NULL; |
| 900 | |
| 901 | if (removed) |
Yehuda Sadeh | 3715166 | 2010-06-17 16:16:12 -0700 | [diff] [blame] | 902 | ceph_put_cap(mdsc, cap); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 903 | |
| 904 | if (!__ceph_is_any_caps(ci) && ci->i_snap_realm) { |
| 905 | struct ceph_snap_realm *realm = ci->i_snap_realm; |
| 906 | spin_lock(&realm->inodes_with_caps_lock); |
| 907 | list_del_init(&ci->i_snap_realm_item); |
| 908 | ci->i_snap_realm_counter++; |
| 909 | ci->i_snap_realm = NULL; |
| 910 | spin_unlock(&realm->inodes_with_caps_lock); |
| 911 | ceph_put_snap_realm(mdsc, realm); |
| 912 | } |
| 913 | if (!__ceph_is_any_real_caps(ci)) |
| 914 | __cap_delay_cancel(mdsc, ci); |
| 915 | } |
| 916 | |
| 917 | /* |
| 918 | * Build and send a cap message to the given MDS. |
| 919 | * |
| 920 | * Caller should be holding s_mutex. |
| 921 | */ |
| 922 | static int send_cap_msg(struct ceph_mds_session *session, |
| 923 | u64 ino, u64 cid, int op, |
| 924 | int caps, int wanted, int dirty, |
| 925 | u32 seq, u64 flush_tid, u32 issue_seq, u32 mseq, |
| 926 | u64 size, u64 max_size, |
| 927 | struct timespec *mtime, struct timespec *atime, |
| 928 | u64 time_warp_seq, |
| 929 | uid_t uid, gid_t gid, mode_t mode, |
| 930 | u64 xattr_version, |
| 931 | struct ceph_buffer *xattrs_buf, |
| 932 | u64 follows) |
| 933 | { |
| 934 | struct ceph_mds_caps *fc; |
| 935 | struct ceph_msg *msg; |
| 936 | |
| 937 | dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s" |
| 938 | " seq %u/%u mseq %u follows %lld size %llu/%llu" |
| 939 | " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(op), |
| 940 | cid, ino, ceph_cap_string(caps), ceph_cap_string(wanted), |
| 941 | ceph_cap_string(dirty), |
| 942 | seq, issue_seq, mseq, follows, size, max_size, |
| 943 | xattr_version, xattrs_buf ? (int)xattrs_buf->vec.iov_len : 0); |
| 944 | |
Yehuda Sadeh | 34d2376 | 2010-04-06 14:33:58 -0700 | [diff] [blame] | 945 | msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc), GFP_NOFS); |
Sage Weil | a79832f | 2010-04-01 16:06:19 -0700 | [diff] [blame] | 946 | if (!msg) |
| 947 | return -ENOMEM; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 948 | |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 949 | msg->hdr.tid = cpu_to_le64(flush_tid); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 950 | |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 951 | fc = msg->front.iov_base; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 952 | memset(fc, 0, sizeof(*fc)); |
| 953 | |
| 954 | fc->cap_id = cpu_to_le64(cid); |
| 955 | fc->op = cpu_to_le32(op); |
| 956 | fc->seq = cpu_to_le32(seq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 957 | fc->issue_seq = cpu_to_le32(issue_seq); |
| 958 | fc->migrate_seq = cpu_to_le32(mseq); |
| 959 | fc->caps = cpu_to_le32(caps); |
| 960 | fc->wanted = cpu_to_le32(wanted); |
| 961 | fc->dirty = cpu_to_le32(dirty); |
| 962 | fc->ino = cpu_to_le64(ino); |
| 963 | fc->snap_follows = cpu_to_le64(follows); |
| 964 | |
| 965 | fc->size = cpu_to_le64(size); |
| 966 | fc->max_size = cpu_to_le64(max_size); |
| 967 | if (mtime) |
| 968 | ceph_encode_timespec(&fc->mtime, mtime); |
| 969 | if (atime) |
| 970 | ceph_encode_timespec(&fc->atime, atime); |
| 971 | fc->time_warp_seq = cpu_to_le32(time_warp_seq); |
| 972 | |
| 973 | fc->uid = cpu_to_le32(uid); |
| 974 | fc->gid = cpu_to_le32(gid); |
| 975 | fc->mode = cpu_to_le32(mode); |
| 976 | |
| 977 | fc->xattr_version = cpu_to_le64(xattr_version); |
| 978 | if (xattrs_buf) { |
| 979 | msg->middle = ceph_buffer_get(xattrs_buf); |
| 980 | fc->xattr_len = cpu_to_le32(xattrs_buf->vec.iov_len); |
| 981 | msg->hdr.middle_len = cpu_to_le32(xattrs_buf->vec.iov_len); |
| 982 | } |
| 983 | |
| 984 | ceph_con_send(&session->s_con, msg); |
| 985 | return 0; |
| 986 | } |
| 987 | |
Sage Weil | 3d7ded4 | 2010-06-09 16:47:10 -0700 | [diff] [blame] | 988 | static void __queue_cap_release(struct ceph_mds_session *session, |
| 989 | u64 ino, u64 cap_id, u32 migrate_seq, |
| 990 | u32 issue_seq) |
| 991 | { |
| 992 | struct ceph_msg *msg; |
| 993 | struct ceph_mds_cap_release *head; |
| 994 | struct ceph_mds_cap_item *item; |
| 995 | |
| 996 | spin_lock(&session->s_cap_lock); |
| 997 | BUG_ON(!session->s_num_cap_releases); |
| 998 | msg = list_first_entry(&session->s_cap_releases, |
| 999 | struct ceph_msg, list_head); |
| 1000 | |
| 1001 | dout(" adding %llx release to mds%d msg %p (%d left)\n", |
| 1002 | ino, session->s_mds, msg, session->s_num_cap_releases); |
| 1003 | |
| 1004 | BUG_ON(msg->front.iov_len + sizeof(*item) > PAGE_CACHE_SIZE); |
| 1005 | head = msg->front.iov_base; |
| 1006 | head->num = cpu_to_le32(le32_to_cpu(head->num) + 1); |
| 1007 | item = msg->front.iov_base + msg->front.iov_len; |
| 1008 | item->ino = cpu_to_le64(ino); |
| 1009 | item->cap_id = cpu_to_le64(cap_id); |
| 1010 | item->migrate_seq = cpu_to_le32(migrate_seq); |
| 1011 | item->seq = cpu_to_le32(issue_seq); |
| 1012 | |
| 1013 | session->s_num_cap_releases--; |
| 1014 | |
| 1015 | msg->front.iov_len += sizeof(*item); |
| 1016 | if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) { |
| 1017 | dout(" release msg %p full\n", msg); |
| 1018 | list_move_tail(&msg->list_head, &session->s_cap_releases_done); |
| 1019 | } else { |
| 1020 | dout(" release msg %p at %d/%d (%d)\n", msg, |
| 1021 | (int)le32_to_cpu(head->num), |
| 1022 | (int)CEPH_CAPS_PER_RELEASE, |
| 1023 | (int)msg->front.iov_len); |
| 1024 | } |
| 1025 | spin_unlock(&session->s_cap_lock); |
| 1026 | } |
| 1027 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1028 | /* |
Sage Weil | a636974 | 2010-02-22 13:59:00 -0800 | [diff] [blame] | 1029 | * Queue cap releases when an inode is dropped from our cache. Since |
| 1030 | * inode is about to be destroyed, there is no need for i_lock. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1031 | */ |
| 1032 | void ceph_queue_caps_release(struct inode *inode) |
| 1033 | { |
| 1034 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1035 | struct rb_node *p; |
| 1036 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1037 | p = rb_first(&ci->i_caps); |
| 1038 | while (p) { |
| 1039 | struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node); |
| 1040 | struct ceph_mds_session *session = cap->session; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1041 | |
Sage Weil | 3d7ded4 | 2010-06-09 16:47:10 -0700 | [diff] [blame] | 1042 | __queue_cap_release(session, ceph_ino(inode), cap->cap_id, |
| 1043 | cap->mseq, cap->issue_seq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1044 | p = rb_next(p); |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 1045 | __ceph_remove_cap(cap); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1046 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | /* |
| 1050 | * Send a cap msg on the given inode. Update our caps state, then |
| 1051 | * drop i_lock and send the message. |
| 1052 | * |
| 1053 | * Make note of max_size reported/requested from mds, revoked caps |
| 1054 | * that have now been implemented. |
| 1055 | * |
| 1056 | * Make half-hearted attempt ot to invalidate page cache if we are |
| 1057 | * dropping RDCACHE. Note that this will leave behind locked pages |
| 1058 | * that we'll then need to deal with elsewhere. |
| 1059 | * |
| 1060 | * Return non-zero if delayed release, or we experienced an error |
| 1061 | * such that the caller should requeue + retry later. |
| 1062 | * |
| 1063 | * called with i_lock, then drops it. |
| 1064 | * caller should hold snap_rwsem (read), s_mutex. |
| 1065 | */ |
| 1066 | static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap, |
| 1067 | int op, int used, int want, int retain, int flushing, |
| 1068 | unsigned *pflush_tid) |
| 1069 | __releases(cap->ci->vfs_inode->i_lock) |
| 1070 | { |
| 1071 | struct ceph_inode_info *ci = cap->ci; |
| 1072 | struct inode *inode = &ci->vfs_inode; |
| 1073 | u64 cap_id = cap->cap_id; |
Sage Weil | 68c2832 | 2010-02-09 13:41:47 -0800 | [diff] [blame] | 1074 | int held, revoking, dropping, keep; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1075 | u64 seq, issue_seq, mseq, time_warp_seq, follows; |
| 1076 | u64 size, max_size; |
| 1077 | struct timespec mtime, atime; |
| 1078 | int wake = 0; |
| 1079 | mode_t mode; |
| 1080 | uid_t uid; |
| 1081 | gid_t gid; |
| 1082 | struct ceph_mds_session *session; |
| 1083 | u64 xattr_version = 0; |
| 1084 | int delayed = 0; |
| 1085 | u64 flush_tid = 0; |
| 1086 | int i; |
| 1087 | int ret; |
| 1088 | |
Sage Weil | 68c2832 | 2010-02-09 13:41:47 -0800 | [diff] [blame] | 1089 | held = cap->issued | cap->implemented; |
| 1090 | revoking = cap->implemented & ~cap->issued; |
| 1091 | retain &= ~revoking; |
| 1092 | dropping = cap->issued & ~retain; |
| 1093 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1094 | dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n", |
| 1095 | inode, cap, cap->session, |
| 1096 | ceph_cap_string(held), ceph_cap_string(held & retain), |
| 1097 | ceph_cap_string(revoking)); |
| 1098 | BUG_ON((retain & CEPH_CAP_PIN) == 0); |
| 1099 | |
| 1100 | session = cap->session; |
| 1101 | |
| 1102 | /* don't release wanted unless we've waited a bit. */ |
| 1103 | if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 && |
| 1104 | time_before(jiffies, ci->i_hold_caps_min)) { |
| 1105 | dout(" delaying issued %s -> %s, wanted %s -> %s on send\n", |
| 1106 | ceph_cap_string(cap->issued), |
| 1107 | ceph_cap_string(cap->issued & retain), |
| 1108 | ceph_cap_string(cap->mds_wanted), |
| 1109 | ceph_cap_string(want)); |
| 1110 | want |= cap->mds_wanted; |
| 1111 | retain |= cap->issued; |
| 1112 | delayed = 1; |
| 1113 | } |
| 1114 | ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH); |
| 1115 | |
| 1116 | cap->issued &= retain; /* drop bits we don't want */ |
| 1117 | if (cap->implemented & ~cap->issued) { |
| 1118 | /* |
| 1119 | * Wake up any waiters on wanted -> needed transition. |
| 1120 | * This is due to the weird transition from buffered |
| 1121 | * to sync IO... we need to flush dirty pages _before_ |
| 1122 | * allowing sync writes to avoid reordering. |
| 1123 | */ |
| 1124 | wake = 1; |
| 1125 | } |
| 1126 | cap->implemented &= cap->issued | used; |
| 1127 | cap->mds_wanted = want; |
| 1128 | |
| 1129 | if (flushing) { |
| 1130 | /* |
| 1131 | * assign a tid for flush operations so we can avoid |
| 1132 | * flush1 -> dirty1 -> flush2 -> flushack1 -> mark |
| 1133 | * clean type races. track latest tid for every bit |
| 1134 | * so we can handle flush AxFw, flush Fw, and have the |
| 1135 | * first ack clean Ax. |
| 1136 | */ |
| 1137 | flush_tid = ++ci->i_cap_flush_last_tid; |
| 1138 | if (pflush_tid) |
| 1139 | *pflush_tid = flush_tid; |
| 1140 | dout(" cap_flush_tid %d\n", (int)flush_tid); |
| 1141 | for (i = 0; i < CEPH_CAP_BITS; i++) |
| 1142 | if (flushing & (1 << i)) |
| 1143 | ci->i_cap_flush_tid[i] = flush_tid; |
| 1144 | } |
| 1145 | |
| 1146 | keep = cap->implemented; |
| 1147 | seq = cap->seq; |
| 1148 | issue_seq = cap->issue_seq; |
| 1149 | mseq = cap->mseq; |
| 1150 | size = inode->i_size; |
| 1151 | ci->i_reported_size = size; |
| 1152 | max_size = ci->i_wanted_max_size; |
| 1153 | ci->i_requested_max_size = max_size; |
| 1154 | mtime = inode->i_mtime; |
| 1155 | atime = inode->i_atime; |
| 1156 | time_warp_seq = ci->i_time_warp_seq; |
| 1157 | follows = ci->i_snap_realm->cached_context->seq; |
| 1158 | uid = inode->i_uid; |
| 1159 | gid = inode->i_gid; |
| 1160 | mode = inode->i_mode; |
| 1161 | |
| 1162 | if (dropping & CEPH_CAP_XATTR_EXCL) { |
| 1163 | __ceph_build_xattrs_blob(ci); |
| 1164 | xattr_version = ci->i_xattrs.version + 1; |
| 1165 | } |
| 1166 | |
| 1167 | spin_unlock(&inode->i_lock); |
| 1168 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1169 | ret = send_cap_msg(session, ceph_vino(inode).ino, cap_id, |
| 1170 | op, keep, want, flushing, seq, flush_tid, issue_seq, mseq, |
| 1171 | size, max_size, &mtime, &atime, time_warp_seq, |
| 1172 | uid, gid, mode, |
| 1173 | xattr_version, |
| 1174 | (flushing & CEPH_CAP_XATTR_EXCL) ? ci->i_xattrs.blob : NULL, |
| 1175 | follows); |
| 1176 | if (ret < 0) { |
| 1177 | dout("error sending cap msg, must requeue %p\n", inode); |
| 1178 | delayed = 1; |
| 1179 | } |
| 1180 | |
| 1181 | if (wake) |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 1182 | wake_up_all(&ci->i_cap_wq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1183 | |
| 1184 | return delayed; |
| 1185 | } |
| 1186 | |
| 1187 | /* |
| 1188 | * When a snapshot is taken, clients accumulate dirty metadata on |
| 1189 | * inodes with capabilities in ceph_cap_snaps to describe the file |
| 1190 | * state at the time the snapshot was taken. This must be flushed |
| 1191 | * asynchronously back to the MDS once sync writes complete and dirty |
| 1192 | * data is written out. |
| 1193 | * |
| 1194 | * Called under i_lock. Takes s_mutex as needed. |
| 1195 | */ |
| 1196 | void __ceph_flush_snaps(struct ceph_inode_info *ci, |
| 1197 | struct ceph_mds_session **psession) |
Yehuda Sadeh | cd84db6 | 2010-06-11 16:58:48 -0700 | [diff] [blame] | 1198 | __releases(ci->vfs_inode->i_lock) |
| 1199 | __acquires(ci->vfs_inode->i_lock) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1200 | { |
| 1201 | struct inode *inode = &ci->vfs_inode; |
| 1202 | int mds; |
| 1203 | struct ceph_cap_snap *capsnap; |
| 1204 | u32 mseq; |
| 1205 | struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc; |
| 1206 | struct ceph_mds_session *session = NULL; /* if session != NULL, we hold |
| 1207 | session->s_mutex */ |
| 1208 | u64 next_follows = 0; /* keep track of how far we've gotten through the |
| 1209 | i_cap_snaps list, and skip these entries next time |
| 1210 | around to avoid an infinite loop */ |
| 1211 | |
| 1212 | if (psession) |
| 1213 | session = *psession; |
| 1214 | |
| 1215 | dout("__flush_snaps %p\n", inode); |
| 1216 | retry: |
| 1217 | list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { |
| 1218 | /* avoid an infiniute loop after retry */ |
| 1219 | if (capsnap->follows < next_follows) |
| 1220 | continue; |
| 1221 | /* |
| 1222 | * we need to wait for sync writes to complete and for dirty |
| 1223 | * pages to be written out. |
| 1224 | */ |
| 1225 | if (capsnap->dirty_pages || capsnap->writing) |
| 1226 | continue; |
| 1227 | |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 1228 | /* |
| 1229 | * if cap writeback already occurred, we should have dropped |
| 1230 | * the capsnap in ceph_put_wrbuffer_cap_refs. |
| 1231 | */ |
| 1232 | BUG_ON(capsnap->dirty == 0); |
| 1233 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1234 | /* pick mds, take s_mutex */ |
Sage Weil | ca81f3f | 2010-06-10 14:21:36 -0700 | [diff] [blame] | 1235 | if (ci->i_auth_cap == NULL) { |
| 1236 | dout("no auth cap (migrating?), doing nothing\n"); |
| 1237 | goto out; |
| 1238 | } |
| 1239 | mds = ci->i_auth_cap->session->s_mds; |
| 1240 | mseq = ci->i_auth_cap->mseq; |
| 1241 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1242 | if (session && session->s_mds != mds) { |
| 1243 | dout("oops, wrong session %p mutex\n", session); |
| 1244 | mutex_unlock(&session->s_mutex); |
| 1245 | ceph_put_mds_session(session); |
| 1246 | session = NULL; |
| 1247 | } |
| 1248 | if (!session) { |
| 1249 | spin_unlock(&inode->i_lock); |
| 1250 | mutex_lock(&mdsc->mutex); |
| 1251 | session = __ceph_lookup_mds_session(mdsc, mds); |
| 1252 | mutex_unlock(&mdsc->mutex); |
| 1253 | if (session) { |
| 1254 | dout("inverting session/ino locks on %p\n", |
| 1255 | session); |
| 1256 | mutex_lock(&session->s_mutex); |
| 1257 | } |
| 1258 | /* |
| 1259 | * if session == NULL, we raced against a cap |
Sage Weil | ca81f3f | 2010-06-10 14:21:36 -0700 | [diff] [blame] | 1260 | * deletion or migration. retry, and we'll |
| 1261 | * get a better @mds value next time. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1262 | */ |
| 1263 | spin_lock(&inode->i_lock); |
| 1264 | goto retry; |
| 1265 | } |
| 1266 | |
| 1267 | capsnap->flush_tid = ++ci->i_cap_flush_last_tid; |
| 1268 | atomic_inc(&capsnap->nref); |
| 1269 | if (!list_empty(&capsnap->flushing_item)) |
| 1270 | list_del_init(&capsnap->flushing_item); |
| 1271 | list_add_tail(&capsnap->flushing_item, |
| 1272 | &session->s_cap_snaps_flushing); |
| 1273 | spin_unlock(&inode->i_lock); |
| 1274 | |
| 1275 | dout("flush_snaps %p cap_snap %p follows %lld size %llu\n", |
| 1276 | inode, capsnap, next_follows, capsnap->size); |
| 1277 | send_cap_msg(session, ceph_vino(inode).ino, 0, |
| 1278 | CEPH_CAP_OP_FLUSHSNAP, capsnap->issued, 0, |
| 1279 | capsnap->dirty, 0, capsnap->flush_tid, 0, mseq, |
| 1280 | capsnap->size, 0, |
| 1281 | &capsnap->mtime, &capsnap->atime, |
| 1282 | capsnap->time_warp_seq, |
| 1283 | capsnap->uid, capsnap->gid, capsnap->mode, |
| 1284 | 0, NULL, |
| 1285 | capsnap->follows); |
| 1286 | |
| 1287 | next_follows = capsnap->follows + 1; |
| 1288 | ceph_put_cap_snap(capsnap); |
| 1289 | |
| 1290 | spin_lock(&inode->i_lock); |
| 1291 | goto retry; |
| 1292 | } |
| 1293 | |
| 1294 | /* we flushed them all; remove this inode from the queue */ |
| 1295 | spin_lock(&mdsc->snap_flush_lock); |
| 1296 | list_del_init(&ci->i_snap_flush_item); |
| 1297 | spin_unlock(&mdsc->snap_flush_lock); |
| 1298 | |
Sage Weil | ca81f3f | 2010-06-10 14:21:36 -0700 | [diff] [blame] | 1299 | out: |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1300 | if (psession) |
| 1301 | *psession = session; |
| 1302 | else if (session) { |
| 1303 | mutex_unlock(&session->s_mutex); |
| 1304 | ceph_put_mds_session(session); |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | static void ceph_flush_snaps(struct ceph_inode_info *ci) |
| 1309 | { |
| 1310 | struct inode *inode = &ci->vfs_inode; |
| 1311 | |
| 1312 | spin_lock(&inode->i_lock); |
| 1313 | __ceph_flush_snaps(ci, NULL); |
| 1314 | spin_unlock(&inode->i_lock); |
| 1315 | } |
| 1316 | |
| 1317 | /* |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1318 | * Mark caps dirty. If inode is newly dirty, add to the global dirty |
| 1319 | * list. |
| 1320 | */ |
| 1321 | void __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask) |
| 1322 | { |
Cheng Renquan | 640ef79 | 2010-03-26 17:40:33 +0800 | [diff] [blame] | 1323 | struct ceph_mds_client *mdsc = |
| 1324 | &ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc; |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 1325 | struct inode *inode = &ci->vfs_inode; |
| 1326 | int was = ci->i_dirty_caps; |
| 1327 | int dirty = 0; |
| 1328 | |
| 1329 | dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode, |
| 1330 | ceph_cap_string(mask), ceph_cap_string(was), |
| 1331 | ceph_cap_string(was | mask)); |
| 1332 | ci->i_dirty_caps |= mask; |
| 1333 | if (was == 0) { |
| 1334 | dout(" inode %p now dirty\n", &ci->vfs_inode); |
| 1335 | BUG_ON(!list_empty(&ci->i_dirty_item)); |
| 1336 | spin_lock(&mdsc->cap_dirty_lock); |
| 1337 | list_add(&ci->i_dirty_item, &mdsc->cap_dirty); |
| 1338 | spin_unlock(&mdsc->cap_dirty_lock); |
| 1339 | if (ci->i_flushing_caps == 0) { |
| 1340 | igrab(inode); |
| 1341 | dirty |= I_DIRTY_SYNC; |
| 1342 | } |
| 1343 | } |
| 1344 | BUG_ON(list_empty(&ci->i_dirty_item)); |
| 1345 | if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) && |
| 1346 | (mask & CEPH_CAP_FILE_BUFFER)) |
| 1347 | dirty |= I_DIRTY_DATASYNC; |
| 1348 | if (dirty) |
| 1349 | __mark_inode_dirty(inode, dirty); |
| 1350 | __cap_delay_requeue(mdsc, ci); |
| 1351 | } |
| 1352 | |
| 1353 | /* |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1354 | * Add dirty inode to the flushing list. Assigned a seq number so we |
| 1355 | * can wait for caps to flush without starving. |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1356 | * |
| 1357 | * Called under i_lock. |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1358 | */ |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1359 | static int __mark_caps_flushing(struct inode *inode, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1360 | struct ceph_mds_session *session) |
| 1361 | { |
Cheng Renquan | 640ef79 | 2010-03-26 17:40:33 +0800 | [diff] [blame] | 1362 | 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] | 1363 | struct ceph_inode_info *ci = ceph_inode(inode); |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1364 | int flushing; |
Sage Weil | 50b885b | 2009-12-01 14:12:07 -0800 | [diff] [blame] | 1365 | |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1366 | BUG_ON(ci->i_dirty_caps == 0); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1367 | BUG_ON(list_empty(&ci->i_dirty_item)); |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1368 | |
| 1369 | flushing = ci->i_dirty_caps; |
| 1370 | dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n", |
| 1371 | ceph_cap_string(flushing), |
| 1372 | ceph_cap_string(ci->i_flushing_caps), |
| 1373 | ceph_cap_string(ci->i_flushing_caps | flushing)); |
| 1374 | ci->i_flushing_caps |= flushing; |
| 1375 | ci->i_dirty_caps = 0; |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 1376 | dout(" inode %p now !dirty\n", inode); |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1377 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1378 | spin_lock(&mdsc->cap_dirty_lock); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 1379 | list_del_init(&ci->i_dirty_item); |
| 1380 | |
| 1381 | ci->i_cap_flush_seq = ++mdsc->cap_flush_seq; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1382 | if (list_empty(&ci->i_flushing_item)) { |
| 1383 | list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing); |
| 1384 | mdsc->num_cap_flushing++; |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 1385 | dout(" inode %p now flushing seq %lld\n", inode, |
| 1386 | ci->i_cap_flush_seq); |
| 1387 | } else { |
| 1388 | list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing); |
| 1389 | dout(" inode %p now flushing (more) seq %lld\n", inode, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1390 | ci->i_cap_flush_seq); |
| 1391 | } |
| 1392 | spin_unlock(&mdsc->cap_dirty_lock); |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1393 | |
| 1394 | return flushing; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | /* |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 1398 | * try to invalidate mapping pages without blocking. |
| 1399 | */ |
| 1400 | static int mapping_is_empty(struct address_space *mapping) |
| 1401 | { |
| 1402 | struct page *page = find_get_page(mapping, 0); |
| 1403 | |
| 1404 | if (!page) |
| 1405 | return 1; |
| 1406 | |
| 1407 | put_page(page); |
| 1408 | return 0; |
| 1409 | } |
| 1410 | |
| 1411 | static int try_nonblocking_invalidate(struct inode *inode) |
| 1412 | { |
| 1413 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1414 | u32 invalidating_gen = ci->i_rdcache_gen; |
| 1415 | |
| 1416 | spin_unlock(&inode->i_lock); |
| 1417 | invalidate_mapping_pages(&inode->i_data, 0, -1); |
| 1418 | spin_lock(&inode->i_lock); |
| 1419 | |
| 1420 | if (mapping_is_empty(&inode->i_data) && |
| 1421 | invalidating_gen == ci->i_rdcache_gen) { |
| 1422 | /* success. */ |
| 1423 | dout("try_nonblocking_invalidate %p success\n", inode); |
| 1424 | ci->i_rdcache_gen = 0; |
| 1425 | ci->i_rdcache_revoking = 0; |
| 1426 | return 0; |
| 1427 | } |
| 1428 | dout("try_nonblocking_invalidate %p failed\n", inode); |
| 1429 | return -1; |
| 1430 | } |
| 1431 | |
| 1432 | /* |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1433 | * Swiss army knife function to examine currently used and wanted |
| 1434 | * versus held caps. Release, flush, ack revoked caps to mds as |
| 1435 | * appropriate. |
| 1436 | * |
| 1437 | * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay |
| 1438 | * cap release further. |
| 1439 | * CHECK_CAPS_AUTHONLY - we should only check the auth cap |
| 1440 | * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without |
| 1441 | * further delay. |
| 1442 | */ |
| 1443 | void ceph_check_caps(struct ceph_inode_info *ci, int flags, |
| 1444 | struct ceph_mds_session *session) |
| 1445 | { |
| 1446 | struct ceph_client *client = ceph_inode_to_client(&ci->vfs_inode); |
| 1447 | struct ceph_mds_client *mdsc = &client->mdsc; |
| 1448 | struct inode *inode = &ci->vfs_inode; |
| 1449 | struct ceph_cap *cap; |
| 1450 | int file_wanted, used; |
| 1451 | int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */ |
Sage Weil | cbd0363 | 2010-02-09 13:41:18 -0800 | [diff] [blame] | 1452 | int issued, implemented, want, retain, revoking, flushing = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1453 | int mds = -1; /* keep track of how far we've gone through i_caps list |
| 1454 | to avoid an infinite loop on retry */ |
| 1455 | struct rb_node *p; |
| 1456 | int tried_invalidate = 0; |
| 1457 | int delayed = 0, sent = 0, force_requeue = 0, num; |
Sage Weil | cbd0363 | 2010-02-09 13:41:18 -0800 | [diff] [blame] | 1458 | int queue_invalidate = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1459 | int is_delayed = flags & CHECK_CAPS_NODELAY; |
| 1460 | |
| 1461 | /* if we are unmounting, flush any unused caps immediately. */ |
| 1462 | if (mdsc->stopping) |
| 1463 | is_delayed = 1; |
| 1464 | |
| 1465 | spin_lock(&inode->i_lock); |
| 1466 | |
| 1467 | if (ci->i_ceph_flags & CEPH_I_FLUSH) |
| 1468 | flags |= CHECK_CAPS_FLUSH; |
| 1469 | |
| 1470 | /* flush snaps first time around only */ |
| 1471 | if (!list_empty(&ci->i_cap_snaps)) |
| 1472 | __ceph_flush_snaps(ci, &session); |
| 1473 | goto retry_locked; |
| 1474 | retry: |
| 1475 | spin_lock(&inode->i_lock); |
| 1476 | retry_locked: |
| 1477 | file_wanted = __ceph_caps_file_wanted(ci); |
| 1478 | used = __ceph_caps_used(ci); |
| 1479 | want = file_wanted | used; |
Sage Weil | cbd0363 | 2010-02-09 13:41:18 -0800 | [diff] [blame] | 1480 | issued = __ceph_caps_issued(ci, &implemented); |
| 1481 | revoking = implemented & ~issued; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1482 | |
| 1483 | retain = want | CEPH_CAP_PIN; |
| 1484 | if (!mdsc->stopping && inode->i_nlink > 0) { |
| 1485 | if (want) { |
| 1486 | retain |= CEPH_CAP_ANY; /* be greedy */ |
| 1487 | } else { |
| 1488 | retain |= CEPH_CAP_ANY_SHARED; |
| 1489 | /* |
| 1490 | * keep RD only if we didn't have the file open RW, |
| 1491 | * because then the mds would revoke it anyway to |
| 1492 | * journal max_size=0. |
| 1493 | */ |
| 1494 | if (ci->i_max_size == 0) |
| 1495 | retain |= CEPH_CAP_ANY_RD; |
| 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | 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] | 1500 | " issued %s revoking %s retain %s %s%s%s\n", inode, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1501 | ceph_cap_string(file_wanted), |
| 1502 | ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps), |
| 1503 | ceph_cap_string(ci->i_flushing_caps), |
Sage Weil | cbd0363 | 2010-02-09 13:41:18 -0800 | [diff] [blame] | 1504 | ceph_cap_string(issued), ceph_cap_string(revoking), |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1505 | ceph_cap_string(retain), |
| 1506 | (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "", |
| 1507 | (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "", |
| 1508 | (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : ""); |
| 1509 | |
| 1510 | /* |
| 1511 | * If we no longer need to hold onto old our caps, and we may |
| 1512 | * have cached pages, but don't want them, then try to invalidate. |
| 1513 | * If we fail, it's because pages are locked.... try again later. |
| 1514 | */ |
| 1515 | if ((!is_delayed || mdsc->stopping) && |
| 1516 | ci->i_wrbuffer_ref == 0 && /* no dirty pages... */ |
| 1517 | ci->i_rdcache_gen && /* may have cached pages */ |
Sage Weil | cbd0363 | 2010-02-09 13:41:18 -0800 | [diff] [blame] | 1518 | (file_wanted == 0 || /* no open files */ |
Sage Weil | 2962507 | 2010-05-27 10:40:43 -0700 | [diff] [blame] | 1519 | (revoking & (CEPH_CAP_FILE_CACHE| |
| 1520 | CEPH_CAP_FILE_LAZYIO))) && /* or revoking cache */ |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1521 | !tried_invalidate) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1522 | dout("check_caps trying to invalidate on %p\n", inode); |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 1523 | if (try_nonblocking_invalidate(inode) < 0) { |
Sage Weil | 2962507 | 2010-05-27 10:40:43 -0700 | [diff] [blame] | 1524 | if (revoking & (CEPH_CAP_FILE_CACHE| |
| 1525 | CEPH_CAP_FILE_LAZYIO)) { |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 1526 | dout("check_caps queuing invalidate\n"); |
| 1527 | queue_invalidate = 1; |
| 1528 | ci->i_rdcache_revoking = ci->i_rdcache_gen; |
| 1529 | } else { |
| 1530 | dout("check_caps failed to invalidate pages\n"); |
| 1531 | /* we failed to invalidate pages. check these |
| 1532 | caps again later. */ |
| 1533 | force_requeue = 1; |
| 1534 | __cap_set_timeouts(mdsc, ci); |
| 1535 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1536 | } |
| 1537 | tried_invalidate = 1; |
| 1538 | goto retry_locked; |
| 1539 | } |
| 1540 | |
| 1541 | num = 0; |
| 1542 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 1543 | cap = rb_entry(p, struct ceph_cap, ci_node); |
| 1544 | num++; |
| 1545 | |
| 1546 | /* avoid looping forever */ |
| 1547 | if (mds >= cap->mds || |
| 1548 | ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap)) |
| 1549 | continue; |
| 1550 | |
| 1551 | /* NOTE: no side-effects allowed, until we take s_mutex */ |
| 1552 | |
| 1553 | revoking = cap->implemented & ~cap->issued; |
| 1554 | if (revoking) |
Sage Weil | cbd0363 | 2010-02-09 13:41:18 -0800 | [diff] [blame] | 1555 | dout(" mds%d revoking %s\n", cap->mds, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1556 | ceph_cap_string(revoking)); |
| 1557 | |
| 1558 | if (cap == ci->i_auth_cap && |
| 1559 | (cap->issued & CEPH_CAP_FILE_WR)) { |
| 1560 | /* request larger max_size from MDS? */ |
| 1561 | if (ci->i_wanted_max_size > ci->i_max_size && |
| 1562 | ci->i_wanted_max_size > ci->i_requested_max_size) { |
| 1563 | dout("requesting new max_size\n"); |
| 1564 | goto ack; |
| 1565 | } |
| 1566 | |
| 1567 | /* approaching file_max? */ |
| 1568 | if ((inode->i_size << 1) >= ci->i_max_size && |
| 1569 | (ci->i_reported_size << 1) < ci->i_max_size) { |
| 1570 | dout("i_size approaching max_size\n"); |
| 1571 | goto ack; |
| 1572 | } |
| 1573 | } |
| 1574 | /* flush anything dirty? */ |
| 1575 | if (cap == ci->i_auth_cap && (flags & CHECK_CAPS_FLUSH) && |
| 1576 | ci->i_dirty_caps) { |
| 1577 | dout("flushing dirty caps\n"); |
| 1578 | goto ack; |
| 1579 | } |
| 1580 | |
| 1581 | /* completed revocation? going down and there are no caps? */ |
| 1582 | if (revoking && (revoking & used) == 0) { |
| 1583 | dout("completed revocation of %s\n", |
| 1584 | ceph_cap_string(cap->implemented & ~cap->issued)); |
| 1585 | goto ack; |
| 1586 | } |
| 1587 | |
| 1588 | /* want more caps from mds? */ |
| 1589 | if (want & ~(cap->mds_wanted | cap->issued)) |
| 1590 | goto ack; |
| 1591 | |
| 1592 | /* things we might delay */ |
| 1593 | if ((cap->issued & ~retain) == 0 && |
| 1594 | cap->mds_wanted == want) |
| 1595 | continue; /* nope, all good */ |
| 1596 | |
| 1597 | if (is_delayed) |
| 1598 | goto ack; |
| 1599 | |
| 1600 | /* delay? */ |
| 1601 | if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 && |
| 1602 | time_before(jiffies, ci->i_hold_caps_max)) { |
| 1603 | dout(" delaying issued %s -> %s, wanted %s -> %s\n", |
| 1604 | ceph_cap_string(cap->issued), |
| 1605 | ceph_cap_string(cap->issued & retain), |
| 1606 | ceph_cap_string(cap->mds_wanted), |
| 1607 | ceph_cap_string(want)); |
| 1608 | delayed++; |
| 1609 | continue; |
| 1610 | } |
| 1611 | |
| 1612 | ack: |
Sage Weil | e9964c1 | 2010-03-01 15:16:56 -0800 | [diff] [blame] | 1613 | if (ci->i_ceph_flags & CEPH_I_NOFLUSH) { |
| 1614 | dout(" skipping %p I_NOFLUSH set\n", inode); |
| 1615 | continue; |
| 1616 | } |
| 1617 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1618 | if (session && session != cap->session) { |
| 1619 | dout("oops, wrong session %p mutex\n", session); |
| 1620 | mutex_unlock(&session->s_mutex); |
| 1621 | session = NULL; |
| 1622 | } |
| 1623 | if (!session) { |
| 1624 | session = cap->session; |
| 1625 | if (mutex_trylock(&session->s_mutex) == 0) { |
| 1626 | dout("inverting session/ino locks on %p\n", |
| 1627 | session); |
| 1628 | spin_unlock(&inode->i_lock); |
| 1629 | if (took_snap_rwsem) { |
| 1630 | up_read(&mdsc->snap_rwsem); |
| 1631 | took_snap_rwsem = 0; |
| 1632 | } |
| 1633 | mutex_lock(&session->s_mutex); |
| 1634 | goto retry; |
| 1635 | } |
| 1636 | } |
| 1637 | /* take snap_rwsem after session mutex */ |
| 1638 | if (!took_snap_rwsem) { |
| 1639 | if (down_read_trylock(&mdsc->snap_rwsem) == 0) { |
| 1640 | dout("inverting snap/in locks on %p\n", |
| 1641 | inode); |
| 1642 | spin_unlock(&inode->i_lock); |
| 1643 | down_read(&mdsc->snap_rwsem); |
| 1644 | took_snap_rwsem = 1; |
| 1645 | goto retry; |
| 1646 | } |
| 1647 | took_snap_rwsem = 1; |
| 1648 | } |
| 1649 | |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1650 | if (cap == ci->i_auth_cap && ci->i_dirty_caps) |
| 1651 | flushing = __mark_caps_flushing(inode, session); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1652 | |
| 1653 | mds = cap->mds; /* remember mds, so we don't repeat */ |
| 1654 | sent++; |
| 1655 | |
| 1656 | /* __send_cap drops i_lock */ |
| 1657 | delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, used, want, |
| 1658 | retain, flushing, NULL); |
| 1659 | goto retry; /* retake i_lock and restart our cap scan. */ |
| 1660 | } |
| 1661 | |
| 1662 | /* |
| 1663 | * Reschedule delayed caps release if we delayed anything, |
| 1664 | * otherwise cancel. |
| 1665 | */ |
| 1666 | if (delayed && is_delayed) |
| 1667 | force_requeue = 1; /* __send_cap delayed release; requeue */ |
| 1668 | if (!delayed && !is_delayed) |
| 1669 | __cap_delay_cancel(mdsc, ci); |
| 1670 | else if (!is_delayed || force_requeue) |
| 1671 | __cap_delay_requeue(mdsc, ci); |
| 1672 | |
| 1673 | spin_unlock(&inode->i_lock); |
| 1674 | |
Sage Weil | cbd0363 | 2010-02-09 13:41:18 -0800 | [diff] [blame] | 1675 | if (queue_invalidate) |
Sage Weil | 3c6f6b7 | 2010-02-09 15:24:44 -0800 | [diff] [blame] | 1676 | ceph_queue_invalidate(inode); |
Sage Weil | cbd0363 | 2010-02-09 13:41:18 -0800 | [diff] [blame] | 1677 | |
Sage Weil | cdc2ce0 | 2010-03-16 13:39:28 -0700 | [diff] [blame] | 1678 | if (session) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1679 | mutex_unlock(&session->s_mutex); |
| 1680 | if (took_snap_rwsem) |
| 1681 | up_read(&mdsc->snap_rwsem); |
| 1682 | } |
| 1683 | |
| 1684 | /* |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1685 | * Try to flush dirty caps back to the auth mds. |
| 1686 | */ |
| 1687 | static int try_flush_caps(struct inode *inode, struct ceph_mds_session *session, |
| 1688 | unsigned *flush_tid) |
| 1689 | { |
Cheng Renquan | 640ef79 | 2010-03-26 17:40:33 +0800 | [diff] [blame] | 1690 | 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] | 1691 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1692 | int unlock_session = session ? 0 : 1; |
| 1693 | int flushing = 0; |
| 1694 | |
| 1695 | retry: |
| 1696 | spin_lock(&inode->i_lock); |
Sage Weil | e9964c1 | 2010-03-01 15:16:56 -0800 | [diff] [blame] | 1697 | if (ci->i_ceph_flags & CEPH_I_NOFLUSH) { |
| 1698 | dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode); |
| 1699 | goto out; |
| 1700 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1701 | if (ci->i_dirty_caps && ci->i_auth_cap) { |
| 1702 | struct ceph_cap *cap = ci->i_auth_cap; |
| 1703 | int used = __ceph_caps_used(ci); |
| 1704 | int want = __ceph_caps_wanted(ci); |
| 1705 | int delayed; |
| 1706 | |
| 1707 | if (!session) { |
| 1708 | spin_unlock(&inode->i_lock); |
| 1709 | session = cap->session; |
| 1710 | mutex_lock(&session->s_mutex); |
| 1711 | goto retry; |
| 1712 | } |
| 1713 | BUG_ON(session != cap->session); |
| 1714 | if (cap->session->s_state < CEPH_MDS_SESSION_OPEN) |
| 1715 | goto out; |
| 1716 | |
Sage Weil | cdc35f9 | 2009-10-14 14:24:19 -0700 | [diff] [blame] | 1717 | flushing = __mark_caps_flushing(inode, session); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1718 | |
| 1719 | /* __send_cap drops i_lock */ |
| 1720 | delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, used, want, |
| 1721 | cap->issued | cap->implemented, flushing, |
| 1722 | flush_tid); |
| 1723 | if (!delayed) |
| 1724 | goto out_unlocked; |
| 1725 | |
| 1726 | spin_lock(&inode->i_lock); |
| 1727 | __cap_delay_requeue(mdsc, ci); |
| 1728 | } |
| 1729 | out: |
| 1730 | spin_unlock(&inode->i_lock); |
| 1731 | out_unlocked: |
| 1732 | if (session && unlock_session) |
| 1733 | mutex_unlock(&session->s_mutex); |
| 1734 | return flushing; |
| 1735 | } |
| 1736 | |
| 1737 | /* |
| 1738 | * Return true if we've flushed caps through the given flush_tid. |
| 1739 | */ |
| 1740 | static int caps_are_flushed(struct inode *inode, unsigned tid) |
| 1741 | { |
| 1742 | struct ceph_inode_info *ci = ceph_inode(inode); |
Dan Carpenter | a5ee751 | 2010-05-07 10:27:14 +0200 | [diff] [blame] | 1743 | int i, ret = 1; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1744 | |
| 1745 | spin_lock(&inode->i_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1746 | for (i = 0; i < CEPH_CAP_BITS; i++) |
| 1747 | if ((ci->i_flushing_caps & (1 << i)) && |
| 1748 | ci->i_cap_flush_tid[i] <= tid) { |
| 1749 | /* still flushing this bit */ |
| 1750 | ret = 0; |
| 1751 | break; |
| 1752 | } |
| 1753 | spin_unlock(&inode->i_lock); |
| 1754 | return ret; |
| 1755 | } |
| 1756 | |
| 1757 | /* |
| 1758 | * Wait on any unsafe replies for the given inode. First wait on the |
| 1759 | * newest request, and make that the upper bound. Then, if there are |
| 1760 | * more requests, keep waiting on the oldest as long as it is still older |
| 1761 | * than the original request. |
| 1762 | */ |
| 1763 | static void sync_write_wait(struct inode *inode) |
| 1764 | { |
| 1765 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1766 | struct list_head *head = &ci->i_unsafe_writes; |
| 1767 | struct ceph_osd_request *req; |
| 1768 | u64 last_tid; |
| 1769 | |
| 1770 | spin_lock(&ci->i_unsafe_lock); |
| 1771 | if (list_empty(head)) |
| 1772 | goto out; |
| 1773 | |
| 1774 | /* set upper bound as _last_ entry in chain */ |
| 1775 | req = list_entry(head->prev, struct ceph_osd_request, |
| 1776 | r_unsafe_item); |
| 1777 | last_tid = req->r_tid; |
| 1778 | |
| 1779 | do { |
| 1780 | ceph_osdc_get_request(req); |
| 1781 | spin_unlock(&ci->i_unsafe_lock); |
| 1782 | dout("sync_write_wait on tid %llu (until %llu)\n", |
| 1783 | req->r_tid, last_tid); |
| 1784 | wait_for_completion(&req->r_safe_completion); |
| 1785 | spin_lock(&ci->i_unsafe_lock); |
| 1786 | ceph_osdc_put_request(req); |
| 1787 | |
| 1788 | /* |
| 1789 | * from here on look at first entry in chain, since we |
| 1790 | * only want to wait for anything older than last_tid |
| 1791 | */ |
| 1792 | if (list_empty(head)) |
| 1793 | break; |
| 1794 | req = list_entry(head->next, struct ceph_osd_request, |
| 1795 | r_unsafe_item); |
| 1796 | } while (req->r_tid < last_tid); |
| 1797 | out: |
| 1798 | spin_unlock(&ci->i_unsafe_lock); |
| 1799 | } |
| 1800 | |
Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 1801 | int ceph_fsync(struct file *file, int datasync) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1802 | { |
Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 1803 | struct inode *inode = file->f_mapping->host; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1804 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1805 | unsigned flush_tid; |
| 1806 | int ret; |
| 1807 | int dirty; |
| 1808 | |
| 1809 | dout("fsync %p%s\n", inode, datasync ? " datasync" : ""); |
| 1810 | sync_write_wait(inode); |
| 1811 | |
| 1812 | ret = filemap_write_and_wait(inode->i_mapping); |
| 1813 | if (ret < 0) |
| 1814 | return ret; |
| 1815 | |
| 1816 | dirty = try_flush_caps(inode, NULL, &flush_tid); |
| 1817 | dout("fsync dirty caps are %s\n", ceph_cap_string(dirty)); |
| 1818 | |
| 1819 | /* |
| 1820 | * only wait on non-file metadata writeback (the mds |
| 1821 | * can recover size and mtime, so we don't need to |
| 1822 | * wait for that) |
| 1823 | */ |
| 1824 | if (!datasync && (dirty & ~CEPH_CAP_ANY_FILE_WR)) { |
| 1825 | dout("fsync waiting for flush_tid %u\n", flush_tid); |
| 1826 | ret = wait_event_interruptible(ci->i_cap_wq, |
| 1827 | caps_are_flushed(inode, flush_tid)); |
| 1828 | } |
| 1829 | |
| 1830 | dout("fsync %p%s done\n", inode, datasync ? " datasync" : ""); |
| 1831 | return ret; |
| 1832 | } |
| 1833 | |
| 1834 | /* |
| 1835 | * Flush any dirty caps back to the mds. If we aren't asked to wait, |
| 1836 | * queue inode for flush but don't do so immediately, because we can |
| 1837 | * get by with fewer MDS messages if we wait for data writeback to |
| 1838 | * complete first. |
| 1839 | */ |
Stephen Rothwell | f1a3d57 | 2010-01-18 11:53:08 +1100 | [diff] [blame] | 1840 | int ceph_write_inode(struct inode *inode, struct writeback_control *wbc) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1841 | { |
| 1842 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1843 | unsigned flush_tid; |
| 1844 | int err = 0; |
| 1845 | int dirty; |
Stephen Rothwell | f1a3d57 | 2010-01-18 11:53:08 +1100 | [diff] [blame] | 1846 | int wait = wbc->sync_mode == WB_SYNC_ALL; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1847 | |
| 1848 | dout("write_inode %p wait=%d\n", inode, wait); |
| 1849 | if (wait) { |
| 1850 | dirty = try_flush_caps(inode, NULL, &flush_tid); |
| 1851 | if (dirty) |
| 1852 | err = wait_event_interruptible(ci->i_cap_wq, |
| 1853 | caps_are_flushed(inode, flush_tid)); |
| 1854 | } else { |
Cheng Renquan | 640ef79 | 2010-03-26 17:40:33 +0800 | [diff] [blame] | 1855 | struct ceph_mds_client *mdsc = |
| 1856 | &ceph_sb_to_client(inode->i_sb)->mdsc; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1857 | |
| 1858 | spin_lock(&inode->i_lock); |
| 1859 | if (__ceph_caps_dirty(ci)) |
| 1860 | __cap_delay_requeue_front(mdsc, ci); |
| 1861 | spin_unlock(&inode->i_lock); |
| 1862 | } |
| 1863 | return err; |
| 1864 | } |
| 1865 | |
| 1866 | /* |
| 1867 | * After a recovering MDS goes active, we need to resend any caps |
| 1868 | * we were flushing. |
| 1869 | * |
| 1870 | * Caller holds session->s_mutex. |
| 1871 | */ |
| 1872 | static void kick_flushing_capsnaps(struct ceph_mds_client *mdsc, |
| 1873 | struct ceph_mds_session *session) |
| 1874 | { |
| 1875 | struct ceph_cap_snap *capsnap; |
| 1876 | |
| 1877 | dout("kick_flushing_capsnaps mds%d\n", session->s_mds); |
| 1878 | list_for_each_entry(capsnap, &session->s_cap_snaps_flushing, |
| 1879 | flushing_item) { |
| 1880 | struct ceph_inode_info *ci = capsnap->ci; |
| 1881 | struct inode *inode = &ci->vfs_inode; |
| 1882 | struct ceph_cap *cap; |
| 1883 | |
| 1884 | spin_lock(&inode->i_lock); |
| 1885 | cap = ci->i_auth_cap; |
| 1886 | if (cap && cap->session == session) { |
| 1887 | dout("kick_flushing_caps %p cap %p capsnap %p\n", inode, |
| 1888 | cap, capsnap); |
| 1889 | __ceph_flush_snaps(ci, &session); |
| 1890 | } else { |
| 1891 | pr_err("%p auth cap %p not mds%d ???\n", inode, |
| 1892 | cap, session->s_mds); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1893 | } |
Sage Weil | 0b0c06d | 2010-04-20 10:27:13 -0700 | [diff] [blame] | 1894 | spin_unlock(&inode->i_lock); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1895 | } |
| 1896 | } |
| 1897 | |
| 1898 | void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc, |
| 1899 | struct ceph_mds_session *session) |
| 1900 | { |
| 1901 | struct ceph_inode_info *ci; |
| 1902 | |
| 1903 | kick_flushing_capsnaps(mdsc, session); |
| 1904 | |
| 1905 | dout("kick_flushing_caps mds%d\n", session->s_mds); |
| 1906 | list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) { |
| 1907 | struct inode *inode = &ci->vfs_inode; |
| 1908 | struct ceph_cap *cap; |
| 1909 | int delayed = 0; |
| 1910 | |
| 1911 | spin_lock(&inode->i_lock); |
| 1912 | cap = ci->i_auth_cap; |
| 1913 | if (cap && cap->session == session) { |
| 1914 | dout("kick_flushing_caps %p cap %p %s\n", inode, |
| 1915 | cap, ceph_cap_string(ci->i_flushing_caps)); |
| 1916 | delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, |
| 1917 | __ceph_caps_used(ci), |
| 1918 | __ceph_caps_wanted(ci), |
| 1919 | cap->issued | cap->implemented, |
| 1920 | ci->i_flushing_caps, NULL); |
| 1921 | if (delayed) { |
| 1922 | spin_lock(&inode->i_lock); |
| 1923 | __cap_delay_requeue(mdsc, ci); |
| 1924 | spin_unlock(&inode->i_lock); |
| 1925 | } |
| 1926 | } else { |
| 1927 | pr_err("%p auth cap %p not mds%d ???\n", inode, |
| 1928 | cap, session->s_mds); |
| 1929 | spin_unlock(&inode->i_lock); |
| 1930 | } |
| 1931 | } |
| 1932 | } |
| 1933 | |
| 1934 | |
| 1935 | /* |
| 1936 | * Take references to capabilities we hold, so that we don't release |
| 1937 | * them to the MDS prematurely. |
| 1938 | * |
| 1939 | * Protected by i_lock. |
| 1940 | */ |
| 1941 | static void __take_cap_refs(struct ceph_inode_info *ci, int got) |
| 1942 | { |
| 1943 | if (got & CEPH_CAP_PIN) |
| 1944 | ci->i_pin_ref++; |
| 1945 | if (got & CEPH_CAP_FILE_RD) |
| 1946 | ci->i_rd_ref++; |
| 1947 | if (got & CEPH_CAP_FILE_CACHE) |
| 1948 | ci->i_rdcache_ref++; |
| 1949 | if (got & CEPH_CAP_FILE_WR) |
| 1950 | ci->i_wr_ref++; |
| 1951 | if (got & CEPH_CAP_FILE_BUFFER) { |
| 1952 | if (ci->i_wrbuffer_ref == 0) |
| 1953 | igrab(&ci->vfs_inode); |
| 1954 | ci->i_wrbuffer_ref++; |
| 1955 | dout("__take_cap_refs %p wrbuffer %d -> %d (?)\n", |
| 1956 | &ci->vfs_inode, ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref); |
| 1957 | } |
| 1958 | } |
| 1959 | |
| 1960 | /* |
| 1961 | * Try to grab cap references. Specify those refs we @want, and the |
| 1962 | * minimal set we @need. Also include the larger offset we are writing |
| 1963 | * to (when applicable), and check against max_size here as well. |
| 1964 | * Note that caller is responsible for ensuring max_size increases are |
| 1965 | * requested from the MDS. |
| 1966 | */ |
| 1967 | static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want, |
| 1968 | int *got, loff_t endoff, int *check_max, int *err) |
| 1969 | { |
| 1970 | struct inode *inode = &ci->vfs_inode; |
| 1971 | int ret = 0; |
| 1972 | int have, implemented; |
Sage Weil | 195d3ce | 2010-03-01 09:57:54 -0800 | [diff] [blame] | 1973 | int file_wanted; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1974 | |
| 1975 | dout("get_cap_refs %p need %s want %s\n", inode, |
| 1976 | ceph_cap_string(need), ceph_cap_string(want)); |
| 1977 | spin_lock(&inode->i_lock); |
| 1978 | |
Sage Weil | 195d3ce | 2010-03-01 09:57:54 -0800 | [diff] [blame] | 1979 | /* make sure file is actually open */ |
| 1980 | file_wanted = __ceph_caps_file_wanted(ci); |
| 1981 | if ((file_wanted & need) == 0) { |
| 1982 | dout("try_get_cap_refs need %s file_wanted %s, EBADF\n", |
| 1983 | ceph_cap_string(need), ceph_cap_string(file_wanted)); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 1984 | *err = -EBADF; |
| 1985 | ret = 1; |
| 1986 | goto out; |
| 1987 | } |
| 1988 | |
| 1989 | if (need & CEPH_CAP_FILE_WR) { |
| 1990 | if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) { |
| 1991 | dout("get_cap_refs %p endoff %llu > maxsize %llu\n", |
| 1992 | inode, endoff, ci->i_max_size); |
| 1993 | if (endoff > ci->i_wanted_max_size) { |
| 1994 | *check_max = 1; |
| 1995 | ret = 1; |
| 1996 | } |
| 1997 | goto out; |
| 1998 | } |
| 1999 | /* |
| 2000 | * If a sync write is in progress, we must wait, so that we |
| 2001 | * can get a final snapshot value for size+mtime. |
| 2002 | */ |
| 2003 | if (__ceph_have_pending_cap_snap(ci)) { |
| 2004 | dout("get_cap_refs %p cap_snap_pending\n", inode); |
| 2005 | goto out; |
| 2006 | } |
| 2007 | } |
| 2008 | have = __ceph_caps_issued(ci, &implemented); |
| 2009 | |
| 2010 | /* |
| 2011 | * disallow writes while a truncate is pending |
| 2012 | */ |
| 2013 | if (ci->i_truncate_pending) |
| 2014 | have &= ~CEPH_CAP_FILE_WR; |
| 2015 | |
| 2016 | if ((have & need) == need) { |
| 2017 | /* |
| 2018 | * Look at (implemented & ~have & not) so that we keep waiting |
| 2019 | * on transition from wanted -> needed caps. This is needed |
| 2020 | * for WRBUFFER|WR -> WR to avoid a new WR sync write from |
| 2021 | * going before a prior buffered writeback happens. |
| 2022 | */ |
| 2023 | int not = want & ~(have & need); |
| 2024 | int revoking = implemented & ~have; |
| 2025 | dout("get_cap_refs %p have %s but not %s (revoking %s)\n", |
| 2026 | inode, ceph_cap_string(have), ceph_cap_string(not), |
| 2027 | ceph_cap_string(revoking)); |
| 2028 | if ((revoking & not) == 0) { |
| 2029 | *got = need | (have & want); |
| 2030 | __take_cap_refs(ci, *got); |
| 2031 | ret = 1; |
| 2032 | } |
| 2033 | } else { |
| 2034 | dout("get_cap_refs %p have %s needed %s\n", inode, |
| 2035 | ceph_cap_string(have), ceph_cap_string(need)); |
| 2036 | } |
| 2037 | out: |
| 2038 | spin_unlock(&inode->i_lock); |
| 2039 | dout("get_cap_refs %p ret %d got %s\n", inode, |
| 2040 | ret, ceph_cap_string(*got)); |
| 2041 | return ret; |
| 2042 | } |
| 2043 | |
| 2044 | /* |
| 2045 | * Check the offset we are writing up to against our current |
| 2046 | * max_size. If necessary, tell the MDS we want to write to |
| 2047 | * a larger offset. |
| 2048 | */ |
| 2049 | static void check_max_size(struct inode *inode, loff_t endoff) |
| 2050 | { |
| 2051 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 2052 | int check = 0; |
| 2053 | |
| 2054 | /* do we need to explicitly request a larger max_size? */ |
| 2055 | spin_lock(&inode->i_lock); |
| 2056 | if ((endoff >= ci->i_max_size || |
| 2057 | endoff > (inode->i_size << 1)) && |
| 2058 | endoff > ci->i_wanted_max_size) { |
| 2059 | dout("write %p at large endoff %llu, req max_size\n", |
| 2060 | inode, endoff); |
| 2061 | ci->i_wanted_max_size = endoff; |
| 2062 | check = 1; |
| 2063 | } |
| 2064 | spin_unlock(&inode->i_lock); |
| 2065 | if (check) |
| 2066 | ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL); |
| 2067 | } |
| 2068 | |
| 2069 | /* |
| 2070 | * Wait for caps, and take cap references. If we can't get a WR cap |
| 2071 | * due to a small max_size, make sure we check_max_size (and possibly |
| 2072 | * ask the mds) so we don't get hung up indefinitely. |
| 2073 | */ |
| 2074 | int ceph_get_caps(struct ceph_inode_info *ci, int need, int want, int *got, |
| 2075 | loff_t endoff) |
| 2076 | { |
| 2077 | int check_max, ret, err; |
| 2078 | |
| 2079 | retry: |
| 2080 | if (endoff > 0) |
| 2081 | check_max_size(&ci->vfs_inode, endoff); |
| 2082 | check_max = 0; |
| 2083 | err = 0; |
| 2084 | ret = wait_event_interruptible(ci->i_cap_wq, |
| 2085 | try_get_cap_refs(ci, need, want, |
| 2086 | got, endoff, |
| 2087 | &check_max, &err)); |
| 2088 | if (err) |
| 2089 | ret = err; |
| 2090 | if (check_max) |
| 2091 | goto retry; |
| 2092 | return ret; |
| 2093 | } |
| 2094 | |
| 2095 | /* |
| 2096 | * Take cap refs. Caller must already know we hold at least one ref |
| 2097 | * on the caps in question or we don't know this is safe. |
| 2098 | */ |
| 2099 | void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps) |
| 2100 | { |
| 2101 | spin_lock(&ci->vfs_inode.i_lock); |
| 2102 | __take_cap_refs(ci, caps); |
| 2103 | spin_unlock(&ci->vfs_inode.i_lock); |
| 2104 | } |
| 2105 | |
| 2106 | /* |
| 2107 | * Release cap refs. |
| 2108 | * |
| 2109 | * If we released the last ref on any given cap, call ceph_check_caps |
| 2110 | * to release (or schedule a release). |
| 2111 | * |
| 2112 | * If we are releasing a WR cap (from a sync write), finalize any affected |
| 2113 | * cap_snap, and wake up any waiters. |
| 2114 | */ |
| 2115 | void ceph_put_cap_refs(struct ceph_inode_info *ci, int had) |
| 2116 | { |
| 2117 | struct inode *inode = &ci->vfs_inode; |
| 2118 | int last = 0, put = 0, flushsnaps = 0, wake = 0; |
| 2119 | struct ceph_cap_snap *capsnap; |
| 2120 | |
| 2121 | spin_lock(&inode->i_lock); |
| 2122 | if (had & CEPH_CAP_PIN) |
| 2123 | --ci->i_pin_ref; |
| 2124 | if (had & CEPH_CAP_FILE_RD) |
| 2125 | if (--ci->i_rd_ref == 0) |
| 2126 | last++; |
| 2127 | if (had & CEPH_CAP_FILE_CACHE) |
| 2128 | if (--ci->i_rdcache_ref == 0) |
| 2129 | last++; |
| 2130 | if (had & CEPH_CAP_FILE_BUFFER) { |
| 2131 | if (--ci->i_wrbuffer_ref == 0) { |
| 2132 | last++; |
| 2133 | put++; |
| 2134 | } |
| 2135 | dout("put_cap_refs %p wrbuffer %d -> %d (?)\n", |
| 2136 | inode, ci->i_wrbuffer_ref+1, ci->i_wrbuffer_ref); |
| 2137 | } |
| 2138 | if (had & CEPH_CAP_FILE_WR) |
| 2139 | if (--ci->i_wr_ref == 0) { |
| 2140 | last++; |
| 2141 | if (!list_empty(&ci->i_cap_snaps)) { |
| 2142 | capsnap = list_first_entry(&ci->i_cap_snaps, |
| 2143 | struct ceph_cap_snap, |
| 2144 | ci_item); |
| 2145 | if (capsnap->writing) { |
| 2146 | capsnap->writing = 0; |
| 2147 | flushsnaps = |
| 2148 | __ceph_finish_cap_snap(ci, |
| 2149 | capsnap); |
| 2150 | wake = 1; |
| 2151 | } |
| 2152 | } |
| 2153 | } |
| 2154 | spin_unlock(&inode->i_lock); |
| 2155 | |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 2156 | dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had), |
| 2157 | last ? " last" : "", put ? " put" : ""); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2158 | |
| 2159 | if (last && !flushsnaps) |
| 2160 | ceph_check_caps(ci, 0, NULL); |
| 2161 | else if (flushsnaps) |
| 2162 | ceph_flush_snaps(ci); |
| 2163 | if (wake) |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 2164 | wake_up_all(&ci->i_cap_wq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2165 | if (put) |
| 2166 | iput(inode); |
| 2167 | } |
| 2168 | |
| 2169 | /* |
| 2170 | * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap |
| 2171 | * context. Adjust per-snap dirty page accounting as appropriate. |
| 2172 | * Once all dirty data for a cap_snap is flushed, flush snapped file |
| 2173 | * metadata back to the MDS. If we dropped the last ref, call |
| 2174 | * ceph_check_caps. |
| 2175 | */ |
| 2176 | void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr, |
| 2177 | struct ceph_snap_context *snapc) |
| 2178 | { |
| 2179 | struct inode *inode = &ci->vfs_inode; |
| 2180 | int last = 0; |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 2181 | int complete_capsnap = 0; |
| 2182 | int drop_capsnap = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2183 | int found = 0; |
| 2184 | struct ceph_cap_snap *capsnap = NULL; |
| 2185 | |
| 2186 | spin_lock(&inode->i_lock); |
| 2187 | ci->i_wrbuffer_ref -= nr; |
| 2188 | last = !ci->i_wrbuffer_ref; |
| 2189 | |
| 2190 | if (ci->i_head_snapc == snapc) { |
| 2191 | ci->i_wrbuffer_ref_head -= nr; |
| 2192 | if (!ci->i_wrbuffer_ref_head) { |
| 2193 | ceph_put_snap_context(ci->i_head_snapc); |
| 2194 | ci->i_head_snapc = NULL; |
| 2195 | } |
| 2196 | dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n", |
| 2197 | inode, |
| 2198 | ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr, |
| 2199 | ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head, |
| 2200 | last ? " LAST" : ""); |
| 2201 | } else { |
| 2202 | list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { |
| 2203 | if (capsnap->context == snapc) { |
| 2204 | found = 1; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2205 | break; |
| 2206 | } |
| 2207 | } |
| 2208 | BUG_ON(!found); |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 2209 | capsnap->dirty_pages -= nr; |
| 2210 | if (capsnap->dirty_pages == 0) { |
| 2211 | complete_capsnap = 1; |
| 2212 | if (capsnap->dirty == 0) |
| 2213 | /* cap writeback completed before we created |
| 2214 | * the cap_snap; no FLUSHSNAP is needed */ |
| 2215 | drop_capsnap = 1; |
| 2216 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2217 | dout("put_wrbuffer_cap_refs on %p cap_snap %p " |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 2218 | " snap %lld %d/%d -> %d/%d %s%s%s\n", |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2219 | inode, capsnap, capsnap->context->seq, |
| 2220 | ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr, |
| 2221 | ci->i_wrbuffer_ref, capsnap->dirty_pages, |
| 2222 | last ? " (wrbuffer last)" : "", |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 2223 | complete_capsnap ? " (complete capsnap)" : "", |
| 2224 | drop_capsnap ? " (drop capsnap)" : ""); |
| 2225 | if (drop_capsnap) { |
| 2226 | ceph_put_snap_context(capsnap->context); |
| 2227 | list_del(&capsnap->ci_item); |
| 2228 | list_del(&capsnap->flushing_item); |
| 2229 | ceph_put_cap_snap(capsnap); |
| 2230 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2231 | } |
| 2232 | |
| 2233 | spin_unlock(&inode->i_lock); |
| 2234 | |
| 2235 | if (last) { |
| 2236 | ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL); |
| 2237 | iput(inode); |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 2238 | } else if (complete_capsnap) { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2239 | ceph_flush_snaps(ci); |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 2240 | wake_up_all(&ci->i_cap_wq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2241 | } |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 2242 | if (drop_capsnap) |
| 2243 | iput(inode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | /* |
| 2247 | * Handle a cap GRANT message from the MDS. (Note that a GRANT may |
| 2248 | * actually be a revocation if it specifies a smaller cap set.) |
| 2249 | * |
Sage Weil | 15637c8 | 2010-03-16 13:42:00 -0700 | [diff] [blame] | 2250 | * caller holds s_mutex and i_lock, we drop both. |
| 2251 | * |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2252 | * return value: |
| 2253 | * 0 - ok |
| 2254 | * 1 - check_caps on auth cap only (writeback) |
| 2255 | * 2 - check_caps (ack revoke) |
| 2256 | */ |
Sage Weil | 15637c8 | 2010-03-16 13:42:00 -0700 | [diff] [blame] | 2257 | static void handle_cap_grant(struct inode *inode, struct ceph_mds_caps *grant, |
| 2258 | struct ceph_mds_session *session, |
| 2259 | struct ceph_cap *cap, |
| 2260 | struct ceph_buffer *xattr_buf) |
Yehuda Sadeh | cd84db6 | 2010-06-11 16:58:48 -0700 | [diff] [blame] | 2261 | __releases(inode->i_lock) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2262 | { |
| 2263 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 2264 | int mds = session->s_mds; |
| 2265 | int seq = le32_to_cpu(grant->seq); |
| 2266 | int newcaps = le32_to_cpu(grant->caps); |
| 2267 | int issued, implemented, used, wanted, dirty; |
| 2268 | u64 size = le64_to_cpu(grant->size); |
| 2269 | u64 max_size = le64_to_cpu(grant->max_size); |
| 2270 | struct timespec mtime, atime, ctime; |
Sage Weil | 15637c8 | 2010-03-16 13:42:00 -0700 | [diff] [blame] | 2271 | int check_caps = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2272 | int wake = 0; |
| 2273 | int writeback = 0; |
| 2274 | int revoked_rdcache = 0; |
Sage Weil | 3c6f6b7 | 2010-02-09 15:24:44 -0800 | [diff] [blame] | 2275 | int queue_invalidate = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2276 | |
| 2277 | dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n", |
| 2278 | inode, cap, mds, seq, ceph_cap_string(newcaps)); |
| 2279 | dout(" size %llu max_size %llu, i_size %llu\n", size, max_size, |
| 2280 | inode->i_size); |
| 2281 | |
| 2282 | /* |
| 2283 | * If CACHE is being revoked, and we have no dirty buffers, |
| 2284 | * try to invalidate (once). (If there are dirty buffers, we |
| 2285 | * will invalidate _after_ writeback.) |
| 2286 | */ |
Sage Weil | 3b454c4 | 2010-06-10 13:20:33 -0700 | [diff] [blame] | 2287 | if (((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) && |
| 2288 | (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 && |
Yehuda Sadeh | bcd2cbd | 2010-02-19 00:12:21 +0000 | [diff] [blame] | 2289 | !ci->i_wrbuffer_ref) { |
Sage Weil | 5ecad6f | 2010-02-17 10:43:37 -0800 | [diff] [blame] | 2290 | if (try_nonblocking_invalidate(inode) == 0) { |
| 2291 | revoked_rdcache = 1; |
| 2292 | } else { |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2293 | /* there were locked pages.. invalidate later |
| 2294 | in a separate thread. */ |
| 2295 | if (ci->i_rdcache_revoking != ci->i_rdcache_gen) { |
Sage Weil | 3c6f6b7 | 2010-02-09 15:24:44 -0800 | [diff] [blame] | 2296 | queue_invalidate = 1; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2297 | ci->i_rdcache_revoking = ci->i_rdcache_gen; |
| 2298 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2299 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2300 | } |
| 2301 | |
| 2302 | /* side effects now are allowed */ |
| 2303 | |
| 2304 | issued = __ceph_caps_issued(ci, &implemented); |
| 2305 | issued |= implemented | __ceph_caps_dirty(ci); |
| 2306 | |
Sage Weil | 685f9a5d | 2009-11-09 12:05:48 -0800 | [diff] [blame] | 2307 | cap->cap_gen = session->s_cap_gen; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2308 | |
| 2309 | __check_cap_issue(ci, cap, newcaps); |
| 2310 | |
| 2311 | if ((issued & CEPH_CAP_AUTH_EXCL) == 0) { |
| 2312 | inode->i_mode = le32_to_cpu(grant->mode); |
| 2313 | inode->i_uid = le32_to_cpu(grant->uid); |
| 2314 | inode->i_gid = le32_to_cpu(grant->gid); |
| 2315 | dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode, |
| 2316 | inode->i_uid, inode->i_gid); |
| 2317 | } |
| 2318 | |
| 2319 | if ((issued & CEPH_CAP_LINK_EXCL) == 0) |
| 2320 | inode->i_nlink = le32_to_cpu(grant->nlink); |
| 2321 | |
| 2322 | if ((issued & CEPH_CAP_XATTR_EXCL) == 0 && grant->xattr_len) { |
| 2323 | int len = le32_to_cpu(grant->xattr_len); |
| 2324 | u64 version = le64_to_cpu(grant->xattr_version); |
| 2325 | |
| 2326 | if (version > ci->i_xattrs.version) { |
| 2327 | dout(" got new xattrs v%llu on %p len %d\n", |
| 2328 | version, inode, len); |
| 2329 | if (ci->i_xattrs.blob) |
| 2330 | ceph_buffer_put(ci->i_xattrs.blob); |
| 2331 | ci->i_xattrs.blob = ceph_buffer_get(xattr_buf); |
| 2332 | ci->i_xattrs.version = version; |
| 2333 | } |
| 2334 | } |
| 2335 | |
| 2336 | /* size/ctime/mtime/atime? */ |
| 2337 | ceph_fill_file_size(inode, issued, |
| 2338 | le32_to_cpu(grant->truncate_seq), |
| 2339 | le64_to_cpu(grant->truncate_size), size); |
| 2340 | ceph_decode_timespec(&mtime, &grant->mtime); |
| 2341 | ceph_decode_timespec(&atime, &grant->atime); |
| 2342 | ceph_decode_timespec(&ctime, &grant->ctime); |
| 2343 | ceph_fill_file_time(inode, issued, |
| 2344 | le32_to_cpu(grant->time_warp_seq), &ctime, &mtime, |
| 2345 | &atime); |
| 2346 | |
| 2347 | /* max size increase? */ |
| 2348 | if (max_size != ci->i_max_size) { |
| 2349 | dout("max_size %lld -> %llu\n", ci->i_max_size, max_size); |
| 2350 | ci->i_max_size = max_size; |
| 2351 | if (max_size >= ci->i_wanted_max_size) { |
| 2352 | ci->i_wanted_max_size = 0; /* reset */ |
| 2353 | ci->i_requested_max_size = 0; |
| 2354 | } |
| 2355 | wake = 1; |
| 2356 | } |
| 2357 | |
| 2358 | /* check cap bits */ |
| 2359 | wanted = __ceph_caps_wanted(ci); |
| 2360 | used = __ceph_caps_used(ci); |
| 2361 | dirty = __ceph_caps_dirty(ci); |
| 2362 | dout(" my wanted = %s, used = %s, dirty %s\n", |
| 2363 | ceph_cap_string(wanted), |
| 2364 | ceph_cap_string(used), |
| 2365 | ceph_cap_string(dirty)); |
| 2366 | if (wanted != le32_to_cpu(grant->wanted)) { |
| 2367 | dout("mds wanted %s -> %s\n", |
| 2368 | ceph_cap_string(le32_to_cpu(grant->wanted)), |
| 2369 | ceph_cap_string(wanted)); |
| 2370 | grant->wanted = cpu_to_le32(wanted); |
| 2371 | } |
| 2372 | |
| 2373 | cap->seq = seq; |
| 2374 | |
| 2375 | /* file layout may have changed */ |
| 2376 | ci->i_layout = grant->layout; |
| 2377 | |
| 2378 | /* revocation, grant, or no-op? */ |
| 2379 | if (cap->issued & ~newcaps) { |
Sage Weil | 3b454c4 | 2010-06-10 13:20:33 -0700 | [diff] [blame] | 2380 | int revoking = cap->issued & ~newcaps; |
| 2381 | |
| 2382 | dout("revocation: %s -> %s (revoking %s)\n", |
| 2383 | ceph_cap_string(cap->issued), |
| 2384 | ceph_cap_string(newcaps), |
| 2385 | ceph_cap_string(revoking)); |
| 2386 | if (revoking & CEPH_CAP_FILE_BUFFER) |
| 2387 | writeback = 1; /* initiate writeback; will delay ack */ |
| 2388 | else if (revoking == CEPH_CAP_FILE_CACHE && |
| 2389 | (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 && |
| 2390 | queue_invalidate) |
| 2391 | ; /* do nothing yet, invalidation will be queued */ |
| 2392 | else if (cap == ci->i_auth_cap) |
| 2393 | check_caps = 1; /* check auth cap only */ |
| 2394 | else |
| 2395 | check_caps = 2; /* check all caps */ |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2396 | cap->issued = newcaps; |
Sage Weil | 978097c | 2010-03-08 15:27:53 -0800 | [diff] [blame] | 2397 | cap->implemented |= newcaps; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2398 | } else if (cap->issued == newcaps) { |
| 2399 | dout("caps unchanged: %s -> %s\n", |
| 2400 | ceph_cap_string(cap->issued), ceph_cap_string(newcaps)); |
| 2401 | } else { |
| 2402 | dout("grant: %s -> %s\n", ceph_cap_string(cap->issued), |
| 2403 | ceph_cap_string(newcaps)); |
| 2404 | cap->issued = newcaps; |
| 2405 | cap->implemented |= newcaps; /* add bits only, to |
| 2406 | * avoid stepping on a |
| 2407 | * pending revocation */ |
| 2408 | wake = 1; |
| 2409 | } |
Sage Weil | 978097c | 2010-03-08 15:27:53 -0800 | [diff] [blame] | 2410 | BUG_ON(cap->issued & ~cap->implemented); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2411 | |
| 2412 | spin_unlock(&inode->i_lock); |
Sage Weil | 3c6f6b7 | 2010-02-09 15:24:44 -0800 | [diff] [blame] | 2413 | if (writeback) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2414 | /* |
| 2415 | * queue inode for writeback: we can't actually call |
| 2416 | * filemap_write_and_wait, etc. from message handler |
| 2417 | * context. |
| 2418 | */ |
Sage Weil | 3c6f6b7 | 2010-02-09 15:24:44 -0800 | [diff] [blame] | 2419 | ceph_queue_writeback(inode); |
| 2420 | if (queue_invalidate) |
| 2421 | ceph_queue_invalidate(inode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2422 | if (wake) |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 2423 | wake_up_all(&ci->i_cap_wq); |
Sage Weil | 15637c8 | 2010-03-16 13:42:00 -0700 | [diff] [blame] | 2424 | |
| 2425 | if (check_caps == 1) |
| 2426 | ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY, |
| 2427 | session); |
| 2428 | else if (check_caps == 2) |
| 2429 | ceph_check_caps(ci, CHECK_CAPS_NODELAY, session); |
| 2430 | else |
| 2431 | mutex_unlock(&session->s_mutex); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2432 | } |
| 2433 | |
| 2434 | /* |
| 2435 | * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the |
| 2436 | * MDS has been safely committed. |
| 2437 | */ |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 2438 | static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2439 | struct ceph_mds_caps *m, |
| 2440 | struct ceph_mds_session *session, |
| 2441 | struct ceph_cap *cap) |
| 2442 | __releases(inode->i_lock) |
| 2443 | { |
| 2444 | struct ceph_inode_info *ci = ceph_inode(inode); |
Cheng Renquan | 640ef79 | 2010-03-26 17:40:33 +0800 | [diff] [blame] | 2445 | 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] | 2446 | unsigned seq = le32_to_cpu(m->seq); |
| 2447 | int dirty = le32_to_cpu(m->dirty); |
| 2448 | int cleaned = 0; |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 2449 | int drop = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2450 | int i; |
| 2451 | |
| 2452 | for (i = 0; i < CEPH_CAP_BITS; i++) |
| 2453 | if ((dirty & (1 << i)) && |
| 2454 | flush_tid == ci->i_cap_flush_tid[i]) |
| 2455 | cleaned |= 1 << i; |
| 2456 | |
| 2457 | dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s," |
| 2458 | " flushing %s -> %s\n", |
| 2459 | inode, session->s_mds, seq, ceph_cap_string(dirty), |
| 2460 | ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps), |
| 2461 | ceph_cap_string(ci->i_flushing_caps & ~cleaned)); |
| 2462 | |
| 2463 | if (ci->i_flushing_caps == (ci->i_flushing_caps & ~cleaned)) |
| 2464 | goto out; |
| 2465 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2466 | ci->i_flushing_caps &= ~cleaned; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2467 | |
| 2468 | spin_lock(&mdsc->cap_dirty_lock); |
| 2469 | if (ci->i_flushing_caps == 0) { |
| 2470 | list_del_init(&ci->i_flushing_item); |
| 2471 | if (!list_empty(&session->s_cap_flushing)) |
| 2472 | dout(" mds%d still flushing cap on %p\n", |
| 2473 | session->s_mds, |
| 2474 | &list_entry(session->s_cap_flushing.next, |
| 2475 | struct ceph_inode_info, |
| 2476 | i_flushing_item)->vfs_inode); |
| 2477 | mdsc->num_cap_flushing--; |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 2478 | wake_up_all(&mdsc->cap_flushing_wq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2479 | dout(" inode %p now !flushing\n", inode); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 2480 | |
| 2481 | if (ci->i_dirty_caps == 0) { |
| 2482 | dout(" inode %p now clean\n", inode); |
| 2483 | BUG_ON(!list_empty(&ci->i_dirty_item)); |
| 2484 | drop = 1; |
Sage Weil | 76e3b39 | 2009-10-15 18:13:53 -0700 | [diff] [blame] | 2485 | } else { |
| 2486 | BUG_ON(list_empty(&ci->i_dirty_item)); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 2487 | } |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2488 | } |
| 2489 | spin_unlock(&mdsc->cap_dirty_lock); |
Yehuda Sadeh | 03066f2 | 2010-07-27 13:11:08 -0700 | [diff] [blame] | 2490 | wake_up_all(&ci->i_cap_wq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2491 | |
| 2492 | out: |
| 2493 | spin_unlock(&inode->i_lock); |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 2494 | if (drop) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2495 | iput(inode); |
| 2496 | } |
| 2497 | |
| 2498 | /* |
| 2499 | * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can |
| 2500 | * throw away our cap_snap. |
| 2501 | * |
| 2502 | * Caller hold s_mutex. |
| 2503 | */ |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 2504 | static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid, |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2505 | struct ceph_mds_caps *m, |
| 2506 | struct ceph_mds_session *session) |
| 2507 | { |
| 2508 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 2509 | u64 follows = le64_to_cpu(m->snap_follows); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2510 | struct ceph_cap_snap *capsnap; |
| 2511 | int drop = 0; |
| 2512 | |
| 2513 | dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n", |
| 2514 | inode, ci, session->s_mds, follows); |
| 2515 | |
| 2516 | spin_lock(&inode->i_lock); |
| 2517 | list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { |
| 2518 | if (capsnap->follows == follows) { |
| 2519 | if (capsnap->flush_tid != flush_tid) { |
| 2520 | dout(" cap_snap %p follows %lld tid %lld !=" |
| 2521 | " %lld\n", capsnap, follows, |
| 2522 | flush_tid, capsnap->flush_tid); |
| 2523 | break; |
| 2524 | } |
| 2525 | WARN_ON(capsnap->dirty_pages || capsnap->writing); |
Sage Weil | 819ccbf | 2010-04-01 09:33:46 -0700 | [diff] [blame] | 2526 | dout(" removing %p cap_snap %p follows %lld\n", |
| 2527 | inode, capsnap, follows); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2528 | ceph_put_snap_context(capsnap->context); |
| 2529 | list_del(&capsnap->ci_item); |
| 2530 | list_del(&capsnap->flushing_item); |
| 2531 | ceph_put_cap_snap(capsnap); |
| 2532 | drop = 1; |
| 2533 | break; |
| 2534 | } else { |
| 2535 | dout(" skipping cap_snap %p follows %lld\n", |
| 2536 | capsnap, capsnap->follows); |
| 2537 | } |
| 2538 | } |
| 2539 | spin_unlock(&inode->i_lock); |
| 2540 | if (drop) |
| 2541 | iput(inode); |
| 2542 | } |
| 2543 | |
| 2544 | /* |
| 2545 | * Handle TRUNC from MDS, indicating file truncation. |
| 2546 | * |
| 2547 | * caller hold s_mutex. |
| 2548 | */ |
| 2549 | static void handle_cap_trunc(struct inode *inode, |
| 2550 | struct ceph_mds_caps *trunc, |
| 2551 | struct ceph_mds_session *session) |
| 2552 | __releases(inode->i_lock) |
| 2553 | { |
| 2554 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 2555 | int mds = session->s_mds; |
| 2556 | int seq = le32_to_cpu(trunc->seq); |
| 2557 | u32 truncate_seq = le32_to_cpu(trunc->truncate_seq); |
| 2558 | u64 truncate_size = le64_to_cpu(trunc->truncate_size); |
| 2559 | u64 size = le64_to_cpu(trunc->size); |
| 2560 | int implemented = 0; |
| 2561 | int dirty = __ceph_caps_dirty(ci); |
| 2562 | int issued = __ceph_caps_issued(ceph_inode(inode), &implemented); |
| 2563 | int queue_trunc = 0; |
| 2564 | |
| 2565 | issued |= implemented | dirty; |
| 2566 | |
| 2567 | dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n", |
| 2568 | inode, mds, seq, truncate_size, truncate_seq); |
| 2569 | queue_trunc = ceph_fill_file_size(inode, issued, |
| 2570 | truncate_seq, truncate_size, size); |
| 2571 | spin_unlock(&inode->i_lock); |
| 2572 | |
| 2573 | if (queue_trunc) |
Sage Weil | 3c6f6b7 | 2010-02-09 15:24:44 -0800 | [diff] [blame] | 2574 | ceph_queue_vmtruncate(inode); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2575 | } |
| 2576 | |
| 2577 | /* |
| 2578 | * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a |
| 2579 | * different one. If we are the most recent migration we've seen (as |
| 2580 | * indicated by mseq), make note of the migrating cap bits for the |
| 2581 | * duration (until we see the corresponding IMPORT). |
| 2582 | * |
| 2583 | * caller holds s_mutex |
| 2584 | */ |
| 2585 | static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex, |
Sage Weil | 154f42c | 2010-06-21 13:45:04 -0700 | [diff] [blame] | 2586 | struct ceph_mds_session *session, |
| 2587 | int *open_target_sessions) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2588 | { |
| 2589 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 2590 | int mds = session->s_mds; |
| 2591 | unsigned mseq = le32_to_cpu(ex->migrate_seq); |
| 2592 | struct ceph_cap *cap = NULL, *t; |
| 2593 | struct rb_node *p; |
| 2594 | int remember = 1; |
| 2595 | |
| 2596 | dout("handle_cap_export inode %p ci %p mds%d mseq %d\n", |
| 2597 | inode, ci, mds, mseq); |
| 2598 | |
| 2599 | spin_lock(&inode->i_lock); |
| 2600 | |
| 2601 | /* make sure we haven't seen a higher mseq */ |
| 2602 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { |
| 2603 | t = rb_entry(p, struct ceph_cap, ci_node); |
| 2604 | if (ceph_seq_cmp(t->mseq, mseq) > 0) { |
| 2605 | dout(" higher mseq on cap from mds%d\n", |
| 2606 | t->session->s_mds); |
| 2607 | remember = 0; |
| 2608 | } |
| 2609 | if (t->session->s_mds == mds) |
| 2610 | cap = t; |
| 2611 | } |
| 2612 | |
| 2613 | if (cap) { |
| 2614 | if (remember) { |
| 2615 | /* make note */ |
| 2616 | ci->i_cap_exporting_mds = mds; |
| 2617 | ci->i_cap_exporting_mseq = mseq; |
| 2618 | ci->i_cap_exporting_issued = cap->issued; |
Sage Weil | 154f42c | 2010-06-21 13:45:04 -0700 | [diff] [blame] | 2619 | |
| 2620 | /* |
| 2621 | * make sure we have open sessions with all possible |
| 2622 | * export targets, so that we get the matching IMPORT |
| 2623 | */ |
| 2624 | *open_target_sessions = 1; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2625 | } |
Sage Weil | 7c1332b | 2010-02-16 11:39:45 -0800 | [diff] [blame] | 2626 | __ceph_remove_cap(cap); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2627 | } |
Sage Weil | 4ea0043 | 2010-03-16 10:36:40 -0700 | [diff] [blame] | 2628 | /* else, we already released it */ |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2629 | |
| 2630 | spin_unlock(&inode->i_lock); |
| 2631 | } |
| 2632 | |
| 2633 | /* |
| 2634 | * Handle cap IMPORT. If there are temp bits from an older EXPORT, |
| 2635 | * clean them up. |
| 2636 | * |
| 2637 | * caller holds s_mutex. |
| 2638 | */ |
| 2639 | static void handle_cap_import(struct ceph_mds_client *mdsc, |
| 2640 | struct inode *inode, struct ceph_mds_caps *im, |
| 2641 | struct ceph_mds_session *session, |
| 2642 | void *snaptrace, int snaptrace_len) |
| 2643 | { |
| 2644 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 2645 | int mds = session->s_mds; |
| 2646 | unsigned issued = le32_to_cpu(im->caps); |
| 2647 | unsigned wanted = le32_to_cpu(im->wanted); |
| 2648 | unsigned seq = le32_to_cpu(im->seq); |
| 2649 | unsigned mseq = le32_to_cpu(im->migrate_seq); |
| 2650 | u64 realmino = le64_to_cpu(im->realm); |
| 2651 | u64 cap_id = le64_to_cpu(im->cap_id); |
| 2652 | |
| 2653 | if (ci->i_cap_exporting_mds >= 0 && |
| 2654 | ceph_seq_cmp(ci->i_cap_exporting_mseq, mseq) < 0) { |
| 2655 | dout("handle_cap_import inode %p ci %p mds%d mseq %d" |
| 2656 | " - cleared exporting from mds%d\n", |
| 2657 | inode, ci, mds, mseq, |
| 2658 | ci->i_cap_exporting_mds); |
| 2659 | ci->i_cap_exporting_issued = 0; |
| 2660 | ci->i_cap_exporting_mseq = 0; |
| 2661 | ci->i_cap_exporting_mds = -1; |
| 2662 | } else { |
| 2663 | dout("handle_cap_import inode %p ci %p mds%d mseq %d\n", |
| 2664 | inode, ci, mds, mseq); |
| 2665 | } |
| 2666 | |
| 2667 | down_write(&mdsc->snap_rwsem); |
| 2668 | ceph_update_snap_trace(mdsc, snaptrace, snaptrace+snaptrace_len, |
| 2669 | false); |
| 2670 | downgrade_write(&mdsc->snap_rwsem); |
| 2671 | ceph_add_cap(inode, session, cap_id, -1, |
| 2672 | issued, wanted, seq, mseq, realmino, CEPH_CAP_FLAG_AUTH, |
| 2673 | NULL /* no caps context */); |
| 2674 | try_flush_caps(inode, session, NULL); |
| 2675 | up_read(&mdsc->snap_rwsem); |
| 2676 | } |
| 2677 | |
| 2678 | /* |
| 2679 | * Handle a caps message from the MDS. |
| 2680 | * |
| 2681 | * Identify the appropriate session, inode, and call the right handler |
| 2682 | * based on the cap op. |
| 2683 | */ |
| 2684 | void ceph_handle_caps(struct ceph_mds_session *session, |
| 2685 | struct ceph_msg *msg) |
| 2686 | { |
| 2687 | struct ceph_mds_client *mdsc = session->s_mdsc; |
| 2688 | struct super_block *sb = mdsc->client->sb; |
| 2689 | struct inode *inode; |
| 2690 | struct ceph_cap *cap; |
| 2691 | struct ceph_mds_caps *h; |
Sage Weil | 2600d2d | 2010-02-22 15:12:16 -0800 | [diff] [blame] | 2692 | int mds = session->s_mds; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2693 | int op; |
Sage Weil | 3d7ded4 | 2010-06-09 16:47:10 -0700 | [diff] [blame] | 2694 | u32 seq, mseq; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2695 | struct ceph_vino vino; |
| 2696 | u64 cap_id; |
| 2697 | u64 size, max_size; |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 2698 | u64 tid; |
Sage Weil | 70edb55 | 2010-03-01 13:20:50 -0800 | [diff] [blame] | 2699 | void *snaptrace; |
Sage Weil | 154f42c | 2010-06-21 13:45:04 -0700 | [diff] [blame] | 2700 | int open_target_sessions = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2701 | |
| 2702 | dout("handle_caps from mds%d\n", mds); |
| 2703 | |
| 2704 | /* decode */ |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 2705 | tid = le64_to_cpu(msg->hdr.tid); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2706 | if (msg->front.iov_len < sizeof(*h)) |
| 2707 | goto bad; |
| 2708 | h = msg->front.iov_base; |
Sage Weil | 70edb55 | 2010-03-01 13:20:50 -0800 | [diff] [blame] | 2709 | snaptrace = h + 1; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2710 | op = le32_to_cpu(h->op); |
| 2711 | vino.ino = le64_to_cpu(h->ino); |
| 2712 | vino.snap = CEPH_NOSNAP; |
| 2713 | cap_id = le64_to_cpu(h->cap_id); |
| 2714 | seq = le32_to_cpu(h->seq); |
Sage Weil | 3d7ded4 | 2010-06-09 16:47:10 -0700 | [diff] [blame] | 2715 | mseq = le32_to_cpu(h->migrate_seq); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2716 | size = le64_to_cpu(h->size); |
| 2717 | max_size = le64_to_cpu(h->max_size); |
| 2718 | |
| 2719 | mutex_lock(&session->s_mutex); |
| 2720 | session->s_seq++; |
| 2721 | dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq, |
| 2722 | (unsigned)seq); |
| 2723 | |
| 2724 | /* lookup ino */ |
| 2725 | inode = ceph_find_inode(sb, vino); |
| 2726 | dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino, |
| 2727 | vino.snap, inode); |
| 2728 | if (!inode) { |
| 2729 | dout(" i don't have ino %llx\n", vino.ino); |
Sage Weil | 3d7ded4 | 2010-06-09 16:47:10 -0700 | [diff] [blame] | 2730 | |
| 2731 | if (op == CEPH_CAP_OP_IMPORT) |
| 2732 | __queue_cap_release(session, vino.ino, cap_id, |
| 2733 | mseq, seq); |
| 2734 | |
| 2735 | /* |
| 2736 | * send any full release message to try to move things |
| 2737 | * along for the mds (who clearly thinks we still have this |
| 2738 | * cap). |
| 2739 | */ |
Sage Weil | ee6b272 | 2010-06-10 12:55:52 -0700 | [diff] [blame] | 2740 | ceph_add_cap_releases(mdsc, session); |
Sage Weil | 3d7ded4 | 2010-06-09 16:47:10 -0700 | [diff] [blame] | 2741 | ceph_send_cap_releases(mdsc, session); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2742 | goto done; |
| 2743 | } |
| 2744 | |
| 2745 | /* these will work even if we don't have a cap yet */ |
| 2746 | switch (op) { |
| 2747 | case CEPH_CAP_OP_FLUSHSNAP_ACK: |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 2748 | handle_cap_flushsnap_ack(inode, tid, h, session); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2749 | goto done; |
| 2750 | |
| 2751 | case CEPH_CAP_OP_EXPORT: |
Sage Weil | 154f42c | 2010-06-21 13:45:04 -0700 | [diff] [blame] | 2752 | handle_cap_export(inode, h, session, &open_target_sessions); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2753 | goto done; |
| 2754 | |
| 2755 | case CEPH_CAP_OP_IMPORT: |
| 2756 | handle_cap_import(mdsc, inode, h, session, |
Sage Weil | 70edb55 | 2010-03-01 13:20:50 -0800 | [diff] [blame] | 2757 | snaptrace, le32_to_cpu(h->snap_trace_len)); |
Sage Weil | 15637c8 | 2010-03-16 13:42:00 -0700 | [diff] [blame] | 2758 | ceph_check_caps(ceph_inode(inode), CHECK_CAPS_NODELAY, |
| 2759 | session); |
| 2760 | goto done_unlocked; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2761 | } |
| 2762 | |
| 2763 | /* the rest require a cap */ |
| 2764 | spin_lock(&inode->i_lock); |
| 2765 | cap = __get_cap_for_mds(ceph_inode(inode), mds); |
| 2766 | if (!cap) { |
Sage Weil | 9dbd412 | 2010-06-10 13:21:20 -0700 | [diff] [blame] | 2767 | dout(" no cap on %p ino %llx.%llx from mds%d\n", |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2768 | inode, ceph_ino(inode), ceph_snap(inode), mds); |
| 2769 | spin_unlock(&inode->i_lock); |
| 2770 | goto done; |
| 2771 | } |
| 2772 | |
| 2773 | /* note that each of these drops i_lock for us */ |
| 2774 | switch (op) { |
| 2775 | case CEPH_CAP_OP_REVOKE: |
| 2776 | case CEPH_CAP_OP_GRANT: |
Sage Weil | 15637c8 | 2010-03-16 13:42:00 -0700 | [diff] [blame] | 2777 | handle_cap_grant(inode, h, session, cap, msg->middle); |
| 2778 | goto done_unlocked; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2779 | |
| 2780 | case CEPH_CAP_OP_FLUSH_ACK: |
Sage Weil | 6df058c | 2009-12-22 11:24:33 -0800 | [diff] [blame] | 2781 | handle_cap_flush_ack(inode, tid, h, session, cap); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2782 | break; |
| 2783 | |
| 2784 | case CEPH_CAP_OP_TRUNC: |
| 2785 | handle_cap_trunc(inode, h, session); |
| 2786 | break; |
| 2787 | |
| 2788 | default: |
| 2789 | spin_unlock(&inode->i_lock); |
| 2790 | pr_err("ceph_handle_caps: unknown cap op %d %s\n", op, |
| 2791 | ceph_cap_op_name(op)); |
| 2792 | } |
| 2793 | |
| 2794 | done: |
Sage Weil | 15637c8 | 2010-03-16 13:42:00 -0700 | [diff] [blame] | 2795 | mutex_unlock(&session->s_mutex); |
| 2796 | done_unlocked: |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2797 | if (inode) |
| 2798 | iput(inode); |
Sage Weil | 154f42c | 2010-06-21 13:45:04 -0700 | [diff] [blame] | 2799 | if (open_target_sessions) |
| 2800 | ceph_mdsc_open_export_target_sessions(mdsc, session); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2801 | return; |
| 2802 | |
| 2803 | bad: |
| 2804 | pr_err("ceph_handle_caps: corrupt message\n"); |
Sage Weil | 9ec7cab | 2009-12-14 15:13:47 -0800 | [diff] [blame] | 2805 | ceph_msg_dump(msg); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2806 | return; |
| 2807 | } |
| 2808 | |
| 2809 | /* |
| 2810 | * Delayed work handler to process end of delayed cap release LRU list. |
| 2811 | */ |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 2812 | void ceph_check_delayed_caps(struct ceph_mds_client *mdsc) |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2813 | { |
| 2814 | struct ceph_inode_info *ci; |
| 2815 | int flags = CHECK_CAPS_NODELAY; |
| 2816 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2817 | dout("check_delayed_caps\n"); |
| 2818 | while (1) { |
| 2819 | spin_lock(&mdsc->cap_delay_lock); |
| 2820 | if (list_empty(&mdsc->cap_delay_list)) |
| 2821 | break; |
| 2822 | ci = list_first_entry(&mdsc->cap_delay_list, |
| 2823 | struct ceph_inode_info, |
| 2824 | i_cap_delay_list); |
| 2825 | if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 && |
| 2826 | time_before(jiffies, ci->i_hold_caps_max)) |
| 2827 | break; |
| 2828 | list_del_init(&ci->i_cap_delay_list); |
| 2829 | spin_unlock(&mdsc->cap_delay_lock); |
| 2830 | dout("check_delayed_caps on %p\n", &ci->vfs_inode); |
| 2831 | ceph_check_caps(ci, flags, NULL); |
| 2832 | } |
| 2833 | spin_unlock(&mdsc->cap_delay_lock); |
| 2834 | } |
| 2835 | |
| 2836 | /* |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 2837 | * Flush all dirty caps to the mds |
| 2838 | */ |
| 2839 | void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc) |
| 2840 | { |
Sage Weil | e9964c1 | 2010-03-01 15:16:56 -0800 | [diff] [blame] | 2841 | struct ceph_inode_info *ci, *nci = NULL; |
| 2842 | struct inode *inode, *ninode = NULL; |
| 2843 | struct list_head *p, *n; |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 2844 | |
| 2845 | dout("flush_dirty_caps\n"); |
| 2846 | spin_lock(&mdsc->cap_dirty_lock); |
Sage Weil | e9964c1 | 2010-03-01 15:16:56 -0800 | [diff] [blame] | 2847 | list_for_each_safe(p, n, &mdsc->cap_dirty) { |
| 2848 | if (nci) { |
| 2849 | ci = nci; |
| 2850 | inode = ninode; |
| 2851 | ci->i_ceph_flags &= ~CEPH_I_NOFLUSH; |
| 2852 | dout("flush_dirty_caps inode %p (was next inode)\n", |
| 2853 | inode); |
| 2854 | } else { |
| 2855 | ci = list_entry(p, struct ceph_inode_info, |
| 2856 | i_dirty_item); |
| 2857 | inode = igrab(&ci->vfs_inode); |
| 2858 | BUG_ON(!inode); |
| 2859 | dout("flush_dirty_caps inode %p\n", inode); |
| 2860 | } |
| 2861 | if (n != &mdsc->cap_dirty) { |
| 2862 | nci = list_entry(n, struct ceph_inode_info, |
| 2863 | i_dirty_item); |
| 2864 | ninode = igrab(&nci->vfs_inode); |
| 2865 | BUG_ON(!ninode); |
| 2866 | nci->i_ceph_flags |= CEPH_I_NOFLUSH; |
| 2867 | dout("flush_dirty_caps next inode %p, noflush\n", |
| 2868 | ninode); |
| 2869 | } else { |
| 2870 | nci = NULL; |
| 2871 | ninode = NULL; |
| 2872 | } |
Sage Weil | afcdaea | 2009-10-14 14:27:38 -0700 | [diff] [blame] | 2873 | spin_unlock(&mdsc->cap_dirty_lock); |
| 2874 | if (inode) { |
| 2875 | ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH, |
| 2876 | NULL); |
| 2877 | iput(inode); |
| 2878 | } |
| 2879 | spin_lock(&mdsc->cap_dirty_lock); |
| 2880 | } |
| 2881 | spin_unlock(&mdsc->cap_dirty_lock); |
| 2882 | } |
| 2883 | |
| 2884 | /* |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2885 | * Drop open file reference. If we were the last open file, |
| 2886 | * we may need to release capabilities to the MDS (or schedule |
| 2887 | * their delayed release). |
| 2888 | */ |
| 2889 | void ceph_put_fmode(struct ceph_inode_info *ci, int fmode) |
| 2890 | { |
| 2891 | struct inode *inode = &ci->vfs_inode; |
| 2892 | int last = 0; |
| 2893 | |
| 2894 | spin_lock(&inode->i_lock); |
| 2895 | dout("put_fmode %p fmode %d %d -> %d\n", inode, fmode, |
| 2896 | ci->i_nr_by_mode[fmode], ci->i_nr_by_mode[fmode]-1); |
| 2897 | BUG_ON(ci->i_nr_by_mode[fmode] == 0); |
| 2898 | if (--ci->i_nr_by_mode[fmode] == 0) |
| 2899 | last++; |
| 2900 | spin_unlock(&inode->i_lock); |
| 2901 | |
| 2902 | if (last && ci->i_vino.snap == CEPH_NOSNAP) |
| 2903 | ceph_check_caps(ci, 0, NULL); |
| 2904 | } |
| 2905 | |
| 2906 | /* |
| 2907 | * Helpers for embedding cap and dentry lease releases into mds |
| 2908 | * requests. |
| 2909 | * |
| 2910 | * @force is used by dentry_release (below) to force inclusion of a |
| 2911 | * record for the directory inode, even when there aren't any caps to |
| 2912 | * drop. |
| 2913 | */ |
| 2914 | int ceph_encode_inode_release(void **p, struct inode *inode, |
| 2915 | int mds, int drop, int unless, int force) |
| 2916 | { |
| 2917 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 2918 | struct ceph_cap *cap; |
| 2919 | struct ceph_mds_request_release *rel = *p; |
Sage Weil | ec97f88 | 2010-06-24 15:12:37 -0700 | [diff] [blame] | 2920 | int used, dirty; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2921 | int ret = 0; |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2922 | |
| 2923 | spin_lock(&inode->i_lock); |
Sage Weil | 916623d | 2010-03-16 15:01:07 -0700 | [diff] [blame] | 2924 | used = __ceph_caps_used(ci); |
Sage Weil | ec97f88 | 2010-06-24 15:12:37 -0700 | [diff] [blame] | 2925 | dirty = __ceph_caps_dirty(ci); |
Sage Weil | 916623d | 2010-03-16 15:01:07 -0700 | [diff] [blame] | 2926 | |
Sage Weil | ec97f88 | 2010-06-24 15:12:37 -0700 | [diff] [blame] | 2927 | dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n", |
| 2928 | inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop), |
Sage Weil | 916623d | 2010-03-16 15:01:07 -0700 | [diff] [blame] | 2929 | ceph_cap_string(unless)); |
| 2930 | |
Sage Weil | ec97f88 | 2010-06-24 15:12:37 -0700 | [diff] [blame] | 2931 | /* only drop unused, clean caps */ |
| 2932 | drop &= ~(used | dirty); |
Sage Weil | 916623d | 2010-03-16 15:01:07 -0700 | [diff] [blame] | 2933 | |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 2934 | cap = __get_cap_for_mds(ci, mds); |
| 2935 | if (cap && __cap_is_valid(cap)) { |
| 2936 | if (force || |
| 2937 | ((cap->issued & drop) && |
| 2938 | (cap->issued & unless) == 0)) { |
| 2939 | if ((cap->issued & drop) && |
| 2940 | (cap->issued & unless) == 0) { |
| 2941 | dout("encode_inode_release %p cap %p %s -> " |
| 2942 | "%s\n", inode, cap, |
| 2943 | ceph_cap_string(cap->issued), |
| 2944 | ceph_cap_string(cap->issued & ~drop)); |
| 2945 | cap->issued &= ~drop; |
| 2946 | cap->implemented &= ~drop; |
| 2947 | if (ci->i_ceph_flags & CEPH_I_NODELAY) { |
| 2948 | int wanted = __ceph_caps_wanted(ci); |
| 2949 | dout(" wanted %s -> %s (act %s)\n", |
| 2950 | ceph_cap_string(cap->mds_wanted), |
| 2951 | ceph_cap_string(cap->mds_wanted & |
| 2952 | ~wanted), |
| 2953 | ceph_cap_string(wanted)); |
| 2954 | cap->mds_wanted &= wanted; |
| 2955 | } |
| 2956 | } else { |
| 2957 | dout("encode_inode_release %p cap %p %s" |
| 2958 | " (force)\n", inode, cap, |
| 2959 | ceph_cap_string(cap->issued)); |
| 2960 | } |
| 2961 | |
| 2962 | rel->ino = cpu_to_le64(ceph_ino(inode)); |
| 2963 | rel->cap_id = cpu_to_le64(cap->cap_id); |
| 2964 | rel->seq = cpu_to_le32(cap->seq); |
| 2965 | rel->issue_seq = cpu_to_le32(cap->issue_seq), |
| 2966 | rel->mseq = cpu_to_le32(cap->mseq); |
| 2967 | rel->caps = cpu_to_le32(cap->issued); |
| 2968 | rel->wanted = cpu_to_le32(cap->mds_wanted); |
| 2969 | rel->dname_len = 0; |
| 2970 | rel->dname_seq = 0; |
| 2971 | *p += sizeof(*rel); |
| 2972 | ret = 1; |
| 2973 | } else { |
| 2974 | dout("encode_inode_release %p cap %p %s\n", |
| 2975 | inode, cap, ceph_cap_string(cap->issued)); |
| 2976 | } |
| 2977 | } |
| 2978 | spin_unlock(&inode->i_lock); |
| 2979 | return ret; |
| 2980 | } |
| 2981 | |
| 2982 | int ceph_encode_dentry_release(void **p, struct dentry *dentry, |
| 2983 | int mds, int drop, int unless) |
| 2984 | { |
| 2985 | struct inode *dir = dentry->d_parent->d_inode; |
| 2986 | struct ceph_mds_request_release *rel = *p; |
| 2987 | struct ceph_dentry_info *di = ceph_dentry(dentry); |
| 2988 | int force = 0; |
| 2989 | int ret; |
| 2990 | |
| 2991 | /* |
| 2992 | * force an record for the directory caps if we have a dentry lease. |
| 2993 | * this is racy (can't take i_lock and d_lock together), but it |
| 2994 | * doesn't have to be perfect; the mds will revoke anything we don't |
| 2995 | * release. |
| 2996 | */ |
| 2997 | spin_lock(&dentry->d_lock); |
| 2998 | if (di->lease_session && di->lease_session->s_mds == mds) |
| 2999 | force = 1; |
| 3000 | spin_unlock(&dentry->d_lock); |
| 3001 | |
| 3002 | ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force); |
| 3003 | |
| 3004 | spin_lock(&dentry->d_lock); |
| 3005 | if (ret && di->lease_session && di->lease_session->s_mds == mds) { |
| 3006 | dout("encode_dentry_release %p mds%d seq %d\n", |
| 3007 | dentry, mds, (int)di->lease_seq); |
| 3008 | rel->dname_len = cpu_to_le32(dentry->d_name.len); |
| 3009 | memcpy(*p, dentry->d_name.name, dentry->d_name.len); |
| 3010 | *p += dentry->d_name.len; |
| 3011 | rel->dname_seq = cpu_to_le32(di->lease_seq); |
Sage Weil | 1dadcce | 2010-07-23 13:54:21 -0700 | [diff] [blame] | 3012 | __ceph_mdsc_drop_dentry_lease(dentry); |
Sage Weil | a8599bd | 2009-10-06 11:31:12 -0700 | [diff] [blame] | 3013 | } |
| 3014 | spin_unlock(&dentry->d_lock); |
| 3015 | return ret; |
| 3016 | } |