13d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h> 2a8599bd8SSage Weil 3a8599bd8SSage Weil #include <linux/fs.h> 4a8599bd8SSage Weil #include <linux/kernel.h> 5a8599bd8SSage Weil #include <linux/sched.h> 65a0e3ad6STejun Heo #include <linux/slab.h> 7a8599bd8SSage Weil #include <linux/vmalloc.h> 8a8599bd8SSage Weil #include <linux/wait.h> 9f1a3d572SStephen Rothwell #include <linux/writeback.h> 10a8599bd8SSage Weil 11a8599bd8SSage Weil #include "super.h" 123d14c5d2SYehuda Sadeh #include "mds_client.h" 1399ccbd22SMilosz Tanski #include "cache.h" 143d14c5d2SYehuda Sadeh #include <linux/ceph/decode.h> 153d14c5d2SYehuda Sadeh #include <linux/ceph/messenger.h> 16a8599bd8SSage Weil 17a8599bd8SSage Weil /* 18a8599bd8SSage Weil * Capability management 19a8599bd8SSage Weil * 20a8599bd8SSage Weil * The Ceph metadata servers control client access to inode metadata 21a8599bd8SSage Weil * and file data by issuing capabilities, granting clients permission 22a8599bd8SSage Weil * to read and/or write both inode field and file data to OSDs 23a8599bd8SSage Weil * (storage nodes). Each capability consists of a set of bits 24a8599bd8SSage Weil * indicating which operations are allowed. 25a8599bd8SSage Weil * 26a8599bd8SSage Weil * If the client holds a *_SHARED cap, the client has a coherent value 27a8599bd8SSage Weil * that can be safely read from the cached inode. 28a8599bd8SSage Weil * 29a8599bd8SSage Weil * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the 30a8599bd8SSage Weil * client is allowed to change inode attributes (e.g., file size, 31a8599bd8SSage Weil * mtime), note its dirty state in the ceph_cap, and asynchronously 32a8599bd8SSage Weil * flush that metadata change to the MDS. 33a8599bd8SSage Weil * 34a8599bd8SSage Weil * In the event of a conflicting operation (perhaps by another 35a8599bd8SSage Weil * client), the MDS will revoke the conflicting client capabilities. 36a8599bd8SSage Weil * 37a8599bd8SSage Weil * In order for a client to cache an inode, it must hold a capability 38a8599bd8SSage Weil * with at least one MDS server. When inodes are released, release 39a8599bd8SSage Weil * notifications are batched and periodically sent en masse to the MDS 40a8599bd8SSage Weil * cluster to release server state. 41a8599bd8SSage Weil */ 42a8599bd8SSage Weil 43a8599bd8SSage Weil 44a8599bd8SSage Weil /* 45a8599bd8SSage Weil * Generate readable cap strings for debugging output. 46a8599bd8SSage Weil */ 47a8599bd8SSage Weil #define MAX_CAP_STR 20 48a8599bd8SSage Weil static char cap_str[MAX_CAP_STR][40]; 49a8599bd8SSage Weil static DEFINE_SPINLOCK(cap_str_lock); 50a8599bd8SSage Weil static int last_cap_str; 51a8599bd8SSage Weil 52a8599bd8SSage Weil static char *gcap_string(char *s, int c) 53a8599bd8SSage Weil { 54a8599bd8SSage Weil if (c & CEPH_CAP_GSHARED) 55a8599bd8SSage Weil *s++ = 's'; 56a8599bd8SSage Weil if (c & CEPH_CAP_GEXCL) 57a8599bd8SSage Weil *s++ = 'x'; 58a8599bd8SSage Weil if (c & CEPH_CAP_GCACHE) 59a8599bd8SSage Weil *s++ = 'c'; 60a8599bd8SSage Weil if (c & CEPH_CAP_GRD) 61a8599bd8SSage Weil *s++ = 'r'; 62a8599bd8SSage Weil if (c & CEPH_CAP_GWR) 63a8599bd8SSage Weil *s++ = 'w'; 64a8599bd8SSage Weil if (c & CEPH_CAP_GBUFFER) 65a8599bd8SSage Weil *s++ = 'b'; 66a8599bd8SSage Weil if (c & CEPH_CAP_GLAZYIO) 67a8599bd8SSage Weil *s++ = 'l'; 68a8599bd8SSage Weil return s; 69a8599bd8SSage Weil } 70a8599bd8SSage Weil 71a8599bd8SSage Weil const char *ceph_cap_string(int caps) 72a8599bd8SSage Weil { 73a8599bd8SSage Weil int i; 74a8599bd8SSage Weil char *s; 75a8599bd8SSage Weil int c; 76a8599bd8SSage Weil 77a8599bd8SSage Weil spin_lock(&cap_str_lock); 78a8599bd8SSage Weil i = last_cap_str++; 79a8599bd8SSage Weil if (last_cap_str == MAX_CAP_STR) 80a8599bd8SSage Weil last_cap_str = 0; 81a8599bd8SSage Weil spin_unlock(&cap_str_lock); 82a8599bd8SSage Weil 83a8599bd8SSage Weil s = cap_str[i]; 84a8599bd8SSage Weil 85a8599bd8SSage Weil if (caps & CEPH_CAP_PIN) 86a8599bd8SSage Weil *s++ = 'p'; 87a8599bd8SSage Weil 88a8599bd8SSage Weil c = (caps >> CEPH_CAP_SAUTH) & 3; 89a8599bd8SSage Weil if (c) { 90a8599bd8SSage Weil *s++ = 'A'; 91a8599bd8SSage Weil s = gcap_string(s, c); 92a8599bd8SSage Weil } 93a8599bd8SSage Weil 94a8599bd8SSage Weil c = (caps >> CEPH_CAP_SLINK) & 3; 95a8599bd8SSage Weil if (c) { 96a8599bd8SSage Weil *s++ = 'L'; 97a8599bd8SSage Weil s = gcap_string(s, c); 98a8599bd8SSage Weil } 99a8599bd8SSage Weil 100a8599bd8SSage Weil c = (caps >> CEPH_CAP_SXATTR) & 3; 101a8599bd8SSage Weil if (c) { 102a8599bd8SSage Weil *s++ = 'X'; 103a8599bd8SSage Weil s = gcap_string(s, c); 104a8599bd8SSage Weil } 105a8599bd8SSage Weil 106a8599bd8SSage Weil c = caps >> CEPH_CAP_SFILE; 107a8599bd8SSage Weil if (c) { 108a8599bd8SSage Weil *s++ = 'F'; 109a8599bd8SSage Weil s = gcap_string(s, c); 110a8599bd8SSage Weil } 111a8599bd8SSage Weil 112a8599bd8SSage Weil if (s == cap_str[i]) 113a8599bd8SSage Weil *s++ = '-'; 114a8599bd8SSage Weil *s = 0; 115a8599bd8SSage Weil return cap_str[i]; 116a8599bd8SSage Weil } 117a8599bd8SSage Weil 11837151668SYehuda Sadeh void ceph_caps_init(struct ceph_mds_client *mdsc) 119a8599bd8SSage Weil { 12037151668SYehuda Sadeh INIT_LIST_HEAD(&mdsc->caps_list); 12137151668SYehuda Sadeh spin_lock_init(&mdsc->caps_list_lock); 122a8599bd8SSage Weil } 123a8599bd8SSage Weil 12437151668SYehuda Sadeh void ceph_caps_finalize(struct ceph_mds_client *mdsc) 125a8599bd8SSage Weil { 126a8599bd8SSage Weil struct ceph_cap *cap; 127a8599bd8SSage Weil 12837151668SYehuda Sadeh spin_lock(&mdsc->caps_list_lock); 12937151668SYehuda Sadeh while (!list_empty(&mdsc->caps_list)) { 13037151668SYehuda Sadeh cap = list_first_entry(&mdsc->caps_list, 13137151668SYehuda Sadeh struct ceph_cap, caps_item); 132a8599bd8SSage Weil list_del(&cap->caps_item); 133a8599bd8SSage Weil kmem_cache_free(ceph_cap_cachep, cap); 134a8599bd8SSage Weil } 13537151668SYehuda Sadeh mdsc->caps_total_count = 0; 13637151668SYehuda Sadeh mdsc->caps_avail_count = 0; 13737151668SYehuda Sadeh mdsc->caps_use_count = 0; 13837151668SYehuda Sadeh mdsc->caps_reserve_count = 0; 13937151668SYehuda Sadeh mdsc->caps_min_count = 0; 14037151668SYehuda Sadeh spin_unlock(&mdsc->caps_list_lock); 14185ccce43SSage Weil } 14285ccce43SSage Weil 14337151668SYehuda Sadeh void ceph_adjust_min_caps(struct ceph_mds_client *mdsc, int delta) 14485ccce43SSage Weil { 14537151668SYehuda Sadeh spin_lock(&mdsc->caps_list_lock); 14637151668SYehuda Sadeh mdsc->caps_min_count += delta; 14737151668SYehuda Sadeh BUG_ON(mdsc->caps_min_count < 0); 14837151668SYehuda Sadeh spin_unlock(&mdsc->caps_list_lock); 149a8599bd8SSage Weil } 150a8599bd8SSage Weil 15193faca6eSmajianpeng void ceph_reserve_caps(struct ceph_mds_client *mdsc, 15237151668SYehuda Sadeh struct ceph_cap_reservation *ctx, int need) 153a8599bd8SSage Weil { 154a8599bd8SSage Weil int i; 155a8599bd8SSage Weil struct ceph_cap *cap; 156a8599bd8SSage Weil int have; 157a8599bd8SSage Weil int alloc = 0; 158a8599bd8SSage Weil LIST_HEAD(newcaps); 159a8599bd8SSage Weil 160a8599bd8SSage Weil dout("reserve caps ctx=%p need=%d\n", ctx, need); 161a8599bd8SSage Weil 162a8599bd8SSage Weil /* first reserve any caps that are already allocated */ 16337151668SYehuda Sadeh spin_lock(&mdsc->caps_list_lock); 16437151668SYehuda Sadeh if (mdsc->caps_avail_count >= need) 165a8599bd8SSage Weil have = need; 166a8599bd8SSage Weil else 16737151668SYehuda Sadeh have = mdsc->caps_avail_count; 16837151668SYehuda Sadeh mdsc->caps_avail_count -= have; 16937151668SYehuda Sadeh mdsc->caps_reserve_count += have; 17037151668SYehuda Sadeh BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + 17137151668SYehuda Sadeh mdsc->caps_reserve_count + 17237151668SYehuda Sadeh mdsc->caps_avail_count); 17337151668SYehuda Sadeh spin_unlock(&mdsc->caps_list_lock); 174a8599bd8SSage Weil 175a8599bd8SSage Weil for (i = have; i < need; i++) { 176a8599bd8SSage Weil cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS); 17793faca6eSmajianpeng if (!cap) 17893faca6eSmajianpeng break; 179a8599bd8SSage Weil list_add(&cap->caps_item, &newcaps); 180a8599bd8SSage Weil alloc++; 181a8599bd8SSage Weil } 18293faca6eSmajianpeng /* we didn't manage to reserve as much as we needed */ 18393faca6eSmajianpeng if (have + alloc != need) 18493faca6eSmajianpeng pr_warn("reserve caps ctx=%p ENOMEM need=%d got=%d\n", 18593faca6eSmajianpeng ctx, need, have + alloc); 186a8599bd8SSage Weil 18737151668SYehuda Sadeh spin_lock(&mdsc->caps_list_lock); 18837151668SYehuda Sadeh mdsc->caps_total_count += alloc; 18937151668SYehuda Sadeh mdsc->caps_reserve_count += alloc; 19037151668SYehuda Sadeh list_splice(&newcaps, &mdsc->caps_list); 191a8599bd8SSage Weil 19237151668SYehuda Sadeh BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + 19337151668SYehuda Sadeh mdsc->caps_reserve_count + 19437151668SYehuda Sadeh mdsc->caps_avail_count); 19537151668SYehuda Sadeh spin_unlock(&mdsc->caps_list_lock); 196a8599bd8SSage Weil 197a8599bd8SSage Weil ctx->count = need; 198a8599bd8SSage Weil dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n", 19937151668SYehuda Sadeh ctx, mdsc->caps_total_count, mdsc->caps_use_count, 20037151668SYehuda Sadeh mdsc->caps_reserve_count, mdsc->caps_avail_count); 201a8599bd8SSage Weil } 202a8599bd8SSage Weil 20337151668SYehuda Sadeh int ceph_unreserve_caps(struct ceph_mds_client *mdsc, 20437151668SYehuda Sadeh struct ceph_cap_reservation *ctx) 205a8599bd8SSage Weil { 206a8599bd8SSage Weil dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count); 207a8599bd8SSage Weil if (ctx->count) { 20837151668SYehuda Sadeh spin_lock(&mdsc->caps_list_lock); 20937151668SYehuda Sadeh BUG_ON(mdsc->caps_reserve_count < ctx->count); 21037151668SYehuda Sadeh mdsc->caps_reserve_count -= ctx->count; 21137151668SYehuda Sadeh mdsc->caps_avail_count += ctx->count; 212a8599bd8SSage Weil ctx->count = 0; 213a8599bd8SSage Weil dout("unreserve caps %d = %d used + %d resv + %d avail\n", 21437151668SYehuda Sadeh mdsc->caps_total_count, mdsc->caps_use_count, 21537151668SYehuda Sadeh mdsc->caps_reserve_count, mdsc->caps_avail_count); 21637151668SYehuda Sadeh BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + 21737151668SYehuda Sadeh mdsc->caps_reserve_count + 21837151668SYehuda Sadeh mdsc->caps_avail_count); 21937151668SYehuda Sadeh spin_unlock(&mdsc->caps_list_lock); 220a8599bd8SSage Weil } 221a8599bd8SSage Weil return 0; 222a8599bd8SSage Weil } 223a8599bd8SSage Weil 22437151668SYehuda Sadeh static struct ceph_cap *get_cap(struct ceph_mds_client *mdsc, 22537151668SYehuda Sadeh struct ceph_cap_reservation *ctx) 226a8599bd8SSage Weil { 227a8599bd8SSage Weil struct ceph_cap *cap = NULL; 228a8599bd8SSage Weil 229a8599bd8SSage Weil /* temporary, until we do something about cap import/export */ 230443b3760SSage Weil if (!ctx) { 231443b3760SSage Weil cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS); 232443b3760SSage Weil if (cap) { 2334d1d0534SYan, Zheng spin_lock(&mdsc->caps_list_lock); 23437151668SYehuda Sadeh mdsc->caps_use_count++; 23537151668SYehuda Sadeh mdsc->caps_total_count++; 2364d1d0534SYan, Zheng spin_unlock(&mdsc->caps_list_lock); 237443b3760SSage Weil } 238443b3760SSage Weil return cap; 239443b3760SSage Weil } 240a8599bd8SSage Weil 24137151668SYehuda Sadeh spin_lock(&mdsc->caps_list_lock); 242a8599bd8SSage Weil dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n", 24337151668SYehuda Sadeh ctx, ctx->count, mdsc->caps_total_count, mdsc->caps_use_count, 24437151668SYehuda Sadeh mdsc->caps_reserve_count, mdsc->caps_avail_count); 245a8599bd8SSage Weil BUG_ON(!ctx->count); 24637151668SYehuda Sadeh BUG_ON(ctx->count > mdsc->caps_reserve_count); 24737151668SYehuda Sadeh BUG_ON(list_empty(&mdsc->caps_list)); 248a8599bd8SSage Weil 249a8599bd8SSage Weil ctx->count--; 25037151668SYehuda Sadeh mdsc->caps_reserve_count--; 25137151668SYehuda Sadeh mdsc->caps_use_count++; 252a8599bd8SSage Weil 25337151668SYehuda Sadeh cap = list_first_entry(&mdsc->caps_list, struct ceph_cap, caps_item); 254a8599bd8SSage Weil list_del(&cap->caps_item); 255a8599bd8SSage Weil 25637151668SYehuda Sadeh BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + 25737151668SYehuda Sadeh mdsc->caps_reserve_count + mdsc->caps_avail_count); 25837151668SYehuda Sadeh spin_unlock(&mdsc->caps_list_lock); 259a8599bd8SSage Weil return cap; 260a8599bd8SSage Weil } 261a8599bd8SSage Weil 26237151668SYehuda Sadeh void ceph_put_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap) 263a8599bd8SSage Weil { 26437151668SYehuda Sadeh spin_lock(&mdsc->caps_list_lock); 2657c1332b8SSage Weil dout("put_cap %p %d = %d used + %d resv + %d avail\n", 26637151668SYehuda Sadeh cap, mdsc->caps_total_count, mdsc->caps_use_count, 26737151668SYehuda Sadeh mdsc->caps_reserve_count, mdsc->caps_avail_count); 26837151668SYehuda Sadeh mdsc->caps_use_count--; 269a8599bd8SSage Weil /* 27085ccce43SSage Weil * Keep some preallocated caps around (ceph_min_count), to 27185ccce43SSage Weil * avoid lots of free/alloc churn. 272a8599bd8SSage Weil */ 27337151668SYehuda Sadeh if (mdsc->caps_avail_count >= mdsc->caps_reserve_count + 27437151668SYehuda Sadeh mdsc->caps_min_count) { 27537151668SYehuda Sadeh mdsc->caps_total_count--; 276a8599bd8SSage Weil kmem_cache_free(ceph_cap_cachep, cap); 277a8599bd8SSage Weil } else { 27837151668SYehuda Sadeh mdsc->caps_avail_count++; 27937151668SYehuda Sadeh list_add(&cap->caps_item, &mdsc->caps_list); 280a8599bd8SSage Weil } 281a8599bd8SSage Weil 28237151668SYehuda Sadeh BUG_ON(mdsc->caps_total_count != mdsc->caps_use_count + 28337151668SYehuda Sadeh mdsc->caps_reserve_count + mdsc->caps_avail_count); 28437151668SYehuda Sadeh spin_unlock(&mdsc->caps_list_lock); 285a8599bd8SSage Weil } 286a8599bd8SSage Weil 2873d14c5d2SYehuda Sadeh void ceph_reservation_status(struct ceph_fs_client *fsc, 28885ccce43SSage Weil int *total, int *avail, int *used, int *reserved, 28985ccce43SSage Weil int *min) 290a8599bd8SSage Weil { 2913d14c5d2SYehuda Sadeh struct ceph_mds_client *mdsc = fsc->mdsc; 29237151668SYehuda Sadeh 293a8599bd8SSage Weil if (total) 29437151668SYehuda Sadeh *total = mdsc->caps_total_count; 295a8599bd8SSage Weil if (avail) 29637151668SYehuda Sadeh *avail = mdsc->caps_avail_count; 297a8599bd8SSage Weil if (used) 29837151668SYehuda Sadeh *used = mdsc->caps_use_count; 299a8599bd8SSage Weil if (reserved) 30037151668SYehuda Sadeh *reserved = mdsc->caps_reserve_count; 30185ccce43SSage Weil if (min) 30237151668SYehuda Sadeh *min = mdsc->caps_min_count; 303a8599bd8SSage Weil } 304a8599bd8SSage Weil 305a8599bd8SSage Weil /* 306a8599bd8SSage Weil * Find ceph_cap for given mds, if any. 307a8599bd8SSage Weil * 308be655596SSage Weil * Called with i_ceph_lock held. 309a8599bd8SSage Weil */ 310a8599bd8SSage Weil static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds) 311a8599bd8SSage Weil { 312a8599bd8SSage Weil struct ceph_cap *cap; 313a8599bd8SSage Weil struct rb_node *n = ci->i_caps.rb_node; 314a8599bd8SSage Weil 315a8599bd8SSage Weil while (n) { 316a8599bd8SSage Weil cap = rb_entry(n, struct ceph_cap, ci_node); 317a8599bd8SSage Weil if (mds < cap->mds) 318a8599bd8SSage Weil n = n->rb_left; 319a8599bd8SSage Weil else if (mds > cap->mds) 320a8599bd8SSage Weil n = n->rb_right; 321a8599bd8SSage Weil else 322a8599bd8SSage Weil return cap; 323a8599bd8SSage Weil } 324a8599bd8SSage Weil return NULL; 325a8599bd8SSage Weil } 326a8599bd8SSage Weil 3272bc50259SGreg Farnum struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci, int mds) 3282bc50259SGreg Farnum { 3292bc50259SGreg Farnum struct ceph_cap *cap; 3302bc50259SGreg Farnum 331be655596SSage Weil spin_lock(&ci->i_ceph_lock); 3322bc50259SGreg Farnum cap = __get_cap_for_mds(ci, mds); 333be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 3342bc50259SGreg Farnum return cap; 3352bc50259SGreg Farnum } 3362bc50259SGreg Farnum 337a8599bd8SSage Weil /* 33833caad32SSage Weil * Return id of any MDS with a cap, preferably FILE_WR|BUFFER|EXCL, else -1. 339a8599bd8SSage Weil */ 340ca81f3f6SSage Weil static int __ceph_get_cap_mds(struct ceph_inode_info *ci) 341a8599bd8SSage Weil { 342a8599bd8SSage Weil struct ceph_cap *cap; 343a8599bd8SSage Weil int mds = -1; 344a8599bd8SSage Weil struct rb_node *p; 345a8599bd8SSage Weil 34633caad32SSage Weil /* prefer mds with WR|BUFFER|EXCL caps */ 347a8599bd8SSage Weil for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { 348a8599bd8SSage Weil cap = rb_entry(p, struct ceph_cap, ci_node); 349a8599bd8SSage Weil mds = cap->mds; 350a8599bd8SSage Weil if (cap->issued & (CEPH_CAP_FILE_WR | 351a8599bd8SSage Weil CEPH_CAP_FILE_BUFFER | 352a8599bd8SSage Weil CEPH_CAP_FILE_EXCL)) 353a8599bd8SSage Weil break; 354a8599bd8SSage Weil } 355a8599bd8SSage Weil return mds; 356a8599bd8SSage Weil } 357a8599bd8SSage Weil 358a8599bd8SSage Weil int ceph_get_cap_mds(struct inode *inode) 359a8599bd8SSage Weil { 360be655596SSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 361a8599bd8SSage Weil int mds; 362be655596SSage Weil spin_lock(&ci->i_ceph_lock); 363ca81f3f6SSage Weil mds = __ceph_get_cap_mds(ceph_inode(inode)); 364be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 365a8599bd8SSage Weil return mds; 366a8599bd8SSage Weil } 367a8599bd8SSage Weil 368a8599bd8SSage Weil /* 369be655596SSage Weil * Called under i_ceph_lock. 370a8599bd8SSage Weil */ 371a8599bd8SSage Weil static void __insert_cap_node(struct ceph_inode_info *ci, 372a8599bd8SSage Weil struct ceph_cap *new) 373a8599bd8SSage Weil { 374a8599bd8SSage Weil struct rb_node **p = &ci->i_caps.rb_node; 375a8599bd8SSage Weil struct rb_node *parent = NULL; 376a8599bd8SSage Weil struct ceph_cap *cap = NULL; 377a8599bd8SSage Weil 378a8599bd8SSage Weil while (*p) { 379a8599bd8SSage Weil parent = *p; 380a8599bd8SSage Weil cap = rb_entry(parent, struct ceph_cap, ci_node); 381a8599bd8SSage Weil if (new->mds < cap->mds) 382a8599bd8SSage Weil p = &(*p)->rb_left; 383a8599bd8SSage Weil else if (new->mds > cap->mds) 384a8599bd8SSage Weil p = &(*p)->rb_right; 385a8599bd8SSage Weil else 386a8599bd8SSage Weil BUG(); 387a8599bd8SSage Weil } 388a8599bd8SSage Weil 389a8599bd8SSage Weil rb_link_node(&new->ci_node, parent, p); 390a8599bd8SSage Weil rb_insert_color(&new->ci_node, &ci->i_caps); 391a8599bd8SSage Weil } 392a8599bd8SSage Weil 393a8599bd8SSage Weil /* 394a8599bd8SSage Weil * (re)set cap hold timeouts, which control the delayed release 395a8599bd8SSage Weil * of unused caps back to the MDS. Should be called on cap use. 396a8599bd8SSage Weil */ 397a8599bd8SSage Weil static void __cap_set_timeouts(struct ceph_mds_client *mdsc, 398a8599bd8SSage Weil struct ceph_inode_info *ci) 399a8599bd8SSage Weil { 4003d14c5d2SYehuda Sadeh struct ceph_mount_options *ma = mdsc->fsc->mount_options; 401a8599bd8SSage Weil 402a8599bd8SSage Weil ci->i_hold_caps_min = round_jiffies(jiffies + 403a8599bd8SSage Weil ma->caps_wanted_delay_min * HZ); 404a8599bd8SSage Weil ci->i_hold_caps_max = round_jiffies(jiffies + 405a8599bd8SSage Weil ma->caps_wanted_delay_max * HZ); 406a8599bd8SSage Weil dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode, 407a8599bd8SSage Weil ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies); 408a8599bd8SSage Weil } 409a8599bd8SSage Weil 410a8599bd8SSage Weil /* 411a8599bd8SSage Weil * (Re)queue cap at the end of the delayed cap release list. 412a8599bd8SSage Weil * 413a8599bd8SSage Weil * If I_FLUSH is set, leave the inode at the front of the list. 414a8599bd8SSage Weil * 415be655596SSage Weil * Caller holds i_ceph_lock 416a8599bd8SSage Weil * -> we take mdsc->cap_delay_lock 417a8599bd8SSage Weil */ 418a8599bd8SSage Weil static void __cap_delay_requeue(struct ceph_mds_client *mdsc, 419a8599bd8SSage Weil struct ceph_inode_info *ci) 420a8599bd8SSage Weil { 421a8599bd8SSage Weil __cap_set_timeouts(mdsc, ci); 422a8599bd8SSage Weil dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode, 423a8599bd8SSage Weil ci->i_ceph_flags, ci->i_hold_caps_max); 424a8599bd8SSage Weil if (!mdsc->stopping) { 425a8599bd8SSage Weil spin_lock(&mdsc->cap_delay_lock); 426a8599bd8SSage Weil if (!list_empty(&ci->i_cap_delay_list)) { 427a8599bd8SSage Weil if (ci->i_ceph_flags & CEPH_I_FLUSH) 428a8599bd8SSage Weil goto no_change; 429a8599bd8SSage Weil list_del_init(&ci->i_cap_delay_list); 430a8599bd8SSage Weil } 431a8599bd8SSage Weil list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list); 432a8599bd8SSage Weil no_change: 433a8599bd8SSage Weil spin_unlock(&mdsc->cap_delay_lock); 434a8599bd8SSage Weil } 435a8599bd8SSage Weil } 436a8599bd8SSage Weil 437a8599bd8SSage Weil /* 438a8599bd8SSage Weil * Queue an inode for immediate writeback. Mark inode with I_FLUSH, 439a8599bd8SSage Weil * indicating we should send a cap message to flush dirty metadata 440a8599bd8SSage Weil * asap, and move to the front of the delayed cap list. 441a8599bd8SSage Weil */ 442a8599bd8SSage Weil static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc, 443a8599bd8SSage Weil struct ceph_inode_info *ci) 444a8599bd8SSage Weil { 445a8599bd8SSage Weil dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode); 446a8599bd8SSage Weil spin_lock(&mdsc->cap_delay_lock); 447a8599bd8SSage Weil ci->i_ceph_flags |= CEPH_I_FLUSH; 448a8599bd8SSage Weil if (!list_empty(&ci->i_cap_delay_list)) 449a8599bd8SSage Weil list_del_init(&ci->i_cap_delay_list); 450a8599bd8SSage Weil list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list); 451a8599bd8SSage Weil spin_unlock(&mdsc->cap_delay_lock); 452a8599bd8SSage Weil } 453a8599bd8SSage Weil 454a8599bd8SSage Weil /* 455a8599bd8SSage Weil * Cancel delayed work on cap. 456a8599bd8SSage Weil * 457be655596SSage Weil * Caller must hold i_ceph_lock. 458a8599bd8SSage Weil */ 459a8599bd8SSage Weil static void __cap_delay_cancel(struct ceph_mds_client *mdsc, 460a8599bd8SSage Weil struct ceph_inode_info *ci) 461a8599bd8SSage Weil { 462a8599bd8SSage Weil dout("__cap_delay_cancel %p\n", &ci->vfs_inode); 463a8599bd8SSage Weil if (list_empty(&ci->i_cap_delay_list)) 464a8599bd8SSage Weil return; 465a8599bd8SSage Weil spin_lock(&mdsc->cap_delay_lock); 466a8599bd8SSage Weil list_del_init(&ci->i_cap_delay_list); 467a8599bd8SSage Weil spin_unlock(&mdsc->cap_delay_lock); 468a8599bd8SSage Weil } 469a8599bd8SSage Weil 470a8599bd8SSage Weil /* 471a8599bd8SSage Weil * Common issue checks for add_cap, handle_cap_grant. 472a8599bd8SSage Weil */ 473a8599bd8SSage Weil static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap, 474a8599bd8SSage Weil unsigned issued) 475a8599bd8SSage Weil { 476a8599bd8SSage Weil unsigned had = __ceph_caps_issued(ci, NULL); 477a8599bd8SSage Weil 478a8599bd8SSage Weil /* 479a8599bd8SSage Weil * Each time we receive FILE_CACHE anew, we increment 480a8599bd8SSage Weil * i_rdcache_gen. 481a8599bd8SSage Weil */ 4822962507cSSage Weil if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) && 48399ccbd22SMilosz Tanski (had & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0) { 484a8599bd8SSage Weil ci->i_rdcache_gen++; 48599ccbd22SMilosz Tanski } 486a8599bd8SSage Weil 487a8599bd8SSage Weil /* 4882f276c51SYan, Zheng * if we are newly issued FILE_SHARED, mark dir not complete; we 489a8599bd8SSage Weil * don't know what happened to this directory while we didn't 490a8599bd8SSage Weil * have the cap. 491a8599bd8SSage Weil */ 492a8599bd8SSage Weil if ((issued & CEPH_CAP_FILE_SHARED) && 493a8599bd8SSage Weil (had & CEPH_CAP_FILE_SHARED) == 0) { 494a8599bd8SSage Weil ci->i_shared_gen++; 495a8673d61SYan, Zheng if (S_ISDIR(ci->vfs_inode.i_mode)) { 496a8673d61SYan, Zheng dout(" marking %p NOT complete\n", &ci->vfs_inode); 4972f276c51SYan, Zheng __ceph_dir_clear_complete(ci); 498a8673d61SYan, Zheng } 499a8599bd8SSage Weil } 500a8599bd8SSage Weil } 501a8599bd8SSage Weil 502a8599bd8SSage Weil /* 503a8599bd8SSage Weil * Add a capability under the given MDS session. 504a8599bd8SSage Weil * 505a8599bd8SSage Weil * Caller should hold session snap_rwsem (read) and s_mutex. 506a8599bd8SSage Weil * 507a8599bd8SSage Weil * @fmode is the open file mode, if we are opening a file, otherwise 508a8599bd8SSage Weil * it is < 0. (This is so we can atomically add the cap and add an 509a8599bd8SSage Weil * open file reference to it.) 510a8599bd8SSage Weil */ 511a8599bd8SSage Weil int ceph_add_cap(struct inode *inode, 512a8599bd8SSage Weil struct ceph_mds_session *session, u64 cap_id, 513a8599bd8SSage Weil int fmode, unsigned issued, unsigned wanted, 514a8599bd8SSage Weil unsigned seq, unsigned mseq, u64 realmino, int flags, 515a8599bd8SSage Weil struct ceph_cap_reservation *caps_reservation) 516a8599bd8SSage Weil { 5173d14c5d2SYehuda Sadeh struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc; 518a8599bd8SSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 519a8599bd8SSage Weil struct ceph_cap *new_cap = NULL; 520a8599bd8SSage Weil struct ceph_cap *cap; 521a8599bd8SSage Weil int mds = session->s_mds; 522a8599bd8SSage Weil int actual_wanted; 523a8599bd8SSage Weil 524a8599bd8SSage Weil dout("add_cap %p mds%d cap %llx %s seq %d\n", inode, 525a8599bd8SSage Weil session->s_mds, cap_id, ceph_cap_string(issued), seq); 526a8599bd8SSage Weil 527a8599bd8SSage Weil /* 528a8599bd8SSage Weil * If we are opening the file, include file mode wanted bits 529a8599bd8SSage Weil * in wanted. 530a8599bd8SSage Weil */ 531a8599bd8SSage Weil if (fmode >= 0) 532a8599bd8SSage Weil wanted |= ceph_caps_for_mode(fmode); 533a8599bd8SSage Weil 534a8599bd8SSage Weil retry: 535be655596SSage Weil spin_lock(&ci->i_ceph_lock); 536a8599bd8SSage Weil cap = __get_cap_for_mds(ci, mds); 537a8599bd8SSage Weil if (!cap) { 538a8599bd8SSage Weil if (new_cap) { 539a8599bd8SSage Weil cap = new_cap; 540a8599bd8SSage Weil new_cap = NULL; 541a8599bd8SSage Weil } else { 542be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 54337151668SYehuda Sadeh new_cap = get_cap(mdsc, caps_reservation); 544a8599bd8SSage Weil if (new_cap == NULL) 545a8599bd8SSage Weil return -ENOMEM; 546a8599bd8SSage Weil goto retry; 547a8599bd8SSage Weil } 548a8599bd8SSage Weil 549a8599bd8SSage Weil cap->issued = 0; 550a8599bd8SSage Weil cap->implemented = 0; 551a8599bd8SSage Weil cap->mds = mds; 552a8599bd8SSage Weil cap->mds_wanted = 0; 553964266ccSYan, Zheng cap->mseq = 0; 554a8599bd8SSage Weil 555a8599bd8SSage Weil cap->ci = ci; 556a8599bd8SSage Weil __insert_cap_node(ci, cap); 557a8599bd8SSage Weil 558a8599bd8SSage Weil /* clear out old exporting info? (i.e. on cap import) */ 559a8599bd8SSage Weil if (ci->i_cap_exporting_mds == mds) { 560a8599bd8SSage Weil ci->i_cap_exporting_issued = 0; 561a8599bd8SSage Weil ci->i_cap_exporting_mseq = 0; 562a8599bd8SSage Weil ci->i_cap_exporting_mds = -1; 563a8599bd8SSage Weil } 564a8599bd8SSage Weil 565a8599bd8SSage Weil /* add to session cap list */ 566a8599bd8SSage Weil cap->session = session; 567a8599bd8SSage Weil spin_lock(&session->s_cap_lock); 568a8599bd8SSage Weil list_add_tail(&cap->session_caps, &session->s_caps); 569a8599bd8SSage Weil session->s_nr_caps++; 570a8599bd8SSage Weil spin_unlock(&session->s_cap_lock); 5713540303fSSage Weil } else if (new_cap) 5723540303fSSage Weil ceph_put_cap(mdsc, new_cap); 573a8599bd8SSage Weil 574a8599bd8SSage Weil if (!ci->i_snap_realm) { 575a8599bd8SSage Weil /* 576a8599bd8SSage Weil * add this inode to the appropriate snap realm 577a8599bd8SSage Weil */ 578a8599bd8SSage Weil struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc, 579a8599bd8SSage Weil realmino); 580a8599bd8SSage Weil if (realm) { 581a8599bd8SSage Weil ceph_get_snap_realm(mdsc, realm); 582a8599bd8SSage Weil spin_lock(&realm->inodes_with_caps_lock); 583a8599bd8SSage Weil ci->i_snap_realm = realm; 584a8599bd8SSage Weil list_add(&ci->i_snap_realm_item, 585a8599bd8SSage Weil &realm->inodes_with_caps); 586a8599bd8SSage Weil spin_unlock(&realm->inodes_with_caps_lock); 587a8599bd8SSage Weil } else { 588a8599bd8SSage Weil pr_err("ceph_add_cap: couldn't find snap realm %llx\n", 589a8599bd8SSage Weil realmino); 590b8cd07e7SSage Weil WARN_ON(!realm); 591a8599bd8SSage Weil } 592a8599bd8SSage Weil } 593a8599bd8SSage Weil 594a8599bd8SSage Weil __check_cap_issue(ci, cap, issued); 595a8599bd8SSage Weil 596a8599bd8SSage Weil /* 597a8599bd8SSage Weil * If we are issued caps we don't want, or the mds' wanted 598a8599bd8SSage Weil * value appears to be off, queue a check so we'll release 599a8599bd8SSage Weil * later and/or update the mds wanted value. 600a8599bd8SSage Weil */ 601a8599bd8SSage Weil actual_wanted = __ceph_caps_wanted(ci); 602a8599bd8SSage Weil if ((wanted & ~actual_wanted) || 603a8599bd8SSage Weil (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) { 604a8599bd8SSage Weil dout(" issued %s, mds wanted %s, actual %s, queueing\n", 605a8599bd8SSage Weil ceph_cap_string(issued), ceph_cap_string(wanted), 606a8599bd8SSage Weil ceph_cap_string(actual_wanted)); 607a8599bd8SSage Weil __cap_delay_requeue(mdsc, ci); 608a8599bd8SSage Weil } 609a8599bd8SSage Weil 610b8c2f3aeSYan, Zheng if (flags & CEPH_CAP_FLAG_AUTH) { 611b8c2f3aeSYan, Zheng if (ci->i_auth_cap == NULL || 612b8c2f3aeSYan, Zheng ceph_seq_cmp(ci->i_auth_cap->mseq, mseq) < 0) 613a8599bd8SSage Weil ci->i_auth_cap = cap; 614b8c2f3aeSYan, Zheng } else if (ci->i_auth_cap == cap) { 615a8599bd8SSage Weil ci->i_auth_cap = NULL; 6168a92a119SYan, Zheng spin_lock(&mdsc->cap_dirty_lock); 6178a92a119SYan, Zheng if (!list_empty(&ci->i_dirty_item)) { 6188a92a119SYan, Zheng dout(" moving %p to cap_dirty_migrating\n", inode); 6198a92a119SYan, Zheng list_move(&ci->i_dirty_item, 6208a92a119SYan, Zheng &mdsc->cap_dirty_migrating); 6218a92a119SYan, Zheng } 6228a92a119SYan, Zheng spin_unlock(&mdsc->cap_dirty_lock); 6238a92a119SYan, Zheng } 624a8599bd8SSage Weil 625a8599bd8SSage Weil dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n", 626a8599bd8SSage Weil inode, ceph_vinop(inode), cap, ceph_cap_string(issued), 627a8599bd8SSage Weil ceph_cap_string(issued|cap->issued), seq, mds); 628a8599bd8SSage Weil cap->cap_id = cap_id; 629a8599bd8SSage Weil cap->issued = issued; 630a8599bd8SSage Weil cap->implemented |= issued; 631964266ccSYan, Zheng if (mseq > cap->mseq) 632964266ccSYan, Zheng cap->mds_wanted = wanted; 633964266ccSYan, Zheng else 634a8599bd8SSage Weil cap->mds_wanted |= wanted; 635a8599bd8SSage Weil cap->seq = seq; 636a8599bd8SSage Weil cap->issue_seq = seq; 637a8599bd8SSage Weil cap->mseq = mseq; 638685f9a5dSSage Weil cap->cap_gen = session->s_cap_gen; 639a8599bd8SSage Weil 640a8599bd8SSage Weil if (fmode >= 0) 641a8599bd8SSage Weil __ceph_get_fmode(ci, fmode); 642be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 64303066f23SYehuda Sadeh wake_up_all(&ci->i_cap_wq); 644a8599bd8SSage Weil return 0; 645a8599bd8SSage Weil } 646a8599bd8SSage Weil 647a8599bd8SSage Weil /* 648a8599bd8SSage Weil * Return true if cap has not timed out and belongs to the current 649a8599bd8SSage Weil * generation of the MDS session (i.e. has not gone 'stale' due to 650a8599bd8SSage Weil * us losing touch with the mds). 651a8599bd8SSage Weil */ 652a8599bd8SSage Weil static int __cap_is_valid(struct ceph_cap *cap) 653a8599bd8SSage Weil { 654a8599bd8SSage Weil unsigned long ttl; 655cdac8303SSage Weil u32 gen; 656a8599bd8SSage Weil 657d8fb02abSAlex Elder spin_lock(&cap->session->s_gen_ttl_lock); 658a8599bd8SSage Weil gen = cap->session->s_cap_gen; 659a8599bd8SSage Weil ttl = cap->session->s_cap_ttl; 660d8fb02abSAlex Elder spin_unlock(&cap->session->s_gen_ttl_lock); 661a8599bd8SSage Weil 662685f9a5dSSage Weil if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) { 663a8599bd8SSage Weil dout("__cap_is_valid %p cap %p issued %s " 664a8599bd8SSage Weil "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode, 665685f9a5dSSage Weil cap, ceph_cap_string(cap->issued), cap->cap_gen, gen); 666a8599bd8SSage Weil return 0; 667a8599bd8SSage Weil } 668a8599bd8SSage Weil 669a8599bd8SSage Weil return 1; 670a8599bd8SSage Weil } 671a8599bd8SSage Weil 672a8599bd8SSage Weil /* 673a8599bd8SSage Weil * Return set of valid cap bits issued to us. Note that caps time 674a8599bd8SSage Weil * out, and may be invalidated in bulk if the client session times out 675a8599bd8SSage Weil * and session->s_cap_gen is bumped. 676a8599bd8SSage Weil */ 677a8599bd8SSage Weil int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented) 678a8599bd8SSage Weil { 6797af8f1e4SSage Weil int have = ci->i_snap_caps | ci->i_cap_exporting_issued; 680a8599bd8SSage Weil struct ceph_cap *cap; 681a8599bd8SSage Weil struct rb_node *p; 682a8599bd8SSage Weil 683a8599bd8SSage Weil if (implemented) 684a8599bd8SSage Weil *implemented = 0; 685a8599bd8SSage Weil for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { 686a8599bd8SSage Weil cap = rb_entry(p, struct ceph_cap, ci_node); 687a8599bd8SSage Weil if (!__cap_is_valid(cap)) 688a8599bd8SSage Weil continue; 689a8599bd8SSage Weil dout("__ceph_caps_issued %p cap %p issued %s\n", 690a8599bd8SSage Weil &ci->vfs_inode, cap, ceph_cap_string(cap->issued)); 691a8599bd8SSage Weil have |= cap->issued; 692a8599bd8SSage Weil if (implemented) 693a8599bd8SSage Weil *implemented |= cap->implemented; 694a8599bd8SSage Weil } 695b1530f57SYan, Zheng /* 696b1530f57SYan, Zheng * exclude caps issued by non-auth MDS, but are been revoking 697b1530f57SYan, Zheng * by the auth MDS. The non-auth MDS should be revoking/exporting 698b1530f57SYan, Zheng * these caps, but the message is delayed. 699b1530f57SYan, Zheng */ 700b1530f57SYan, Zheng if (ci->i_auth_cap) { 701b1530f57SYan, Zheng cap = ci->i_auth_cap; 702b1530f57SYan, Zheng have &= ~cap->implemented | cap->issued; 703b1530f57SYan, Zheng } 704a8599bd8SSage Weil return have; 705a8599bd8SSage Weil } 706a8599bd8SSage Weil 707a8599bd8SSage Weil /* 708a8599bd8SSage Weil * Get cap bits issued by caps other than @ocap 709a8599bd8SSage Weil */ 710a8599bd8SSage Weil int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap) 711a8599bd8SSage Weil { 712a8599bd8SSage Weil int have = ci->i_snap_caps; 713a8599bd8SSage Weil struct ceph_cap *cap; 714a8599bd8SSage Weil struct rb_node *p; 715a8599bd8SSage Weil 716a8599bd8SSage Weil for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { 717a8599bd8SSage Weil cap = rb_entry(p, struct ceph_cap, ci_node); 718a8599bd8SSage Weil if (cap == ocap) 719a8599bd8SSage Weil continue; 720a8599bd8SSage Weil if (!__cap_is_valid(cap)) 721a8599bd8SSage Weil continue; 722a8599bd8SSage Weil have |= cap->issued; 723a8599bd8SSage Weil } 724a8599bd8SSage Weil return have; 725a8599bd8SSage Weil } 726a8599bd8SSage Weil 727a8599bd8SSage Weil /* 728a8599bd8SSage Weil * Move a cap to the end of the LRU (oldest caps at list head, newest 729a8599bd8SSage Weil * at list tail). 730a8599bd8SSage Weil */ 731a8599bd8SSage Weil static void __touch_cap(struct ceph_cap *cap) 732a8599bd8SSage Weil { 733a8599bd8SSage Weil struct ceph_mds_session *s = cap->session; 734a8599bd8SSage Weil 7355dacf091SSage Weil spin_lock(&s->s_cap_lock); 7367c1332b8SSage Weil if (s->s_cap_iterator == NULL) { 737a8599bd8SSage Weil dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap, 738a8599bd8SSage Weil s->s_mds); 739a8599bd8SSage Weil list_move_tail(&cap->session_caps, &s->s_caps); 7405dacf091SSage Weil } else { 7415dacf091SSage Weil dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n", 7425dacf091SSage Weil &cap->ci->vfs_inode, cap, s->s_mds); 7435dacf091SSage Weil } 744a8599bd8SSage Weil spin_unlock(&s->s_cap_lock); 745a8599bd8SSage Weil } 746a8599bd8SSage Weil 747a8599bd8SSage Weil /* 748a8599bd8SSage Weil * Check if we hold the given mask. If so, move the cap(s) to the 749a8599bd8SSage Weil * front of their respective LRUs. (This is the preferred way for 750a8599bd8SSage Weil * callers to check for caps they want.) 751a8599bd8SSage Weil */ 752a8599bd8SSage Weil int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch) 753a8599bd8SSage Weil { 754a8599bd8SSage Weil struct ceph_cap *cap; 755a8599bd8SSage Weil struct rb_node *p; 756a8599bd8SSage Weil int have = ci->i_snap_caps; 757a8599bd8SSage Weil 758a8599bd8SSage Weil if ((have & mask) == mask) { 759a8599bd8SSage Weil dout("__ceph_caps_issued_mask %p snap issued %s" 760a8599bd8SSage Weil " (mask %s)\n", &ci->vfs_inode, 761a8599bd8SSage Weil ceph_cap_string(have), 762a8599bd8SSage Weil ceph_cap_string(mask)); 763a8599bd8SSage Weil return 1; 764a8599bd8SSage Weil } 765a8599bd8SSage Weil 766a8599bd8SSage Weil for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { 767a8599bd8SSage Weil cap = rb_entry(p, struct ceph_cap, ci_node); 768a8599bd8SSage Weil if (!__cap_is_valid(cap)) 769a8599bd8SSage Weil continue; 770a8599bd8SSage Weil if ((cap->issued & mask) == mask) { 771a8599bd8SSage Weil dout("__ceph_caps_issued_mask %p cap %p issued %s" 772a8599bd8SSage Weil " (mask %s)\n", &ci->vfs_inode, cap, 773a8599bd8SSage Weil ceph_cap_string(cap->issued), 774a8599bd8SSage Weil ceph_cap_string(mask)); 775a8599bd8SSage Weil if (touch) 776a8599bd8SSage Weil __touch_cap(cap); 777a8599bd8SSage Weil return 1; 778a8599bd8SSage Weil } 779a8599bd8SSage Weil 780a8599bd8SSage Weil /* does a combination of caps satisfy mask? */ 781a8599bd8SSage Weil have |= cap->issued; 782a8599bd8SSage Weil if ((have & mask) == mask) { 783a8599bd8SSage Weil dout("__ceph_caps_issued_mask %p combo issued %s" 784a8599bd8SSage Weil " (mask %s)\n", &ci->vfs_inode, 785a8599bd8SSage Weil ceph_cap_string(cap->issued), 786a8599bd8SSage Weil ceph_cap_string(mask)); 787a8599bd8SSage Weil if (touch) { 788a8599bd8SSage Weil struct rb_node *q; 789a8599bd8SSage Weil 79025985edcSLucas De Marchi /* touch this + preceding caps */ 791a8599bd8SSage Weil __touch_cap(cap); 792a8599bd8SSage Weil for (q = rb_first(&ci->i_caps); q != p; 793a8599bd8SSage Weil q = rb_next(q)) { 794a8599bd8SSage Weil cap = rb_entry(q, struct ceph_cap, 795a8599bd8SSage Weil ci_node); 796a8599bd8SSage Weil if (!__cap_is_valid(cap)) 797a8599bd8SSage Weil continue; 798a8599bd8SSage Weil __touch_cap(cap); 799a8599bd8SSage Weil } 800a8599bd8SSage Weil } 801a8599bd8SSage Weil return 1; 802a8599bd8SSage Weil } 803a8599bd8SSage Weil } 804a8599bd8SSage Weil 805a8599bd8SSage Weil return 0; 806a8599bd8SSage Weil } 807a8599bd8SSage Weil 808a8599bd8SSage Weil /* 809a8599bd8SSage Weil * Return true if mask caps are currently being revoked by an MDS. 810a8599bd8SSage Weil */ 8116ee6b953SYan, Zheng int __ceph_caps_revoking_other(struct ceph_inode_info *ci, 8126ee6b953SYan, Zheng struct ceph_cap *ocap, int mask) 8136ee6b953SYan, Zheng { 8146ee6b953SYan, Zheng struct ceph_cap *cap; 8156ee6b953SYan, Zheng struct rb_node *p; 8166ee6b953SYan, Zheng 8176ee6b953SYan, Zheng for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { 8186ee6b953SYan, Zheng cap = rb_entry(p, struct ceph_cap, ci_node); 8196ee6b953SYan, Zheng if (cap != ocap && __cap_is_valid(cap) && 8206ee6b953SYan, Zheng (cap->implemented & ~cap->issued & mask)) 8216ee6b953SYan, Zheng return 1; 8226ee6b953SYan, Zheng } 8236ee6b953SYan, Zheng return 0; 8246ee6b953SYan, Zheng } 8256ee6b953SYan, Zheng 826a8599bd8SSage Weil int ceph_caps_revoking(struct ceph_inode_info *ci, int mask) 827a8599bd8SSage Weil { 828a8599bd8SSage Weil struct inode *inode = &ci->vfs_inode; 8296ee6b953SYan, Zheng int ret; 830a8599bd8SSage Weil 831be655596SSage Weil spin_lock(&ci->i_ceph_lock); 8326ee6b953SYan, Zheng ret = __ceph_caps_revoking_other(ci, NULL, mask); 833be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 834a8599bd8SSage Weil dout("ceph_caps_revoking %p %s = %d\n", inode, 835a8599bd8SSage Weil ceph_cap_string(mask), ret); 836a8599bd8SSage Weil return ret; 837a8599bd8SSage Weil } 838a8599bd8SSage Weil 839a8599bd8SSage Weil int __ceph_caps_used(struct ceph_inode_info *ci) 840a8599bd8SSage Weil { 841a8599bd8SSage Weil int used = 0; 842a8599bd8SSage Weil if (ci->i_pin_ref) 843a8599bd8SSage Weil used |= CEPH_CAP_PIN; 844a8599bd8SSage Weil if (ci->i_rd_ref) 845a8599bd8SSage Weil used |= CEPH_CAP_FILE_RD; 846a43fb731SSage Weil if (ci->i_rdcache_ref || ci->vfs_inode.i_data.nrpages) 847a8599bd8SSage Weil used |= CEPH_CAP_FILE_CACHE; 848a8599bd8SSage Weil if (ci->i_wr_ref) 849a8599bd8SSage Weil used |= CEPH_CAP_FILE_WR; 850d3d0720dSHenry C Chang if (ci->i_wb_ref || ci->i_wrbuffer_ref) 851a8599bd8SSage Weil used |= CEPH_CAP_FILE_BUFFER; 852a8599bd8SSage Weil return used; 853a8599bd8SSage Weil } 854a8599bd8SSage Weil 855a8599bd8SSage Weil /* 856a8599bd8SSage Weil * wanted, by virtue of open file modes 857a8599bd8SSage Weil */ 858a8599bd8SSage Weil int __ceph_caps_file_wanted(struct ceph_inode_info *ci) 859a8599bd8SSage Weil { 860a8599bd8SSage Weil int want = 0; 861a8599bd8SSage Weil int mode; 86233caad32SSage Weil for (mode = 0; mode < CEPH_FILE_MODE_NUM; mode++) 863a8599bd8SSage Weil if (ci->i_nr_by_mode[mode]) 864a8599bd8SSage Weil want |= ceph_caps_for_mode(mode); 865a8599bd8SSage Weil return want; 866a8599bd8SSage Weil } 867a8599bd8SSage Weil 868a8599bd8SSage Weil /* 869a8599bd8SSage Weil * Return caps we have registered with the MDS(s) as 'wanted'. 870a8599bd8SSage Weil */ 871a8599bd8SSage Weil int __ceph_caps_mds_wanted(struct ceph_inode_info *ci) 872a8599bd8SSage Weil { 873a8599bd8SSage Weil struct ceph_cap *cap; 874a8599bd8SSage Weil struct rb_node *p; 875a8599bd8SSage Weil int mds_wanted = 0; 876a8599bd8SSage Weil 877a8599bd8SSage Weil for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { 878a8599bd8SSage Weil cap = rb_entry(p, struct ceph_cap, ci_node); 879a8599bd8SSage Weil if (!__cap_is_valid(cap)) 880a8599bd8SSage Weil continue; 881a8599bd8SSage Weil mds_wanted |= cap->mds_wanted; 882a8599bd8SSage Weil } 883a8599bd8SSage Weil return mds_wanted; 884a8599bd8SSage Weil } 885a8599bd8SSage Weil 886a8599bd8SSage Weil /* 887be655596SSage Weil * called under i_ceph_lock 888a8599bd8SSage Weil */ 889a8599bd8SSage Weil static int __ceph_is_any_caps(struct ceph_inode_info *ci) 890a8599bd8SSage Weil { 891a8599bd8SSage Weil return !RB_EMPTY_ROOT(&ci->i_caps) || ci->i_cap_exporting_mds >= 0; 892a8599bd8SSage Weil } 893a8599bd8SSage Weil 894a8599bd8SSage Weil /* 895f818a736SSage Weil * Remove a cap. Take steps to deal with a racing iterate_session_caps. 896f818a736SSage Weil * 897be655596SSage Weil * caller should hold i_ceph_lock. 898a6369741SSage Weil * caller will not hold session s_mutex if called from destroy_inode. 899a8599bd8SSage Weil */ 900a096b09aSYan, Zheng void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release) 901a8599bd8SSage Weil { 902a8599bd8SSage Weil struct ceph_mds_session *session = cap->session; 903a8599bd8SSage Weil struct ceph_inode_info *ci = cap->ci; 904640ef79dSCheng Renquan struct ceph_mds_client *mdsc = 9053d14c5d2SYehuda Sadeh ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc; 906f818a736SSage Weil int removed = 0; 907a8599bd8SSage Weil 908a8599bd8SSage Weil dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode); 909a8599bd8SSage Weil 9107c1332b8SSage Weil /* remove from session list */ 9117c1332b8SSage Weil spin_lock(&session->s_cap_lock); 91299a9c273SYan, Zheng /* 91399a9c273SYan, Zheng * s_cap_reconnect is protected by s_cap_lock. no one changes 91499a9c273SYan, Zheng * s_cap_gen while session is in the reconnect state. 91599a9c273SYan, Zheng */ 91699a9c273SYan, Zheng if (queue_release && 91799a9c273SYan, Zheng (!session->s_cap_reconnect || 91899a9c273SYan, Zheng cap->cap_gen == session->s_cap_gen)) 919a096b09aSYan, Zheng __queue_cap_release(session, ci->i_vino.ino, cap->cap_id, 920a096b09aSYan, Zheng cap->mseq, cap->issue_seq); 921a096b09aSYan, Zheng 9227c1332b8SSage Weil if (session->s_cap_iterator == cap) { 9237c1332b8SSage Weil /* not yet, we are iterating over this very cap */ 9247c1332b8SSage Weil dout("__ceph_remove_cap delaying %p removal from session %p\n", 9257c1332b8SSage Weil cap, cap->session); 9267c1332b8SSage Weil } else { 9277c1332b8SSage Weil list_del_init(&cap->session_caps); 9287c1332b8SSage Weil session->s_nr_caps--; 9297c1332b8SSage Weil cap->session = NULL; 930f818a736SSage Weil removed = 1; 9317c1332b8SSage Weil } 932f818a736SSage Weil /* protect backpointer with s_cap_lock: see iterate_session_caps */ 933f818a736SSage Weil cap->ci = NULL; 9347c1332b8SSage Weil spin_unlock(&session->s_cap_lock); 9357c1332b8SSage Weil 936f818a736SSage Weil /* remove from inode list */ 937f818a736SSage Weil rb_erase(&cap->ci_node, &ci->i_caps); 938f818a736SSage Weil if (ci->i_auth_cap == cap) 939f818a736SSage Weil ci->i_auth_cap = NULL; 940f818a736SSage Weil 941f818a736SSage Weil if (removed) 94237151668SYehuda Sadeh ceph_put_cap(mdsc, cap); 943a8599bd8SSage Weil 944a8599bd8SSage Weil if (!__ceph_is_any_caps(ci) && ci->i_snap_realm) { 945a8599bd8SSage Weil struct ceph_snap_realm *realm = ci->i_snap_realm; 946a8599bd8SSage Weil spin_lock(&realm->inodes_with_caps_lock); 947a8599bd8SSage Weil list_del_init(&ci->i_snap_realm_item); 948a8599bd8SSage Weil ci->i_snap_realm_counter++; 949a8599bd8SSage Weil ci->i_snap_realm = NULL; 950a8599bd8SSage Weil spin_unlock(&realm->inodes_with_caps_lock); 951a8599bd8SSage Weil ceph_put_snap_realm(mdsc, realm); 952a8599bd8SSage Weil } 953a8599bd8SSage Weil if (!__ceph_is_any_real_caps(ci)) 954a8599bd8SSage Weil __cap_delay_cancel(mdsc, ci); 955a8599bd8SSage Weil } 956a8599bd8SSage Weil 957a8599bd8SSage Weil /* 958a8599bd8SSage Weil * Build and send a cap message to the given MDS. 959a8599bd8SSage Weil * 960a8599bd8SSage Weil * Caller should be holding s_mutex. 961a8599bd8SSage Weil */ 962a8599bd8SSage Weil static int send_cap_msg(struct ceph_mds_session *session, 963a8599bd8SSage Weil u64 ino, u64 cid, int op, 964a8599bd8SSage Weil int caps, int wanted, int dirty, 965a8599bd8SSage Weil u32 seq, u64 flush_tid, u32 issue_seq, u32 mseq, 966a8599bd8SSage Weil u64 size, u64 max_size, 967a8599bd8SSage Weil struct timespec *mtime, struct timespec *atime, 968a8599bd8SSage Weil u64 time_warp_seq, 96905cb11c1SEric W. Biederman kuid_t uid, kgid_t gid, umode_t mode, 970a8599bd8SSage Weil u64 xattr_version, 971a8599bd8SSage Weil struct ceph_buffer *xattrs_buf, 972a8599bd8SSage Weil u64 follows) 973a8599bd8SSage Weil { 974a8599bd8SSage Weil struct ceph_mds_caps *fc; 975a8599bd8SSage Weil struct ceph_msg *msg; 976a8599bd8SSage Weil 977a8599bd8SSage Weil dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s" 978a8599bd8SSage Weil " seq %u/%u mseq %u follows %lld size %llu/%llu" 979a8599bd8SSage Weil " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(op), 980a8599bd8SSage Weil cid, ino, ceph_cap_string(caps), ceph_cap_string(wanted), 981a8599bd8SSage Weil ceph_cap_string(dirty), 982a8599bd8SSage Weil seq, issue_seq, mseq, follows, size, max_size, 983a8599bd8SSage Weil xattr_version, xattrs_buf ? (int)xattrs_buf->vec.iov_len : 0); 984a8599bd8SSage Weil 985b61c2763SSage Weil msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc), GFP_NOFS, false); 986a79832f2SSage Weil if (!msg) 987a79832f2SSage Weil return -ENOMEM; 988a8599bd8SSage Weil 9896df058c0SSage Weil msg->hdr.tid = cpu_to_le64(flush_tid); 990a8599bd8SSage Weil 9916df058c0SSage Weil fc = msg->front.iov_base; 992a8599bd8SSage Weil memset(fc, 0, sizeof(*fc)); 993a8599bd8SSage Weil 994a8599bd8SSage Weil fc->cap_id = cpu_to_le64(cid); 995a8599bd8SSage Weil fc->op = cpu_to_le32(op); 996a8599bd8SSage Weil fc->seq = cpu_to_le32(seq); 997a8599bd8SSage Weil fc->issue_seq = cpu_to_le32(issue_seq); 998a8599bd8SSage Weil fc->migrate_seq = cpu_to_le32(mseq); 999a8599bd8SSage Weil fc->caps = cpu_to_le32(caps); 1000a8599bd8SSage Weil fc->wanted = cpu_to_le32(wanted); 1001a8599bd8SSage Weil fc->dirty = cpu_to_le32(dirty); 1002a8599bd8SSage Weil fc->ino = cpu_to_le64(ino); 1003a8599bd8SSage Weil fc->snap_follows = cpu_to_le64(follows); 1004a8599bd8SSage Weil 1005a8599bd8SSage Weil fc->size = cpu_to_le64(size); 1006a8599bd8SSage Weil fc->max_size = cpu_to_le64(max_size); 1007a8599bd8SSage Weil if (mtime) 1008a8599bd8SSage Weil ceph_encode_timespec(&fc->mtime, mtime); 1009a8599bd8SSage Weil if (atime) 1010a8599bd8SSage Weil ceph_encode_timespec(&fc->atime, atime); 1011a8599bd8SSage Weil fc->time_warp_seq = cpu_to_le32(time_warp_seq); 1012a8599bd8SSage Weil 101305cb11c1SEric W. Biederman fc->uid = cpu_to_le32(from_kuid(&init_user_ns, uid)); 101405cb11c1SEric W. Biederman fc->gid = cpu_to_le32(from_kgid(&init_user_ns, gid)); 1015a8599bd8SSage Weil fc->mode = cpu_to_le32(mode); 1016a8599bd8SSage Weil 1017a8599bd8SSage Weil fc->xattr_version = cpu_to_le64(xattr_version); 1018a8599bd8SSage Weil if (xattrs_buf) { 1019a8599bd8SSage Weil msg->middle = ceph_buffer_get(xattrs_buf); 1020a8599bd8SSage Weil fc->xattr_len = cpu_to_le32(xattrs_buf->vec.iov_len); 1021a8599bd8SSage Weil msg->hdr.middle_len = cpu_to_le32(xattrs_buf->vec.iov_len); 1022a8599bd8SSage Weil } 1023a8599bd8SSage Weil 1024a8599bd8SSage Weil ceph_con_send(&session->s_con, msg); 1025a8599bd8SSage Weil return 0; 1026a8599bd8SSage Weil } 1027a8599bd8SSage Weil 1028d40ee0dcSYan, Zheng void __queue_cap_release(struct ceph_mds_session *session, 10293d7ded4dSSage Weil u64 ino, u64 cap_id, u32 migrate_seq, 10303d7ded4dSSage Weil u32 issue_seq) 10313d7ded4dSSage Weil { 10323d7ded4dSSage Weil struct ceph_msg *msg; 10333d7ded4dSSage Weil struct ceph_mds_cap_release *head; 10343d7ded4dSSage Weil struct ceph_mds_cap_item *item; 10353d7ded4dSSage Weil 10363d7ded4dSSage Weil BUG_ON(!session->s_num_cap_releases); 10373d7ded4dSSage Weil msg = list_first_entry(&session->s_cap_releases, 10383d7ded4dSSage Weil struct ceph_msg, list_head); 10393d7ded4dSSage Weil 10403d7ded4dSSage Weil dout(" adding %llx release to mds%d msg %p (%d left)\n", 10413d7ded4dSSage Weil ino, session->s_mds, msg, session->s_num_cap_releases); 10423d7ded4dSSage Weil 10433d7ded4dSSage Weil BUG_ON(msg->front.iov_len + sizeof(*item) > PAGE_CACHE_SIZE); 10443d7ded4dSSage Weil head = msg->front.iov_base; 1045b905a7f8SWei Yongjun le32_add_cpu(&head->num, 1); 10463d7ded4dSSage Weil item = msg->front.iov_base + msg->front.iov_len; 10473d7ded4dSSage Weil item->ino = cpu_to_le64(ino); 10483d7ded4dSSage Weil item->cap_id = cpu_to_le64(cap_id); 10493d7ded4dSSage Weil item->migrate_seq = cpu_to_le32(migrate_seq); 10503d7ded4dSSage Weil item->seq = cpu_to_le32(issue_seq); 10513d7ded4dSSage Weil 10523d7ded4dSSage Weil session->s_num_cap_releases--; 10533d7ded4dSSage Weil 10543d7ded4dSSage Weil msg->front.iov_len += sizeof(*item); 10553d7ded4dSSage Weil if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) { 10563d7ded4dSSage Weil dout(" release msg %p full\n", msg); 10573d7ded4dSSage Weil list_move_tail(&msg->list_head, &session->s_cap_releases_done); 10583d7ded4dSSage Weil } else { 10593d7ded4dSSage Weil dout(" release msg %p at %d/%d (%d)\n", msg, 10603d7ded4dSSage Weil (int)le32_to_cpu(head->num), 10613d7ded4dSSage Weil (int)CEPH_CAPS_PER_RELEASE, 10623d7ded4dSSage Weil (int)msg->front.iov_len); 10633d7ded4dSSage Weil } 10643d7ded4dSSage Weil } 10653d7ded4dSSage Weil 1066a8599bd8SSage Weil /* 1067a6369741SSage Weil * Queue cap releases when an inode is dropped from our cache. Since 1068be655596SSage Weil * inode is about to be destroyed, there is no need for i_ceph_lock. 1069a8599bd8SSage Weil */ 1070a8599bd8SSage Weil void ceph_queue_caps_release(struct inode *inode) 1071a8599bd8SSage Weil { 1072a8599bd8SSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 1073a8599bd8SSage Weil struct rb_node *p; 1074a8599bd8SSage Weil 1075a8599bd8SSage Weil p = rb_first(&ci->i_caps); 1076a8599bd8SSage Weil while (p) { 1077a8599bd8SSage Weil struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node); 1078a8599bd8SSage Weil p = rb_next(p); 1079a096b09aSYan, Zheng __ceph_remove_cap(cap, true); 1080a8599bd8SSage Weil } 1081a8599bd8SSage Weil } 1082a8599bd8SSage Weil 1083a8599bd8SSage Weil /* 1084a8599bd8SSage Weil * Send a cap msg on the given inode. Update our caps state, then 1085be655596SSage Weil * drop i_ceph_lock and send the message. 1086a8599bd8SSage Weil * 1087a8599bd8SSage Weil * Make note of max_size reported/requested from mds, revoked caps 1088a8599bd8SSage Weil * that have now been implemented. 1089a8599bd8SSage Weil * 1090a8599bd8SSage Weil * Make half-hearted attempt ot to invalidate page cache if we are 1091a8599bd8SSage Weil * dropping RDCACHE. Note that this will leave behind locked pages 1092a8599bd8SSage Weil * that we'll then need to deal with elsewhere. 1093a8599bd8SSage Weil * 1094a8599bd8SSage Weil * Return non-zero if delayed release, or we experienced an error 1095a8599bd8SSage Weil * such that the caller should requeue + retry later. 1096a8599bd8SSage Weil * 1097be655596SSage Weil * called with i_ceph_lock, then drops it. 1098a8599bd8SSage Weil * caller should hold snap_rwsem (read), s_mutex. 1099a8599bd8SSage Weil */ 1100a8599bd8SSage Weil static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap, 1101a8599bd8SSage Weil int op, int used, int want, int retain, int flushing, 1102a8599bd8SSage Weil unsigned *pflush_tid) 1103be655596SSage Weil __releases(cap->ci->i_ceph_lock) 1104a8599bd8SSage Weil { 1105a8599bd8SSage Weil struct ceph_inode_info *ci = cap->ci; 1106a8599bd8SSage Weil struct inode *inode = &ci->vfs_inode; 1107a8599bd8SSage Weil u64 cap_id = cap->cap_id; 110868c28323SSage Weil int held, revoking, dropping, keep; 1109a8599bd8SSage Weil u64 seq, issue_seq, mseq, time_warp_seq, follows; 1110a8599bd8SSage Weil u64 size, max_size; 1111a8599bd8SSage Weil struct timespec mtime, atime; 1112a8599bd8SSage Weil int wake = 0; 11135706b27dSAl Viro umode_t mode; 111405cb11c1SEric W. Biederman kuid_t uid; 111505cb11c1SEric W. Biederman kgid_t gid; 1116a8599bd8SSage Weil struct ceph_mds_session *session; 1117a8599bd8SSage Weil u64 xattr_version = 0; 1118082afec9SSage Weil struct ceph_buffer *xattr_blob = NULL; 1119a8599bd8SSage Weil int delayed = 0; 1120a8599bd8SSage Weil u64 flush_tid = 0; 1121a8599bd8SSage Weil int i; 1122a8599bd8SSage Weil int ret; 1123a8599bd8SSage Weil 112468c28323SSage Weil held = cap->issued | cap->implemented; 112568c28323SSage Weil revoking = cap->implemented & ~cap->issued; 112668c28323SSage Weil retain &= ~revoking; 112768c28323SSage Weil dropping = cap->issued & ~retain; 112868c28323SSage Weil 1129a8599bd8SSage Weil dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n", 1130a8599bd8SSage Weil inode, cap, cap->session, 1131a8599bd8SSage Weil ceph_cap_string(held), ceph_cap_string(held & retain), 1132a8599bd8SSage Weil ceph_cap_string(revoking)); 1133a8599bd8SSage Weil BUG_ON((retain & CEPH_CAP_PIN) == 0); 1134a8599bd8SSage Weil 1135a8599bd8SSage Weil session = cap->session; 1136a8599bd8SSage Weil 1137a8599bd8SSage Weil /* don't release wanted unless we've waited a bit. */ 1138a8599bd8SSage Weil if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 && 1139a8599bd8SSage Weil time_before(jiffies, ci->i_hold_caps_min)) { 1140a8599bd8SSage Weil dout(" delaying issued %s -> %s, wanted %s -> %s on send\n", 1141a8599bd8SSage Weil ceph_cap_string(cap->issued), 1142a8599bd8SSage Weil ceph_cap_string(cap->issued & retain), 1143a8599bd8SSage Weil ceph_cap_string(cap->mds_wanted), 1144a8599bd8SSage Weil ceph_cap_string(want)); 1145a8599bd8SSage Weil want |= cap->mds_wanted; 1146a8599bd8SSage Weil retain |= cap->issued; 1147a8599bd8SSage Weil delayed = 1; 1148a8599bd8SSage Weil } 1149a8599bd8SSage Weil ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH); 1150a8599bd8SSage Weil 1151a8599bd8SSage Weil cap->issued &= retain; /* drop bits we don't want */ 1152a8599bd8SSage Weil if (cap->implemented & ~cap->issued) { 1153a8599bd8SSage Weil /* 1154a8599bd8SSage Weil * Wake up any waiters on wanted -> needed transition. 1155a8599bd8SSage Weil * This is due to the weird transition from buffered 1156a8599bd8SSage Weil * to sync IO... we need to flush dirty pages _before_ 1157a8599bd8SSage Weil * allowing sync writes to avoid reordering. 1158a8599bd8SSage Weil */ 1159a8599bd8SSage Weil wake = 1; 1160a8599bd8SSage Weil } 1161a8599bd8SSage Weil cap->implemented &= cap->issued | used; 1162a8599bd8SSage Weil cap->mds_wanted = want; 1163a8599bd8SSage Weil 1164a8599bd8SSage Weil if (flushing) { 1165a8599bd8SSage Weil /* 1166a8599bd8SSage Weil * assign a tid for flush operations so we can avoid 1167a8599bd8SSage Weil * flush1 -> dirty1 -> flush2 -> flushack1 -> mark 1168a8599bd8SSage Weil * clean type races. track latest tid for every bit 1169a8599bd8SSage Weil * so we can handle flush AxFw, flush Fw, and have the 1170a8599bd8SSage Weil * first ack clean Ax. 1171a8599bd8SSage Weil */ 1172a8599bd8SSage Weil flush_tid = ++ci->i_cap_flush_last_tid; 1173a8599bd8SSage Weil if (pflush_tid) 1174a8599bd8SSage Weil *pflush_tid = flush_tid; 1175a8599bd8SSage Weil dout(" cap_flush_tid %d\n", (int)flush_tid); 1176a8599bd8SSage Weil for (i = 0; i < CEPH_CAP_BITS; i++) 1177a8599bd8SSage Weil if (flushing & (1 << i)) 1178a8599bd8SSage Weil ci->i_cap_flush_tid[i] = flush_tid; 11797d8cb26dSSage Weil 11807d8cb26dSSage Weil follows = ci->i_head_snapc->seq; 11817d8cb26dSSage Weil } else { 11827d8cb26dSSage Weil follows = 0; 1183a8599bd8SSage Weil } 1184a8599bd8SSage Weil 1185a8599bd8SSage Weil keep = cap->implemented; 1186a8599bd8SSage Weil seq = cap->seq; 1187a8599bd8SSage Weil issue_seq = cap->issue_seq; 1188a8599bd8SSage Weil mseq = cap->mseq; 1189a8599bd8SSage Weil size = inode->i_size; 1190a8599bd8SSage Weil ci->i_reported_size = size; 1191a8599bd8SSage Weil max_size = ci->i_wanted_max_size; 1192a8599bd8SSage Weil ci->i_requested_max_size = max_size; 1193a8599bd8SSage Weil mtime = inode->i_mtime; 1194a8599bd8SSage Weil atime = inode->i_atime; 1195a8599bd8SSage Weil time_warp_seq = ci->i_time_warp_seq; 1196a8599bd8SSage Weil uid = inode->i_uid; 1197a8599bd8SSage Weil gid = inode->i_gid; 1198a8599bd8SSage Weil mode = inode->i_mode; 1199a8599bd8SSage Weil 1200082afec9SSage Weil if (flushing & CEPH_CAP_XATTR_EXCL) { 1201a8599bd8SSage Weil __ceph_build_xattrs_blob(ci); 1202082afec9SSage Weil xattr_blob = ci->i_xattrs.blob; 1203082afec9SSage Weil xattr_version = ci->i_xattrs.version; 1204a8599bd8SSage Weil } 1205a8599bd8SSage Weil 1206be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1207a8599bd8SSage Weil 1208a8599bd8SSage Weil ret = send_cap_msg(session, ceph_vino(inode).ino, cap_id, 1209a8599bd8SSage Weil op, keep, want, flushing, seq, flush_tid, issue_seq, mseq, 1210a8599bd8SSage Weil size, max_size, &mtime, &atime, time_warp_seq, 1211082afec9SSage Weil uid, gid, mode, xattr_version, xattr_blob, 1212a8599bd8SSage Weil follows); 1213a8599bd8SSage Weil if (ret < 0) { 1214a8599bd8SSage Weil dout("error sending cap msg, must requeue %p\n", inode); 1215a8599bd8SSage Weil delayed = 1; 1216a8599bd8SSage Weil } 1217a8599bd8SSage Weil 1218a8599bd8SSage Weil if (wake) 121903066f23SYehuda Sadeh wake_up_all(&ci->i_cap_wq); 1220a8599bd8SSage Weil 1221a8599bd8SSage Weil return delayed; 1222a8599bd8SSage Weil } 1223a8599bd8SSage Weil 1224a8599bd8SSage Weil /* 1225a8599bd8SSage Weil * When a snapshot is taken, clients accumulate dirty metadata on 1226a8599bd8SSage Weil * inodes with capabilities in ceph_cap_snaps to describe the file 1227a8599bd8SSage Weil * state at the time the snapshot was taken. This must be flushed 1228a8599bd8SSage Weil * asynchronously back to the MDS once sync writes complete and dirty 1229a8599bd8SSage Weil * data is written out. 1230a8599bd8SSage Weil * 1231e835124cSSage Weil * Unless @again is true, skip cap_snaps that were already sent to 1232e835124cSSage Weil * the MDS (i.e., during this session). 1233e835124cSSage Weil * 1234be655596SSage Weil * Called under i_ceph_lock. Takes s_mutex as needed. 1235a8599bd8SSage Weil */ 1236a8599bd8SSage Weil void __ceph_flush_snaps(struct ceph_inode_info *ci, 1237e835124cSSage Weil struct ceph_mds_session **psession, 1238e835124cSSage Weil int again) 1239be655596SSage Weil __releases(ci->i_ceph_lock) 1240be655596SSage Weil __acquires(ci->i_ceph_lock) 1241a8599bd8SSage Weil { 1242a8599bd8SSage Weil struct inode *inode = &ci->vfs_inode; 1243a8599bd8SSage Weil int mds; 1244a8599bd8SSage Weil struct ceph_cap_snap *capsnap; 1245a8599bd8SSage Weil u32 mseq; 12463d14c5d2SYehuda Sadeh struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc; 1247a8599bd8SSage Weil struct ceph_mds_session *session = NULL; /* if session != NULL, we hold 1248a8599bd8SSage Weil session->s_mutex */ 1249a8599bd8SSage Weil u64 next_follows = 0; /* keep track of how far we've gotten through the 1250a8599bd8SSage Weil i_cap_snaps list, and skip these entries next time 1251a8599bd8SSage Weil around to avoid an infinite loop */ 1252a8599bd8SSage Weil 1253a8599bd8SSage Weil if (psession) 1254a8599bd8SSage Weil session = *psession; 1255a8599bd8SSage Weil 1256a8599bd8SSage Weil dout("__flush_snaps %p\n", inode); 1257a8599bd8SSage Weil retry: 1258a8599bd8SSage Weil list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { 1259a8599bd8SSage Weil /* avoid an infiniute loop after retry */ 1260a8599bd8SSage Weil if (capsnap->follows < next_follows) 1261a8599bd8SSage Weil continue; 1262a8599bd8SSage Weil /* 1263a8599bd8SSage Weil * we need to wait for sync writes to complete and for dirty 1264a8599bd8SSage Weil * pages to be written out. 1265a8599bd8SSage Weil */ 1266a8599bd8SSage Weil if (capsnap->dirty_pages || capsnap->writing) 1267cfc0bf66SSage Weil break; 1268a8599bd8SSage Weil 1269819ccbfaSSage Weil /* 1270819ccbfaSSage Weil * if cap writeback already occurred, we should have dropped 1271819ccbfaSSage Weil * the capsnap in ceph_put_wrbuffer_cap_refs. 1272819ccbfaSSage Weil */ 1273819ccbfaSSage Weil BUG_ON(capsnap->dirty == 0); 1274819ccbfaSSage Weil 1275a8599bd8SSage Weil /* pick mds, take s_mutex */ 1276ca81f3f6SSage Weil if (ci->i_auth_cap == NULL) { 1277ca81f3f6SSage Weil dout("no auth cap (migrating?), doing nothing\n"); 1278ca81f3f6SSage Weil goto out; 1279ca81f3f6SSage Weil } 1280e835124cSSage Weil 1281e835124cSSage Weil /* only flush each capsnap once */ 1282e835124cSSage Weil if (!again && !list_empty(&capsnap->flushing_item)) { 1283e835124cSSage Weil dout("already flushed %p, skipping\n", capsnap); 1284e835124cSSage Weil continue; 1285e835124cSSage Weil } 1286e835124cSSage Weil 1287ca81f3f6SSage Weil mds = ci->i_auth_cap->session->s_mds; 1288ca81f3f6SSage Weil mseq = ci->i_auth_cap->mseq; 1289ca81f3f6SSage Weil 1290a8599bd8SSage Weil if (session && session->s_mds != mds) { 1291a8599bd8SSage Weil dout("oops, wrong session %p mutex\n", session); 1292a8599bd8SSage Weil mutex_unlock(&session->s_mutex); 1293a8599bd8SSage Weil ceph_put_mds_session(session); 1294a8599bd8SSage Weil session = NULL; 1295a8599bd8SSage Weil } 1296a8599bd8SSage Weil if (!session) { 1297be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1298a8599bd8SSage Weil mutex_lock(&mdsc->mutex); 1299a8599bd8SSage Weil session = __ceph_lookup_mds_session(mdsc, mds); 1300a8599bd8SSage Weil mutex_unlock(&mdsc->mutex); 1301a8599bd8SSage Weil if (session) { 1302a8599bd8SSage Weil dout("inverting session/ino locks on %p\n", 1303a8599bd8SSage Weil session); 1304a8599bd8SSage Weil mutex_lock(&session->s_mutex); 1305a8599bd8SSage Weil } 1306a8599bd8SSage Weil /* 1307a8599bd8SSage Weil * if session == NULL, we raced against a cap 1308ca81f3f6SSage Weil * deletion or migration. retry, and we'll 1309ca81f3f6SSage Weil * get a better @mds value next time. 1310a8599bd8SSage Weil */ 1311be655596SSage Weil spin_lock(&ci->i_ceph_lock); 1312a8599bd8SSage Weil goto retry; 1313a8599bd8SSage Weil } 1314a8599bd8SSage Weil 1315a8599bd8SSage Weil capsnap->flush_tid = ++ci->i_cap_flush_last_tid; 1316a8599bd8SSage Weil atomic_inc(&capsnap->nref); 1317a8599bd8SSage Weil if (!list_empty(&capsnap->flushing_item)) 1318a8599bd8SSage Weil list_del_init(&capsnap->flushing_item); 1319a8599bd8SSage Weil list_add_tail(&capsnap->flushing_item, 1320a8599bd8SSage Weil &session->s_cap_snaps_flushing); 1321be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1322a8599bd8SSage Weil 1323cfc0bf66SSage Weil dout("flush_snaps %p cap_snap %p follows %lld tid %llu\n", 1324cfc0bf66SSage Weil inode, capsnap, capsnap->follows, capsnap->flush_tid); 1325a8599bd8SSage Weil send_cap_msg(session, ceph_vino(inode).ino, 0, 1326a8599bd8SSage Weil CEPH_CAP_OP_FLUSHSNAP, capsnap->issued, 0, 1327a8599bd8SSage Weil capsnap->dirty, 0, capsnap->flush_tid, 0, mseq, 1328a8599bd8SSage Weil capsnap->size, 0, 1329a8599bd8SSage Weil &capsnap->mtime, &capsnap->atime, 1330a8599bd8SSage Weil capsnap->time_warp_seq, 1331a8599bd8SSage Weil capsnap->uid, capsnap->gid, capsnap->mode, 13324a625be4SSage Weil capsnap->xattr_version, capsnap->xattr_blob, 1333a8599bd8SSage Weil capsnap->follows); 1334a8599bd8SSage Weil 1335a8599bd8SSage Weil next_follows = capsnap->follows + 1; 1336a8599bd8SSage Weil ceph_put_cap_snap(capsnap); 1337a8599bd8SSage Weil 1338be655596SSage Weil spin_lock(&ci->i_ceph_lock); 1339a8599bd8SSage Weil goto retry; 1340a8599bd8SSage Weil } 1341a8599bd8SSage Weil 1342a8599bd8SSage Weil /* we flushed them all; remove this inode from the queue */ 1343a8599bd8SSage Weil spin_lock(&mdsc->snap_flush_lock); 1344a8599bd8SSage Weil list_del_init(&ci->i_snap_flush_item); 1345a8599bd8SSage Weil spin_unlock(&mdsc->snap_flush_lock); 1346a8599bd8SSage Weil 1347ca81f3f6SSage Weil out: 1348a8599bd8SSage Weil if (psession) 1349a8599bd8SSage Weil *psession = session; 1350a8599bd8SSage Weil else if (session) { 1351a8599bd8SSage Weil mutex_unlock(&session->s_mutex); 1352a8599bd8SSage Weil ceph_put_mds_session(session); 1353a8599bd8SSage Weil } 1354a8599bd8SSage Weil } 1355a8599bd8SSage Weil 1356a8599bd8SSage Weil static void ceph_flush_snaps(struct ceph_inode_info *ci) 1357a8599bd8SSage Weil { 1358be655596SSage Weil spin_lock(&ci->i_ceph_lock); 1359e835124cSSage Weil __ceph_flush_snaps(ci, NULL, 0); 1360be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1361a8599bd8SSage Weil } 1362a8599bd8SSage Weil 1363a8599bd8SSage Weil /* 1364fca65b4aSSage Weil * Mark caps dirty. If inode is newly dirty, return the dirty flags. 1365fca65b4aSSage Weil * Caller is then responsible for calling __mark_inode_dirty with the 1366fca65b4aSSage Weil * returned flags value. 136776e3b390SSage Weil */ 1368fca65b4aSSage Weil int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask) 136976e3b390SSage Weil { 1370640ef79dSCheng Renquan struct ceph_mds_client *mdsc = 13713d14c5d2SYehuda Sadeh ceph_sb_to_client(ci->vfs_inode.i_sb)->mdsc; 137276e3b390SSage Weil struct inode *inode = &ci->vfs_inode; 137376e3b390SSage Weil int was = ci->i_dirty_caps; 137476e3b390SSage Weil int dirty = 0; 137576e3b390SSage Weil 137676e3b390SSage Weil dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode, 137776e3b390SSage Weil ceph_cap_string(mask), ceph_cap_string(was), 137876e3b390SSage Weil ceph_cap_string(was | mask)); 137976e3b390SSage Weil ci->i_dirty_caps |= mask; 138076e3b390SSage Weil if (was == 0) { 13817d8cb26dSSage Weil if (!ci->i_head_snapc) 13827d8cb26dSSage Weil ci->i_head_snapc = ceph_get_snap_context( 13837d8cb26dSSage Weil ci->i_snap_realm->cached_context); 13840685235fSYan, Zheng dout(" inode %p now dirty snapc %p auth cap %p\n", 13850685235fSYan, Zheng &ci->vfs_inode, ci->i_head_snapc, ci->i_auth_cap); 138676e3b390SSage Weil BUG_ON(!list_empty(&ci->i_dirty_item)); 138776e3b390SSage Weil spin_lock(&mdsc->cap_dirty_lock); 13880685235fSYan, Zheng if (ci->i_auth_cap) 138976e3b390SSage Weil list_add(&ci->i_dirty_item, &mdsc->cap_dirty); 13900685235fSYan, Zheng else 13910685235fSYan, Zheng list_add(&ci->i_dirty_item, 13920685235fSYan, Zheng &mdsc->cap_dirty_migrating); 139376e3b390SSage Weil spin_unlock(&mdsc->cap_dirty_lock); 139476e3b390SSage Weil if (ci->i_flushing_caps == 0) { 13953772d26dSSage Weil ihold(inode); 139676e3b390SSage Weil dirty |= I_DIRTY_SYNC; 139776e3b390SSage Weil } 139876e3b390SSage Weil } 139976e3b390SSage Weil BUG_ON(list_empty(&ci->i_dirty_item)); 140076e3b390SSage Weil if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) && 140176e3b390SSage Weil (mask & CEPH_CAP_FILE_BUFFER)) 140276e3b390SSage Weil dirty |= I_DIRTY_DATASYNC; 140376e3b390SSage Weil __cap_delay_requeue(mdsc, ci); 1404fca65b4aSSage Weil return dirty; 140576e3b390SSage Weil } 140676e3b390SSage Weil 140776e3b390SSage Weil /* 1408a8599bd8SSage Weil * Add dirty inode to the flushing list. Assigned a seq number so we 1409a8599bd8SSage Weil * can wait for caps to flush without starving. 1410cdc35f96SSage Weil * 1411be655596SSage Weil * Called under i_ceph_lock. 1412a8599bd8SSage Weil */ 1413cdc35f96SSage Weil static int __mark_caps_flushing(struct inode *inode, 1414a8599bd8SSage Weil struct ceph_mds_session *session) 1415a8599bd8SSage Weil { 14163d14c5d2SYehuda Sadeh struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc; 1417a8599bd8SSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 1418cdc35f96SSage Weil int flushing; 1419a8599bd8SSage Weil 1420cdc35f96SSage Weil BUG_ON(ci->i_dirty_caps == 0); 1421a8599bd8SSage Weil BUG_ON(list_empty(&ci->i_dirty_item)); 1422cdc35f96SSage Weil 1423cdc35f96SSage Weil flushing = ci->i_dirty_caps; 1424cdc35f96SSage Weil dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n", 1425cdc35f96SSage Weil ceph_cap_string(flushing), 1426cdc35f96SSage Weil ceph_cap_string(ci->i_flushing_caps), 1427cdc35f96SSage Weil ceph_cap_string(ci->i_flushing_caps | flushing)); 1428cdc35f96SSage Weil ci->i_flushing_caps |= flushing; 1429cdc35f96SSage Weil ci->i_dirty_caps = 0; 1430afcdaea3SSage Weil dout(" inode %p now !dirty\n", inode); 1431cdc35f96SSage Weil 1432a8599bd8SSage Weil spin_lock(&mdsc->cap_dirty_lock); 1433cdc35f96SSage Weil list_del_init(&ci->i_dirty_item); 1434afcdaea3SSage Weil 1435afcdaea3SSage Weil ci->i_cap_flush_seq = ++mdsc->cap_flush_seq; 1436afcdaea3SSage Weil if (list_empty(&ci->i_flushing_item)) { 1437a8599bd8SSage Weil list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing); 1438a8599bd8SSage Weil mdsc->num_cap_flushing++; 1439afcdaea3SSage Weil dout(" inode %p now flushing seq %lld\n", inode, 1440afcdaea3SSage Weil ci->i_cap_flush_seq); 1441afcdaea3SSage Weil } else { 1442afcdaea3SSage Weil list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing); 1443afcdaea3SSage Weil dout(" inode %p now flushing (more) seq %lld\n", inode, 1444a8599bd8SSage Weil ci->i_cap_flush_seq); 1445a8599bd8SSage Weil } 1446a8599bd8SSage Weil spin_unlock(&mdsc->cap_dirty_lock); 1447cdc35f96SSage Weil 1448cdc35f96SSage Weil return flushing; 1449a8599bd8SSage Weil } 1450a8599bd8SSage Weil 1451a8599bd8SSage Weil /* 14525ecad6fdSSage Weil * try to invalidate mapping pages without blocking. 14535ecad6fdSSage Weil */ 14545ecad6fdSSage Weil static int try_nonblocking_invalidate(struct inode *inode) 14555ecad6fdSSage Weil { 14565ecad6fdSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 14575ecad6fdSSage Weil u32 invalidating_gen = ci->i_rdcache_gen; 14585ecad6fdSSage Weil 1459be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 14605ecad6fdSSage Weil invalidate_mapping_pages(&inode->i_data, 0, -1); 1461be655596SSage Weil spin_lock(&ci->i_ceph_lock); 14625ecad6fdSSage Weil 146318a38193SSage Weil if (inode->i_data.nrpages == 0 && 14645ecad6fdSSage Weil invalidating_gen == ci->i_rdcache_gen) { 14655ecad6fdSSage Weil /* success. */ 14665ecad6fdSSage Weil dout("try_nonblocking_invalidate %p success\n", inode); 1467cd045cb4SSage Weil /* save any racing async invalidate some trouble */ 1468cd045cb4SSage Weil ci->i_rdcache_revoking = ci->i_rdcache_gen - 1; 14695ecad6fdSSage Weil return 0; 14705ecad6fdSSage Weil } 14715ecad6fdSSage Weil dout("try_nonblocking_invalidate %p failed\n", inode); 14725ecad6fdSSage Weil return -1; 14735ecad6fdSSage Weil } 14745ecad6fdSSage Weil 14755ecad6fdSSage Weil /* 1476a8599bd8SSage Weil * Swiss army knife function to examine currently used and wanted 1477a8599bd8SSage Weil * versus held caps. Release, flush, ack revoked caps to mds as 1478a8599bd8SSage Weil * appropriate. 1479a8599bd8SSage Weil * 1480a8599bd8SSage Weil * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay 1481a8599bd8SSage Weil * cap release further. 1482a8599bd8SSage Weil * CHECK_CAPS_AUTHONLY - we should only check the auth cap 1483a8599bd8SSage Weil * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without 1484a8599bd8SSage Weil * further delay. 1485a8599bd8SSage Weil */ 1486a8599bd8SSage Weil void ceph_check_caps(struct ceph_inode_info *ci, int flags, 1487a8599bd8SSage Weil struct ceph_mds_session *session) 1488a8599bd8SSage Weil { 14893d14c5d2SYehuda Sadeh struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode); 14903d14c5d2SYehuda Sadeh struct ceph_mds_client *mdsc = fsc->mdsc; 1491a8599bd8SSage Weil struct inode *inode = &ci->vfs_inode; 1492a8599bd8SSage Weil struct ceph_cap *cap; 1493395c312bSYan, Zheng int file_wanted, used, cap_used; 1494a8599bd8SSage Weil int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */ 1495cbd03635SSage Weil int issued, implemented, want, retain, revoking, flushing = 0; 1496a8599bd8SSage Weil int mds = -1; /* keep track of how far we've gone through i_caps list 1497a8599bd8SSage Weil to avoid an infinite loop on retry */ 1498a8599bd8SSage Weil struct rb_node *p; 1499a8599bd8SSage Weil int tried_invalidate = 0; 1500a8599bd8SSage Weil int delayed = 0, sent = 0, force_requeue = 0, num; 1501cbd03635SSage Weil int queue_invalidate = 0; 1502a8599bd8SSage Weil int is_delayed = flags & CHECK_CAPS_NODELAY; 1503a8599bd8SSage Weil 1504a8599bd8SSage Weil /* if we are unmounting, flush any unused caps immediately. */ 1505a8599bd8SSage Weil if (mdsc->stopping) 1506a8599bd8SSage Weil is_delayed = 1; 1507a8599bd8SSage Weil 1508be655596SSage Weil spin_lock(&ci->i_ceph_lock); 1509a8599bd8SSage Weil 1510a8599bd8SSage Weil if (ci->i_ceph_flags & CEPH_I_FLUSH) 1511a8599bd8SSage Weil flags |= CHECK_CAPS_FLUSH; 1512a8599bd8SSage Weil 1513a8599bd8SSage Weil /* flush snaps first time around only */ 1514a8599bd8SSage Weil if (!list_empty(&ci->i_cap_snaps)) 1515e835124cSSage Weil __ceph_flush_snaps(ci, &session, 0); 1516a8599bd8SSage Weil goto retry_locked; 1517a8599bd8SSage Weil retry: 1518be655596SSage Weil spin_lock(&ci->i_ceph_lock); 1519a8599bd8SSage Weil retry_locked: 1520a8599bd8SSage Weil file_wanted = __ceph_caps_file_wanted(ci); 1521a8599bd8SSage Weil used = __ceph_caps_used(ci); 1522a8599bd8SSage Weil want = file_wanted | used; 1523cbd03635SSage Weil issued = __ceph_caps_issued(ci, &implemented); 1524cbd03635SSage Weil revoking = implemented & ~issued; 1525a8599bd8SSage Weil 1526a8599bd8SSage Weil retain = want | CEPH_CAP_PIN; 1527a8599bd8SSage Weil if (!mdsc->stopping && inode->i_nlink > 0) { 1528a8599bd8SSage Weil if (want) { 1529a8599bd8SSage Weil retain |= CEPH_CAP_ANY; /* be greedy */ 1530a8599bd8SSage Weil } else { 1531a8599bd8SSage Weil retain |= CEPH_CAP_ANY_SHARED; 1532a8599bd8SSage Weil /* 1533a8599bd8SSage Weil * keep RD only if we didn't have the file open RW, 1534a8599bd8SSage Weil * because then the mds would revoke it anyway to 1535a8599bd8SSage Weil * journal max_size=0. 1536a8599bd8SSage Weil */ 1537a8599bd8SSage Weil if (ci->i_max_size == 0) 1538a8599bd8SSage Weil retain |= CEPH_CAP_ANY_RD; 1539a8599bd8SSage Weil } 1540a8599bd8SSage Weil } 1541a8599bd8SSage Weil 1542a8599bd8SSage Weil dout("check_caps %p file_want %s used %s dirty %s flushing %s" 1543cbd03635SSage Weil " issued %s revoking %s retain %s %s%s%s\n", inode, 1544a8599bd8SSage Weil ceph_cap_string(file_wanted), 1545a8599bd8SSage Weil ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps), 1546a8599bd8SSage Weil ceph_cap_string(ci->i_flushing_caps), 1547cbd03635SSage Weil ceph_cap_string(issued), ceph_cap_string(revoking), 1548a8599bd8SSage Weil ceph_cap_string(retain), 1549a8599bd8SSage Weil (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "", 1550a8599bd8SSage Weil (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "", 1551a8599bd8SSage Weil (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : ""); 1552a8599bd8SSage Weil 1553a8599bd8SSage Weil /* 1554a8599bd8SSage Weil * If we no longer need to hold onto old our caps, and we may 1555a8599bd8SSage Weil * have cached pages, but don't want them, then try to invalidate. 1556a8599bd8SSage Weil * If we fail, it's because pages are locked.... try again later. 1557a8599bd8SSage Weil */ 1558a8599bd8SSage Weil if ((!is_delayed || mdsc->stopping) && 1559a8599bd8SSage Weil ci->i_wrbuffer_ref == 0 && /* no dirty pages... */ 156093afd449SSage Weil inode->i_data.nrpages && /* have cached pages */ 1561cbd03635SSage Weil (file_wanted == 0 || /* no open files */ 15622962507cSSage Weil (revoking & (CEPH_CAP_FILE_CACHE| 15632962507cSSage Weil CEPH_CAP_FILE_LAZYIO))) && /* or revoking cache */ 1564a8599bd8SSage Weil !tried_invalidate) { 1565a8599bd8SSage Weil dout("check_caps trying to invalidate on %p\n", inode); 15665ecad6fdSSage Weil if (try_nonblocking_invalidate(inode) < 0) { 15672962507cSSage Weil if (revoking & (CEPH_CAP_FILE_CACHE| 15682962507cSSage Weil CEPH_CAP_FILE_LAZYIO)) { 1569cbd03635SSage Weil dout("check_caps queuing invalidate\n"); 1570cbd03635SSage Weil queue_invalidate = 1; 1571cbd03635SSage Weil ci->i_rdcache_revoking = ci->i_rdcache_gen; 1572a8599bd8SSage Weil } else { 1573a8599bd8SSage Weil dout("check_caps failed to invalidate pages\n"); 1574a8599bd8SSage Weil /* we failed to invalidate pages. check these 1575a8599bd8SSage Weil caps again later. */ 1576a8599bd8SSage Weil force_requeue = 1; 1577a8599bd8SSage Weil __cap_set_timeouts(mdsc, ci); 1578a8599bd8SSage Weil } 15795ecad6fdSSage Weil } 1580a8599bd8SSage Weil tried_invalidate = 1; 1581a8599bd8SSage Weil goto retry_locked; 1582a8599bd8SSage Weil } 1583a8599bd8SSage Weil 1584a8599bd8SSage Weil num = 0; 1585a8599bd8SSage Weil for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { 1586a8599bd8SSage Weil cap = rb_entry(p, struct ceph_cap, ci_node); 1587a8599bd8SSage Weil num++; 1588a8599bd8SSage Weil 1589a8599bd8SSage Weil /* avoid looping forever */ 1590a8599bd8SSage Weil if (mds >= cap->mds || 1591a8599bd8SSage Weil ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap)) 1592a8599bd8SSage Weil continue; 1593a8599bd8SSage Weil 1594a8599bd8SSage Weil /* NOTE: no side-effects allowed, until we take s_mutex */ 1595a8599bd8SSage Weil 1596395c312bSYan, Zheng cap_used = used; 1597395c312bSYan, Zheng if (ci->i_auth_cap && cap != ci->i_auth_cap) 1598395c312bSYan, Zheng cap_used &= ~ci->i_auth_cap->issued; 1599395c312bSYan, Zheng 1600a8599bd8SSage Weil revoking = cap->implemented & ~cap->issued; 1601395c312bSYan, Zheng dout(" mds%d cap %p used %s issued %s implemented %s revoking %s\n", 1602088b3f5eSSage Weil cap->mds, cap, ceph_cap_string(cap->issued), 1603395c312bSYan, Zheng ceph_cap_string(cap_used), 1604088b3f5eSSage Weil ceph_cap_string(cap->implemented), 1605a8599bd8SSage Weil ceph_cap_string(revoking)); 1606a8599bd8SSage Weil 1607a8599bd8SSage Weil if (cap == ci->i_auth_cap && 1608a8599bd8SSage Weil (cap->issued & CEPH_CAP_FILE_WR)) { 1609a8599bd8SSage Weil /* request larger max_size from MDS? */ 1610a8599bd8SSage Weil if (ci->i_wanted_max_size > ci->i_max_size && 1611a8599bd8SSage Weil ci->i_wanted_max_size > ci->i_requested_max_size) { 1612a8599bd8SSage Weil dout("requesting new max_size\n"); 1613a8599bd8SSage Weil goto ack; 1614a8599bd8SSage Weil } 1615a8599bd8SSage Weil 1616a8599bd8SSage Weil /* approaching file_max? */ 1617a8599bd8SSage Weil if ((inode->i_size << 1) >= ci->i_max_size && 1618a8599bd8SSage Weil (ci->i_reported_size << 1) < ci->i_max_size) { 1619a8599bd8SSage Weil dout("i_size approaching max_size\n"); 1620a8599bd8SSage Weil goto ack; 1621a8599bd8SSage Weil } 1622a8599bd8SSage Weil } 1623a8599bd8SSage Weil /* flush anything dirty? */ 1624a8599bd8SSage Weil if (cap == ci->i_auth_cap && (flags & CHECK_CAPS_FLUSH) && 1625a8599bd8SSage Weil ci->i_dirty_caps) { 1626a8599bd8SSage Weil dout("flushing dirty caps\n"); 1627a8599bd8SSage Weil goto ack; 1628a8599bd8SSage Weil } 1629a8599bd8SSage Weil 1630a8599bd8SSage Weil /* completed revocation? going down and there are no caps? */ 1631395c312bSYan, Zheng if (revoking && (revoking & cap_used) == 0) { 1632a8599bd8SSage Weil dout("completed revocation of %s\n", 1633a8599bd8SSage Weil ceph_cap_string(cap->implemented & ~cap->issued)); 1634a8599bd8SSage Weil goto ack; 1635a8599bd8SSage Weil } 1636a8599bd8SSage Weil 1637a8599bd8SSage Weil /* want more caps from mds? */ 1638a8599bd8SSage Weil if (want & ~(cap->mds_wanted | cap->issued)) 1639a8599bd8SSage Weil goto ack; 1640a8599bd8SSage Weil 1641a8599bd8SSage Weil /* things we might delay */ 1642a8599bd8SSage Weil if ((cap->issued & ~retain) == 0 && 1643a8599bd8SSage Weil cap->mds_wanted == want) 1644a8599bd8SSage Weil continue; /* nope, all good */ 1645a8599bd8SSage Weil 1646a8599bd8SSage Weil if (is_delayed) 1647a8599bd8SSage Weil goto ack; 1648a8599bd8SSage Weil 1649a8599bd8SSage Weil /* delay? */ 1650a8599bd8SSage Weil if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 && 1651a8599bd8SSage Weil time_before(jiffies, ci->i_hold_caps_max)) { 1652a8599bd8SSage Weil dout(" delaying issued %s -> %s, wanted %s -> %s\n", 1653a8599bd8SSage Weil ceph_cap_string(cap->issued), 1654a8599bd8SSage Weil ceph_cap_string(cap->issued & retain), 1655a8599bd8SSage Weil ceph_cap_string(cap->mds_wanted), 1656a8599bd8SSage Weil ceph_cap_string(want)); 1657a8599bd8SSage Weil delayed++; 1658a8599bd8SSage Weil continue; 1659a8599bd8SSage Weil } 1660a8599bd8SSage Weil 1661a8599bd8SSage Weil ack: 1662e9964c10SSage Weil if (ci->i_ceph_flags & CEPH_I_NOFLUSH) { 1663e9964c10SSage Weil dout(" skipping %p I_NOFLUSH set\n", inode); 1664e9964c10SSage Weil continue; 1665e9964c10SSage Weil } 1666e9964c10SSage Weil 1667a8599bd8SSage Weil if (session && session != cap->session) { 1668a8599bd8SSage Weil dout("oops, wrong session %p mutex\n", session); 1669a8599bd8SSage Weil mutex_unlock(&session->s_mutex); 1670a8599bd8SSage Weil session = NULL; 1671a8599bd8SSage Weil } 1672a8599bd8SSage Weil if (!session) { 1673a8599bd8SSage Weil session = cap->session; 1674a8599bd8SSage Weil if (mutex_trylock(&session->s_mutex) == 0) { 1675a8599bd8SSage Weil dout("inverting session/ino locks on %p\n", 1676a8599bd8SSage Weil session); 1677be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1678a8599bd8SSage Weil if (took_snap_rwsem) { 1679a8599bd8SSage Weil up_read(&mdsc->snap_rwsem); 1680a8599bd8SSage Weil took_snap_rwsem = 0; 1681a8599bd8SSage Weil } 1682a8599bd8SSage Weil mutex_lock(&session->s_mutex); 1683a8599bd8SSage Weil goto retry; 1684a8599bd8SSage Weil } 1685a8599bd8SSage Weil } 1686a8599bd8SSage Weil /* take snap_rwsem after session mutex */ 1687a8599bd8SSage Weil if (!took_snap_rwsem) { 1688a8599bd8SSage Weil if (down_read_trylock(&mdsc->snap_rwsem) == 0) { 1689a8599bd8SSage Weil dout("inverting snap/in locks on %p\n", 1690a8599bd8SSage Weil inode); 1691be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1692a8599bd8SSage Weil down_read(&mdsc->snap_rwsem); 1693a8599bd8SSage Weil took_snap_rwsem = 1; 1694a8599bd8SSage Weil goto retry; 1695a8599bd8SSage Weil } 1696a8599bd8SSage Weil took_snap_rwsem = 1; 1697a8599bd8SSage Weil } 1698a8599bd8SSage Weil 1699cdc35f96SSage Weil if (cap == ci->i_auth_cap && ci->i_dirty_caps) 1700cdc35f96SSage Weil flushing = __mark_caps_flushing(inode, session); 170124be0c48SSage Weil else 170224be0c48SSage Weil flushing = 0; 1703a8599bd8SSage Weil 1704a8599bd8SSage Weil mds = cap->mds; /* remember mds, so we don't repeat */ 1705a8599bd8SSage Weil sent++; 1706a8599bd8SSage Weil 1707be655596SSage Weil /* __send_cap drops i_ceph_lock */ 1708395c312bSYan, Zheng delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, cap_used, 1709395c312bSYan, Zheng want, retain, flushing, NULL); 1710be655596SSage Weil goto retry; /* retake i_ceph_lock and restart our cap scan. */ 1711a8599bd8SSage Weil } 1712a8599bd8SSage Weil 1713a8599bd8SSage Weil /* 1714a8599bd8SSage Weil * Reschedule delayed caps release if we delayed anything, 1715a8599bd8SSage Weil * otherwise cancel. 1716a8599bd8SSage Weil */ 1717a8599bd8SSage Weil if (delayed && is_delayed) 1718a8599bd8SSage Weil force_requeue = 1; /* __send_cap delayed release; requeue */ 1719a8599bd8SSage Weil if (!delayed && !is_delayed) 1720a8599bd8SSage Weil __cap_delay_cancel(mdsc, ci); 1721a8599bd8SSage Weil else if (!is_delayed || force_requeue) 1722a8599bd8SSage Weil __cap_delay_requeue(mdsc, ci); 1723a8599bd8SSage Weil 1724be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1725a8599bd8SSage Weil 1726cbd03635SSage Weil if (queue_invalidate) 17273c6f6b79SSage Weil ceph_queue_invalidate(inode); 1728cbd03635SSage Weil 1729cdc2ce05SSage Weil if (session) 1730a8599bd8SSage Weil mutex_unlock(&session->s_mutex); 1731a8599bd8SSage Weil if (took_snap_rwsem) 1732a8599bd8SSage Weil up_read(&mdsc->snap_rwsem); 1733a8599bd8SSage Weil } 1734a8599bd8SSage Weil 1735a8599bd8SSage Weil /* 1736a8599bd8SSage Weil * Try to flush dirty caps back to the auth mds. 1737a8599bd8SSage Weil */ 17384fe59789SYan, Zheng static int try_flush_caps(struct inode *inode, unsigned *flush_tid) 1739a8599bd8SSage Weil { 17403d14c5d2SYehuda Sadeh struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc; 1741a8599bd8SSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 1742a8599bd8SSage Weil int flushing = 0; 17434fe59789SYan, Zheng struct ceph_mds_session *session = NULL; 1744a8599bd8SSage Weil 1745a8599bd8SSage Weil retry: 1746be655596SSage Weil spin_lock(&ci->i_ceph_lock); 1747e9964c10SSage Weil if (ci->i_ceph_flags & CEPH_I_NOFLUSH) { 1748e9964c10SSage Weil dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode); 1749e9964c10SSage Weil goto out; 1750e9964c10SSage Weil } 1751a8599bd8SSage Weil if (ci->i_dirty_caps && ci->i_auth_cap) { 1752a8599bd8SSage Weil struct ceph_cap *cap = ci->i_auth_cap; 1753a8599bd8SSage Weil int used = __ceph_caps_used(ci); 1754a8599bd8SSage Weil int want = __ceph_caps_wanted(ci); 1755a8599bd8SSage Weil int delayed; 1756a8599bd8SSage Weil 17574fe59789SYan, Zheng if (!session || session != cap->session) { 1758be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 17594fe59789SYan, Zheng if (session) 17604fe59789SYan, Zheng mutex_unlock(&session->s_mutex); 1761a8599bd8SSage Weil session = cap->session; 1762a8599bd8SSage Weil mutex_lock(&session->s_mutex); 1763a8599bd8SSage Weil goto retry; 1764a8599bd8SSage Weil } 1765a8599bd8SSage Weil if (cap->session->s_state < CEPH_MDS_SESSION_OPEN) 1766a8599bd8SSage Weil goto out; 1767a8599bd8SSage Weil 1768cdc35f96SSage Weil flushing = __mark_caps_flushing(inode, session); 1769a8599bd8SSage Weil 1770be655596SSage Weil /* __send_cap drops i_ceph_lock */ 1771a8599bd8SSage Weil delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, used, want, 1772a8599bd8SSage Weil cap->issued | cap->implemented, flushing, 1773a8599bd8SSage Weil flush_tid); 1774a8599bd8SSage Weil if (!delayed) 1775a8599bd8SSage Weil goto out_unlocked; 1776a8599bd8SSage Weil 1777be655596SSage Weil spin_lock(&ci->i_ceph_lock); 1778a8599bd8SSage Weil __cap_delay_requeue(mdsc, ci); 1779a8599bd8SSage Weil } 1780a8599bd8SSage Weil out: 1781be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1782a8599bd8SSage Weil out_unlocked: 17834fe59789SYan, Zheng if (session) 1784a8599bd8SSage Weil mutex_unlock(&session->s_mutex); 1785a8599bd8SSage Weil return flushing; 1786a8599bd8SSage Weil } 1787a8599bd8SSage Weil 1788a8599bd8SSage Weil /* 1789a8599bd8SSage Weil * Return true if we've flushed caps through the given flush_tid. 1790a8599bd8SSage Weil */ 1791a8599bd8SSage Weil static int caps_are_flushed(struct inode *inode, unsigned tid) 1792a8599bd8SSage Weil { 1793a8599bd8SSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 1794a5ee751cSDan Carpenter int i, ret = 1; 1795a8599bd8SSage Weil 1796be655596SSage Weil spin_lock(&ci->i_ceph_lock); 1797a8599bd8SSage Weil for (i = 0; i < CEPH_CAP_BITS; i++) 1798a8599bd8SSage Weil if ((ci->i_flushing_caps & (1 << i)) && 1799a8599bd8SSage Weil ci->i_cap_flush_tid[i] <= tid) { 1800a8599bd8SSage Weil /* still flushing this bit */ 1801a8599bd8SSage Weil ret = 0; 1802a8599bd8SSage Weil break; 1803a8599bd8SSage Weil } 1804be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1805a8599bd8SSage Weil return ret; 1806a8599bd8SSage Weil } 1807a8599bd8SSage Weil 1808a8599bd8SSage Weil /* 1809a8599bd8SSage Weil * Wait on any unsafe replies for the given inode. First wait on the 1810a8599bd8SSage Weil * newest request, and make that the upper bound. Then, if there are 1811a8599bd8SSage Weil * more requests, keep waiting on the oldest as long as it is still older 1812a8599bd8SSage Weil * than the original request. 1813a8599bd8SSage Weil */ 1814a8599bd8SSage Weil static void sync_write_wait(struct inode *inode) 1815a8599bd8SSage Weil { 1816a8599bd8SSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 1817a8599bd8SSage Weil struct list_head *head = &ci->i_unsafe_writes; 1818a8599bd8SSage Weil struct ceph_osd_request *req; 1819a8599bd8SSage Weil u64 last_tid; 1820a8599bd8SSage Weil 1821a8599bd8SSage Weil spin_lock(&ci->i_unsafe_lock); 1822a8599bd8SSage Weil if (list_empty(head)) 1823a8599bd8SSage Weil goto out; 1824a8599bd8SSage Weil 1825a8599bd8SSage Weil /* set upper bound as _last_ entry in chain */ 1826a8599bd8SSage Weil req = list_entry(head->prev, struct ceph_osd_request, 1827a8599bd8SSage Weil r_unsafe_item); 1828a8599bd8SSage Weil last_tid = req->r_tid; 1829a8599bd8SSage Weil 1830a8599bd8SSage Weil do { 1831a8599bd8SSage Weil ceph_osdc_get_request(req); 1832a8599bd8SSage Weil spin_unlock(&ci->i_unsafe_lock); 1833a8599bd8SSage Weil dout("sync_write_wait on tid %llu (until %llu)\n", 1834a8599bd8SSage Weil req->r_tid, last_tid); 1835a8599bd8SSage Weil wait_for_completion(&req->r_safe_completion); 1836a8599bd8SSage Weil spin_lock(&ci->i_unsafe_lock); 1837a8599bd8SSage Weil ceph_osdc_put_request(req); 1838a8599bd8SSage Weil 1839a8599bd8SSage Weil /* 1840a8599bd8SSage Weil * from here on look at first entry in chain, since we 1841a8599bd8SSage Weil * only want to wait for anything older than last_tid 1842a8599bd8SSage Weil */ 1843a8599bd8SSage Weil if (list_empty(head)) 1844a8599bd8SSage Weil break; 1845a8599bd8SSage Weil req = list_entry(head->next, struct ceph_osd_request, 1846a8599bd8SSage Weil r_unsafe_item); 1847a8599bd8SSage Weil } while (req->r_tid < last_tid); 1848a8599bd8SSage Weil out: 1849a8599bd8SSage Weil spin_unlock(&ci->i_unsafe_lock); 1850a8599bd8SSage Weil } 1851a8599bd8SSage Weil 185202c24a82SJosef Bacik int ceph_fsync(struct file *file, loff_t start, loff_t end, int datasync) 1853a8599bd8SSage Weil { 18547ea80859SChristoph Hellwig struct inode *inode = file->f_mapping->host; 1855a8599bd8SSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 1856a8599bd8SSage Weil unsigned flush_tid; 1857a8599bd8SSage Weil int ret; 1858a8599bd8SSage Weil int dirty; 1859a8599bd8SSage Weil 1860a8599bd8SSage Weil dout("fsync %p%s\n", inode, datasync ? " datasync" : ""); 1861a8599bd8SSage Weil sync_write_wait(inode); 1862a8599bd8SSage Weil 186302c24a82SJosef Bacik ret = filemap_write_and_wait_range(inode->i_mapping, start, end); 1864a8599bd8SSage Weil if (ret < 0) 1865a8599bd8SSage Weil return ret; 186602c24a82SJosef Bacik mutex_lock(&inode->i_mutex); 1867a8599bd8SSage Weil 18684fe59789SYan, Zheng dirty = try_flush_caps(inode, &flush_tid); 1869a8599bd8SSage Weil dout("fsync dirty caps are %s\n", ceph_cap_string(dirty)); 1870a8599bd8SSage Weil 1871a8599bd8SSage Weil /* 1872a8599bd8SSage Weil * only wait on non-file metadata writeback (the mds 1873a8599bd8SSage Weil * can recover size and mtime, so we don't need to 1874a8599bd8SSage Weil * wait for that) 1875a8599bd8SSage Weil */ 1876a8599bd8SSage Weil if (!datasync && (dirty & ~CEPH_CAP_ANY_FILE_WR)) { 1877a8599bd8SSage Weil dout("fsync waiting for flush_tid %u\n", flush_tid); 1878a8599bd8SSage Weil ret = wait_event_interruptible(ci->i_cap_wq, 1879a8599bd8SSage Weil caps_are_flushed(inode, flush_tid)); 1880a8599bd8SSage Weil } 1881a8599bd8SSage Weil 1882a8599bd8SSage Weil dout("fsync %p%s done\n", inode, datasync ? " datasync" : ""); 188302c24a82SJosef Bacik mutex_unlock(&inode->i_mutex); 1884a8599bd8SSage Weil return ret; 1885a8599bd8SSage Weil } 1886a8599bd8SSage Weil 1887a8599bd8SSage Weil /* 1888a8599bd8SSage Weil * Flush any dirty caps back to the mds. If we aren't asked to wait, 1889a8599bd8SSage Weil * queue inode for flush but don't do so immediately, because we can 1890a8599bd8SSage Weil * get by with fewer MDS messages if we wait for data writeback to 1891a8599bd8SSage Weil * complete first. 1892a8599bd8SSage Weil */ 1893f1a3d572SStephen Rothwell int ceph_write_inode(struct inode *inode, struct writeback_control *wbc) 1894a8599bd8SSage Weil { 1895a8599bd8SSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 1896a8599bd8SSage Weil unsigned flush_tid; 1897a8599bd8SSage Weil int err = 0; 1898a8599bd8SSage Weil int dirty; 1899f1a3d572SStephen Rothwell int wait = wbc->sync_mode == WB_SYNC_ALL; 1900a8599bd8SSage Weil 1901a8599bd8SSage Weil dout("write_inode %p wait=%d\n", inode, wait); 1902a8599bd8SSage Weil if (wait) { 19034fe59789SYan, Zheng dirty = try_flush_caps(inode, &flush_tid); 1904a8599bd8SSage Weil if (dirty) 1905a8599bd8SSage Weil err = wait_event_interruptible(ci->i_cap_wq, 1906a8599bd8SSage Weil caps_are_flushed(inode, flush_tid)); 1907a8599bd8SSage Weil } else { 1908640ef79dSCheng Renquan struct ceph_mds_client *mdsc = 19093d14c5d2SYehuda Sadeh ceph_sb_to_client(inode->i_sb)->mdsc; 1910a8599bd8SSage Weil 1911be655596SSage Weil spin_lock(&ci->i_ceph_lock); 1912a8599bd8SSage Weil if (__ceph_caps_dirty(ci)) 1913a8599bd8SSage Weil __cap_delay_requeue_front(mdsc, ci); 1914be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1915a8599bd8SSage Weil } 1916a8599bd8SSage Weil return err; 1917a8599bd8SSage Weil } 1918a8599bd8SSage Weil 1919a8599bd8SSage Weil /* 1920a8599bd8SSage Weil * After a recovering MDS goes active, we need to resend any caps 1921a8599bd8SSage Weil * we were flushing. 1922a8599bd8SSage Weil * 1923a8599bd8SSage Weil * Caller holds session->s_mutex. 1924a8599bd8SSage Weil */ 1925a8599bd8SSage Weil static void kick_flushing_capsnaps(struct ceph_mds_client *mdsc, 1926a8599bd8SSage Weil struct ceph_mds_session *session) 1927a8599bd8SSage Weil { 1928a8599bd8SSage Weil struct ceph_cap_snap *capsnap; 1929a8599bd8SSage Weil 1930a8599bd8SSage Weil dout("kick_flushing_capsnaps mds%d\n", session->s_mds); 1931a8599bd8SSage Weil list_for_each_entry(capsnap, &session->s_cap_snaps_flushing, 1932a8599bd8SSage Weil flushing_item) { 1933a8599bd8SSage Weil struct ceph_inode_info *ci = capsnap->ci; 1934a8599bd8SSage Weil struct inode *inode = &ci->vfs_inode; 1935a8599bd8SSage Weil struct ceph_cap *cap; 1936a8599bd8SSage Weil 1937be655596SSage Weil spin_lock(&ci->i_ceph_lock); 1938a8599bd8SSage Weil cap = ci->i_auth_cap; 1939a8599bd8SSage Weil if (cap && cap->session == session) { 1940a8599bd8SSage Weil dout("kick_flushing_caps %p cap %p capsnap %p\n", inode, 1941a8599bd8SSage Weil cap, capsnap); 1942e835124cSSage Weil __ceph_flush_snaps(ci, &session, 1); 1943a8599bd8SSage Weil } else { 1944a8599bd8SSage Weil pr_err("%p auth cap %p not mds%d ???\n", inode, 1945a8599bd8SSage Weil cap, session->s_mds); 1946a8599bd8SSage Weil } 1947be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1948a8599bd8SSage Weil } 1949a8599bd8SSage Weil } 1950a8599bd8SSage Weil 1951a8599bd8SSage Weil void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc, 1952a8599bd8SSage Weil struct ceph_mds_session *session) 1953a8599bd8SSage Weil { 1954a8599bd8SSage Weil struct ceph_inode_info *ci; 1955a8599bd8SSage Weil 1956a8599bd8SSage Weil kick_flushing_capsnaps(mdsc, session); 1957a8599bd8SSage Weil 1958a8599bd8SSage Weil dout("kick_flushing_caps mds%d\n", session->s_mds); 1959a8599bd8SSage Weil list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) { 1960a8599bd8SSage Weil struct inode *inode = &ci->vfs_inode; 1961a8599bd8SSage Weil struct ceph_cap *cap; 1962a8599bd8SSage Weil int delayed = 0; 1963a8599bd8SSage Weil 1964be655596SSage Weil spin_lock(&ci->i_ceph_lock); 1965a8599bd8SSage Weil cap = ci->i_auth_cap; 1966a8599bd8SSage Weil if (cap && cap->session == session) { 1967a8599bd8SSage Weil dout("kick_flushing_caps %p cap %p %s\n", inode, 1968a8599bd8SSage Weil cap, ceph_cap_string(ci->i_flushing_caps)); 1969a8599bd8SSage Weil delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, 1970a8599bd8SSage Weil __ceph_caps_used(ci), 1971a8599bd8SSage Weil __ceph_caps_wanted(ci), 1972a8599bd8SSage Weil cap->issued | cap->implemented, 1973a8599bd8SSage Weil ci->i_flushing_caps, NULL); 1974a8599bd8SSage Weil if (delayed) { 1975be655596SSage Weil spin_lock(&ci->i_ceph_lock); 1976a8599bd8SSage Weil __cap_delay_requeue(mdsc, ci); 1977be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1978a8599bd8SSage Weil } 1979a8599bd8SSage Weil } else { 1980a8599bd8SSage Weil pr_err("%p auth cap %p not mds%d ???\n", inode, 1981a8599bd8SSage Weil cap, session->s_mds); 1982be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 1983a8599bd8SSage Weil } 1984a8599bd8SSage Weil } 1985a8599bd8SSage Weil } 1986a8599bd8SSage Weil 1987088b3f5eSSage Weil static void kick_flushing_inode_caps(struct ceph_mds_client *mdsc, 1988088b3f5eSSage Weil struct ceph_mds_session *session, 1989088b3f5eSSage Weil struct inode *inode) 1990088b3f5eSSage Weil { 1991088b3f5eSSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 1992088b3f5eSSage Weil struct ceph_cap *cap; 1993088b3f5eSSage Weil int delayed = 0; 1994088b3f5eSSage Weil 1995be655596SSage Weil spin_lock(&ci->i_ceph_lock); 1996088b3f5eSSage Weil cap = ci->i_auth_cap; 1997088b3f5eSSage Weil dout("kick_flushing_inode_caps %p flushing %s flush_seq %lld\n", inode, 1998088b3f5eSSage Weil ceph_cap_string(ci->i_flushing_caps), ci->i_cap_flush_seq); 1999005c4697SYan, Zheng 2000088b3f5eSSage Weil __ceph_flush_snaps(ci, &session, 1); 2001005c4697SYan, Zheng 2002088b3f5eSSage Weil if (ci->i_flushing_caps) { 2003005c4697SYan, Zheng spin_lock(&mdsc->cap_dirty_lock); 2004005c4697SYan, Zheng list_move_tail(&ci->i_flushing_item, 2005005c4697SYan, Zheng &cap->session->s_cap_flushing); 2006005c4697SYan, Zheng spin_unlock(&mdsc->cap_dirty_lock); 2007005c4697SYan, Zheng 2008088b3f5eSSage Weil delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, 2009088b3f5eSSage Weil __ceph_caps_used(ci), 2010088b3f5eSSage Weil __ceph_caps_wanted(ci), 2011088b3f5eSSage Weil cap->issued | cap->implemented, 2012088b3f5eSSage Weil ci->i_flushing_caps, NULL); 2013088b3f5eSSage Weil if (delayed) { 2014be655596SSage Weil spin_lock(&ci->i_ceph_lock); 2015088b3f5eSSage Weil __cap_delay_requeue(mdsc, ci); 2016be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 2017088b3f5eSSage Weil } 2018088b3f5eSSage Weil } else { 2019be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 2020088b3f5eSSage Weil } 2021088b3f5eSSage Weil } 2022088b3f5eSSage Weil 2023a8599bd8SSage Weil 2024a8599bd8SSage Weil /* 2025a8599bd8SSage Weil * Take references to capabilities we hold, so that we don't release 2026a8599bd8SSage Weil * them to the MDS prematurely. 2027a8599bd8SSage Weil * 2028be655596SSage Weil * Protected by i_ceph_lock. 2029a8599bd8SSage Weil */ 2030a8599bd8SSage Weil static void __take_cap_refs(struct ceph_inode_info *ci, int got) 2031a8599bd8SSage Weil { 2032a8599bd8SSage Weil if (got & CEPH_CAP_PIN) 2033a8599bd8SSage Weil ci->i_pin_ref++; 2034a8599bd8SSage Weil if (got & CEPH_CAP_FILE_RD) 2035a8599bd8SSage Weil ci->i_rd_ref++; 2036a8599bd8SSage Weil if (got & CEPH_CAP_FILE_CACHE) 2037a8599bd8SSage Weil ci->i_rdcache_ref++; 2038a8599bd8SSage Weil if (got & CEPH_CAP_FILE_WR) 2039a8599bd8SSage Weil ci->i_wr_ref++; 2040a8599bd8SSage Weil if (got & CEPH_CAP_FILE_BUFFER) { 2041d3d0720dSHenry C Chang if (ci->i_wb_ref == 0) 20423772d26dSSage Weil ihold(&ci->vfs_inode); 2043d3d0720dSHenry C Chang ci->i_wb_ref++; 2044d3d0720dSHenry C Chang dout("__take_cap_refs %p wb %d -> %d (?)\n", 2045d3d0720dSHenry C Chang &ci->vfs_inode, ci->i_wb_ref-1, ci->i_wb_ref); 2046a8599bd8SSage Weil } 2047a8599bd8SSage Weil } 2048a8599bd8SSage Weil 2049a8599bd8SSage Weil /* 2050a8599bd8SSage Weil * Try to grab cap references. Specify those refs we @want, and the 2051a8599bd8SSage Weil * minimal set we @need. Also include the larger offset we are writing 2052a8599bd8SSage Weil * to (when applicable), and check against max_size here as well. 2053a8599bd8SSage Weil * Note that caller is responsible for ensuring max_size increases are 2054a8599bd8SSage Weil * requested from the MDS. 2055a8599bd8SSage Weil */ 2056a8599bd8SSage Weil static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want, 2057a8599bd8SSage Weil int *got, loff_t endoff, int *check_max, int *err) 2058a8599bd8SSage Weil { 2059a8599bd8SSage Weil struct inode *inode = &ci->vfs_inode; 2060a8599bd8SSage Weil int ret = 0; 2061a8599bd8SSage Weil int have, implemented; 2062195d3ce2SSage Weil int file_wanted; 2063a8599bd8SSage Weil 2064a8599bd8SSage Weil dout("get_cap_refs %p need %s want %s\n", inode, 2065a8599bd8SSage Weil ceph_cap_string(need), ceph_cap_string(want)); 2066be655596SSage Weil spin_lock(&ci->i_ceph_lock); 2067a8599bd8SSage Weil 2068195d3ce2SSage Weil /* make sure file is actually open */ 2069195d3ce2SSage Weil file_wanted = __ceph_caps_file_wanted(ci); 2070195d3ce2SSage Weil if ((file_wanted & need) == 0) { 2071195d3ce2SSage Weil dout("try_get_cap_refs need %s file_wanted %s, EBADF\n", 2072195d3ce2SSage Weil ceph_cap_string(need), ceph_cap_string(file_wanted)); 2073a8599bd8SSage Weil *err = -EBADF; 2074a8599bd8SSage Weil ret = 1; 2075a8599bd8SSage Weil goto out; 2076a8599bd8SSage Weil } 2077a8599bd8SSage Weil 207837505d57SYan, Zheng /* finish pending truncate */ 207937505d57SYan, Zheng while (ci->i_truncate_pending) { 208037505d57SYan, Zheng spin_unlock(&ci->i_ceph_lock); 2081b415bf4fSYan, Zheng __ceph_do_pending_vmtruncate(inode); 208237505d57SYan, Zheng spin_lock(&ci->i_ceph_lock); 208337505d57SYan, Zheng } 208437505d57SYan, Zheng 20853871cbb9SYan, Zheng have = __ceph_caps_issued(ci, &implemented); 20863871cbb9SYan, Zheng 20873871cbb9SYan, Zheng if (have & need & CEPH_CAP_FILE_WR) { 2088a8599bd8SSage Weil if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) { 2089a8599bd8SSage Weil dout("get_cap_refs %p endoff %llu > maxsize %llu\n", 2090a8599bd8SSage Weil inode, endoff, ci->i_max_size); 20913871cbb9SYan, Zheng if (endoff > ci->i_requested_max_size) { 2092a8599bd8SSage Weil *check_max = 1; 2093a8599bd8SSage Weil ret = 1; 2094a8599bd8SSage Weil } 2095a8599bd8SSage Weil goto out; 2096a8599bd8SSage Weil } 2097a8599bd8SSage Weil /* 2098a8599bd8SSage Weil * If a sync write is in progress, we must wait, so that we 2099a8599bd8SSage Weil * can get a final snapshot value for size+mtime. 2100a8599bd8SSage Weil */ 2101a8599bd8SSage Weil if (__ceph_have_pending_cap_snap(ci)) { 2102a8599bd8SSage Weil dout("get_cap_refs %p cap_snap_pending\n", inode); 2103a8599bd8SSage Weil goto out; 2104a8599bd8SSage Weil } 2105a8599bd8SSage Weil } 2106a8599bd8SSage Weil 2107a8599bd8SSage Weil if ((have & need) == need) { 2108a8599bd8SSage Weil /* 2109a8599bd8SSage Weil * Look at (implemented & ~have & not) so that we keep waiting 2110a8599bd8SSage Weil * on transition from wanted -> needed caps. This is needed 2111a8599bd8SSage Weil * for WRBUFFER|WR -> WR to avoid a new WR sync write from 2112a8599bd8SSage Weil * going before a prior buffered writeback happens. 2113a8599bd8SSage Weil */ 2114a8599bd8SSage Weil int not = want & ~(have & need); 2115a8599bd8SSage Weil int revoking = implemented & ~have; 2116a8599bd8SSage Weil dout("get_cap_refs %p have %s but not %s (revoking %s)\n", 2117a8599bd8SSage Weil inode, ceph_cap_string(have), ceph_cap_string(not), 2118a8599bd8SSage Weil ceph_cap_string(revoking)); 2119a8599bd8SSage Weil if ((revoking & not) == 0) { 2120a8599bd8SSage Weil *got = need | (have & want); 2121a8599bd8SSage Weil __take_cap_refs(ci, *got); 2122a8599bd8SSage Weil ret = 1; 2123a8599bd8SSage Weil } 2124a8599bd8SSage Weil } else { 2125a8599bd8SSage Weil dout("get_cap_refs %p have %s needed %s\n", inode, 2126a8599bd8SSage Weil ceph_cap_string(have), ceph_cap_string(need)); 2127a8599bd8SSage Weil } 2128a8599bd8SSage Weil out: 2129be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 2130a8599bd8SSage Weil dout("get_cap_refs %p ret %d got %s\n", inode, 2131a8599bd8SSage Weil ret, ceph_cap_string(*got)); 2132a8599bd8SSage Weil return ret; 2133a8599bd8SSage Weil } 2134a8599bd8SSage Weil 2135a8599bd8SSage Weil /* 2136a8599bd8SSage Weil * Check the offset we are writing up to against our current 2137a8599bd8SSage Weil * max_size. If necessary, tell the MDS we want to write to 2138a8599bd8SSage Weil * a larger offset. 2139a8599bd8SSage Weil */ 2140a8599bd8SSage Weil static void check_max_size(struct inode *inode, loff_t endoff) 2141a8599bd8SSage Weil { 2142a8599bd8SSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 2143a8599bd8SSage Weil int check = 0; 2144a8599bd8SSage Weil 2145a8599bd8SSage Weil /* do we need to explicitly request a larger max_size? */ 2146be655596SSage Weil spin_lock(&ci->i_ceph_lock); 21473871cbb9SYan, Zheng if (endoff >= ci->i_max_size && endoff > ci->i_wanted_max_size) { 2148a8599bd8SSage Weil dout("write %p at large endoff %llu, req max_size\n", 2149a8599bd8SSage Weil inode, endoff); 2150a8599bd8SSage Weil ci->i_wanted_max_size = endoff; 2151a8599bd8SSage Weil } 21523871cbb9SYan, Zheng /* duplicate ceph_check_caps()'s logic */ 21533871cbb9SYan, Zheng if (ci->i_auth_cap && 21543871cbb9SYan, Zheng (ci->i_auth_cap->issued & CEPH_CAP_FILE_WR) && 21553871cbb9SYan, Zheng ci->i_wanted_max_size > ci->i_max_size && 21563871cbb9SYan, Zheng ci->i_wanted_max_size > ci->i_requested_max_size) 21573871cbb9SYan, Zheng check = 1; 2158be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 2159a8599bd8SSage Weil if (check) 2160a8599bd8SSage Weil ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL); 2161a8599bd8SSage Weil } 2162a8599bd8SSage Weil 2163a8599bd8SSage Weil /* 2164a8599bd8SSage Weil * Wait for caps, and take cap references. If we can't get a WR cap 2165a8599bd8SSage Weil * due to a small max_size, make sure we check_max_size (and possibly 2166a8599bd8SSage Weil * ask the mds) so we don't get hung up indefinitely. 2167a8599bd8SSage Weil */ 2168a8599bd8SSage Weil int ceph_get_caps(struct ceph_inode_info *ci, int need, int want, int *got, 2169a8599bd8SSage Weil loff_t endoff) 2170a8599bd8SSage Weil { 2171a8599bd8SSage Weil int check_max, ret, err; 2172a8599bd8SSage Weil 2173a8599bd8SSage Weil retry: 2174a8599bd8SSage Weil if (endoff > 0) 2175a8599bd8SSage Weil check_max_size(&ci->vfs_inode, endoff); 2176a8599bd8SSage Weil check_max = 0; 2177a8599bd8SSage Weil err = 0; 2178a8599bd8SSage Weil ret = wait_event_interruptible(ci->i_cap_wq, 2179a8599bd8SSage Weil try_get_cap_refs(ci, need, want, 2180a8599bd8SSage Weil got, endoff, 2181a8599bd8SSage Weil &check_max, &err)); 2182a8599bd8SSage Weil if (err) 2183a8599bd8SSage Weil ret = err; 2184a8599bd8SSage Weil if (check_max) 2185a8599bd8SSage Weil goto retry; 2186a8599bd8SSage Weil return ret; 2187a8599bd8SSage Weil } 2188a8599bd8SSage Weil 2189a8599bd8SSage Weil /* 2190a8599bd8SSage Weil * Take cap refs. Caller must already know we hold at least one ref 2191a8599bd8SSage Weil * on the caps in question or we don't know this is safe. 2192a8599bd8SSage Weil */ 2193a8599bd8SSage Weil void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps) 2194a8599bd8SSage Weil { 2195be655596SSage Weil spin_lock(&ci->i_ceph_lock); 2196a8599bd8SSage Weil __take_cap_refs(ci, caps); 2197be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 2198a8599bd8SSage Weil } 2199a8599bd8SSage Weil 2200a8599bd8SSage Weil /* 2201a8599bd8SSage Weil * Release cap refs. 2202a8599bd8SSage Weil * 2203a8599bd8SSage Weil * If we released the last ref on any given cap, call ceph_check_caps 2204a8599bd8SSage Weil * to release (or schedule a release). 2205a8599bd8SSage Weil * 2206a8599bd8SSage Weil * If we are releasing a WR cap (from a sync write), finalize any affected 2207a8599bd8SSage Weil * cap_snap, and wake up any waiters. 2208a8599bd8SSage Weil */ 2209a8599bd8SSage Weil void ceph_put_cap_refs(struct ceph_inode_info *ci, int had) 2210a8599bd8SSage Weil { 2211a8599bd8SSage Weil struct inode *inode = &ci->vfs_inode; 2212a8599bd8SSage Weil int last = 0, put = 0, flushsnaps = 0, wake = 0; 2213a8599bd8SSage Weil struct ceph_cap_snap *capsnap; 2214a8599bd8SSage Weil 2215be655596SSage Weil spin_lock(&ci->i_ceph_lock); 2216a8599bd8SSage Weil if (had & CEPH_CAP_PIN) 2217a8599bd8SSage Weil --ci->i_pin_ref; 2218a8599bd8SSage Weil if (had & CEPH_CAP_FILE_RD) 2219a8599bd8SSage Weil if (--ci->i_rd_ref == 0) 2220a8599bd8SSage Weil last++; 2221a8599bd8SSage Weil if (had & CEPH_CAP_FILE_CACHE) 2222a8599bd8SSage Weil if (--ci->i_rdcache_ref == 0) 2223a8599bd8SSage Weil last++; 2224a8599bd8SSage Weil if (had & CEPH_CAP_FILE_BUFFER) { 2225d3d0720dSHenry C Chang if (--ci->i_wb_ref == 0) { 2226a8599bd8SSage Weil last++; 2227a8599bd8SSage Weil put++; 2228a8599bd8SSage Weil } 2229d3d0720dSHenry C Chang dout("put_cap_refs %p wb %d -> %d (?)\n", 2230d3d0720dSHenry C Chang inode, ci->i_wb_ref+1, ci->i_wb_ref); 2231a8599bd8SSage Weil } 2232a8599bd8SSage Weil if (had & CEPH_CAP_FILE_WR) 2233a8599bd8SSage Weil if (--ci->i_wr_ref == 0) { 2234a8599bd8SSage Weil last++; 2235a8599bd8SSage Weil if (!list_empty(&ci->i_cap_snaps)) { 2236a8599bd8SSage Weil capsnap = list_first_entry(&ci->i_cap_snaps, 2237a8599bd8SSage Weil struct ceph_cap_snap, 2238a8599bd8SSage Weil ci_item); 2239a8599bd8SSage Weil if (capsnap->writing) { 2240a8599bd8SSage Weil capsnap->writing = 0; 2241a8599bd8SSage Weil flushsnaps = 2242a8599bd8SSage Weil __ceph_finish_cap_snap(ci, 2243a8599bd8SSage Weil capsnap); 2244a8599bd8SSage Weil wake = 1; 2245a8599bd8SSage Weil } 2246a8599bd8SSage Weil } 2247a8599bd8SSage Weil } 2248be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 2249a8599bd8SSage Weil 2250819ccbfaSSage Weil dout("put_cap_refs %p had %s%s%s\n", inode, ceph_cap_string(had), 2251819ccbfaSSage Weil last ? " last" : "", put ? " put" : ""); 2252a8599bd8SSage Weil 2253a8599bd8SSage Weil if (last && !flushsnaps) 2254a8599bd8SSage Weil ceph_check_caps(ci, 0, NULL); 2255a8599bd8SSage Weil else if (flushsnaps) 2256a8599bd8SSage Weil ceph_flush_snaps(ci); 2257a8599bd8SSage Weil if (wake) 225803066f23SYehuda Sadeh wake_up_all(&ci->i_cap_wq); 2259a8599bd8SSage Weil if (put) 2260a8599bd8SSage Weil iput(inode); 2261a8599bd8SSage Weil } 2262a8599bd8SSage Weil 2263a8599bd8SSage Weil /* 2264a8599bd8SSage Weil * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap 2265a8599bd8SSage Weil * context. Adjust per-snap dirty page accounting as appropriate. 2266a8599bd8SSage Weil * Once all dirty data for a cap_snap is flushed, flush snapped file 2267a8599bd8SSage Weil * metadata back to the MDS. If we dropped the last ref, call 2268a8599bd8SSage Weil * ceph_check_caps. 2269a8599bd8SSage Weil */ 2270a8599bd8SSage Weil void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr, 2271a8599bd8SSage Weil struct ceph_snap_context *snapc) 2272a8599bd8SSage Weil { 2273a8599bd8SSage Weil struct inode *inode = &ci->vfs_inode; 2274a8599bd8SSage Weil int last = 0; 2275819ccbfaSSage Weil int complete_capsnap = 0; 2276819ccbfaSSage Weil int drop_capsnap = 0; 2277a8599bd8SSage Weil int found = 0; 2278a8599bd8SSage Weil struct ceph_cap_snap *capsnap = NULL; 2279a8599bd8SSage Weil 2280be655596SSage Weil spin_lock(&ci->i_ceph_lock); 2281a8599bd8SSage Weil ci->i_wrbuffer_ref -= nr; 2282a8599bd8SSage Weil last = !ci->i_wrbuffer_ref; 2283a8599bd8SSage Weil 2284a8599bd8SSage Weil if (ci->i_head_snapc == snapc) { 2285a8599bd8SSage Weil ci->i_wrbuffer_ref_head -= nr; 22867d8cb26dSSage Weil if (ci->i_wrbuffer_ref_head == 0 && 22877d8cb26dSSage Weil ci->i_dirty_caps == 0 && ci->i_flushing_caps == 0) { 22887d8cb26dSSage Weil BUG_ON(!ci->i_head_snapc); 2289a8599bd8SSage Weil ceph_put_snap_context(ci->i_head_snapc); 2290a8599bd8SSage Weil ci->i_head_snapc = NULL; 2291a8599bd8SSage Weil } 2292a8599bd8SSage Weil dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n", 2293a8599bd8SSage Weil inode, 2294a8599bd8SSage Weil ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr, 2295a8599bd8SSage Weil ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head, 2296a8599bd8SSage Weil last ? " LAST" : ""); 2297a8599bd8SSage Weil } else { 2298a8599bd8SSage Weil list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { 2299a8599bd8SSage Weil if (capsnap->context == snapc) { 2300a8599bd8SSage Weil found = 1; 2301a8599bd8SSage Weil break; 2302a8599bd8SSage Weil } 2303a8599bd8SSage Weil } 2304a8599bd8SSage Weil BUG_ON(!found); 2305819ccbfaSSage Weil capsnap->dirty_pages -= nr; 2306819ccbfaSSage Weil if (capsnap->dirty_pages == 0) { 2307819ccbfaSSage Weil complete_capsnap = 1; 2308819ccbfaSSage Weil if (capsnap->dirty == 0) 2309819ccbfaSSage Weil /* cap writeback completed before we created 2310819ccbfaSSage Weil * the cap_snap; no FLUSHSNAP is needed */ 2311819ccbfaSSage Weil drop_capsnap = 1; 2312819ccbfaSSage Weil } 2313a8599bd8SSage Weil dout("put_wrbuffer_cap_refs on %p cap_snap %p " 2314819ccbfaSSage Weil " snap %lld %d/%d -> %d/%d %s%s%s\n", 2315a8599bd8SSage Weil inode, capsnap, capsnap->context->seq, 2316a8599bd8SSage Weil ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr, 2317a8599bd8SSage Weil ci->i_wrbuffer_ref, capsnap->dirty_pages, 2318a8599bd8SSage Weil last ? " (wrbuffer last)" : "", 2319819ccbfaSSage Weil complete_capsnap ? " (complete capsnap)" : "", 2320819ccbfaSSage Weil drop_capsnap ? " (drop capsnap)" : ""); 2321819ccbfaSSage Weil if (drop_capsnap) { 2322819ccbfaSSage Weil ceph_put_snap_context(capsnap->context); 2323819ccbfaSSage Weil list_del(&capsnap->ci_item); 2324819ccbfaSSage Weil list_del(&capsnap->flushing_item); 2325819ccbfaSSage Weil ceph_put_cap_snap(capsnap); 2326819ccbfaSSage Weil } 2327a8599bd8SSage Weil } 2328a8599bd8SSage Weil 2329be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 2330a8599bd8SSage Weil 2331a8599bd8SSage Weil if (last) { 2332a8599bd8SSage Weil ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL); 2333a8599bd8SSage Weil iput(inode); 2334819ccbfaSSage Weil } else if (complete_capsnap) { 2335a8599bd8SSage Weil ceph_flush_snaps(ci); 233603066f23SYehuda Sadeh wake_up_all(&ci->i_cap_wq); 2337a8599bd8SSage Weil } 2338819ccbfaSSage Weil if (drop_capsnap) 2339819ccbfaSSage Weil iput(inode); 2340a8599bd8SSage Weil } 2341a8599bd8SSage Weil 2342a8599bd8SSage Weil /* 2343ca20c991SYan, Zheng * Invalidate unlinked inode's aliases, so we can drop the inode ASAP. 2344ca20c991SYan, Zheng */ 2345ca20c991SYan, Zheng static void invalidate_aliases(struct inode *inode) 2346ca20c991SYan, Zheng { 2347ca20c991SYan, Zheng struct dentry *dn, *prev = NULL; 2348ca20c991SYan, Zheng 2349ca20c991SYan, Zheng dout("invalidate_aliases inode %p\n", inode); 2350ca20c991SYan, Zheng d_prune_aliases(inode); 2351ca20c991SYan, Zheng /* 2352ca20c991SYan, Zheng * For non-directory inode, d_find_alias() only returns 2353fc12c80aSJ. Bruce Fields * hashed dentry. After calling d_invalidate(), the 2354fc12c80aSJ. Bruce Fields * dentry becomes unhashed. 2355ca20c991SYan, Zheng * 2356a8d436f0SYan, Zheng * For directory inode, d_find_alias() can return 2357fc12c80aSJ. Bruce Fields * unhashed dentry. But directory inode should have 2358ca20c991SYan, Zheng * one alias at most. 2359ca20c991SYan, Zheng */ 2360ca20c991SYan, Zheng while ((dn = d_find_alias(inode))) { 2361ca20c991SYan, Zheng if (dn == prev) { 2362ca20c991SYan, Zheng dput(dn); 2363ca20c991SYan, Zheng break; 2364ca20c991SYan, Zheng } 2365a8d436f0SYan, Zheng d_invalidate(dn); 2366ca20c991SYan, Zheng if (prev) 2367ca20c991SYan, Zheng dput(prev); 2368ca20c991SYan, Zheng prev = dn; 2369ca20c991SYan, Zheng } 2370ca20c991SYan, Zheng if (prev) 2371ca20c991SYan, Zheng dput(prev); 2372ca20c991SYan, Zheng } 2373ca20c991SYan, Zheng 2374ca20c991SYan, Zheng /* 2375a8599bd8SSage Weil * Handle a cap GRANT message from the MDS. (Note that a GRANT may 2376a8599bd8SSage Weil * actually be a revocation if it specifies a smaller cap set.) 2377a8599bd8SSage Weil * 2378be655596SSage Weil * caller holds s_mutex and i_ceph_lock, we drop both. 237915637c8bSSage Weil * 2380a8599bd8SSage Weil * return value: 2381a8599bd8SSage Weil * 0 - ok 2382a8599bd8SSage Weil * 1 - check_caps on auth cap only (writeback) 2383a8599bd8SSage Weil * 2 - check_caps (ack revoke) 2384a8599bd8SSage Weil */ 238515637c8bSSage Weil static void handle_cap_grant(struct inode *inode, struct ceph_mds_caps *grant, 2386a8599bd8SSage Weil struct ceph_mds_session *session, 2387a8599bd8SSage Weil struct ceph_cap *cap, 2388a8599bd8SSage Weil struct ceph_buffer *xattr_buf) 2389be655596SSage Weil __releases(ci->i_ceph_lock) 2390a8599bd8SSage Weil { 2391a8599bd8SSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 2392a8599bd8SSage Weil int mds = session->s_mds; 23932f56f56aSSage Weil int seq = le32_to_cpu(grant->seq); 2394a8599bd8SSage Weil int newcaps = le32_to_cpu(grant->caps); 2395a8599bd8SSage Weil int issued, implemented, used, wanted, dirty; 2396a8599bd8SSage Weil u64 size = le64_to_cpu(grant->size); 2397a8599bd8SSage Weil u64 max_size = le64_to_cpu(grant->max_size); 2398a8599bd8SSage Weil struct timespec mtime, atime, ctime; 239915637c8bSSage Weil int check_caps = 0; 2400a8599bd8SSage Weil int wake = 0; 2401a8599bd8SSage Weil int writeback = 0; 24023c6f6b79SSage Weil int queue_invalidate = 0; 2403ca20c991SYan, Zheng int deleted_inode = 0; 240499ccbd22SMilosz Tanski int queue_revalidate = 0; 2405a8599bd8SSage Weil 24062f56f56aSSage Weil dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n", 24072f56f56aSSage Weil inode, cap, mds, seq, ceph_cap_string(newcaps)); 2408a8599bd8SSage Weil dout(" size %llu max_size %llu, i_size %llu\n", size, max_size, 2409a8599bd8SSage Weil inode->i_size); 2410a8599bd8SSage Weil 2411a8599bd8SSage Weil /* 2412a8599bd8SSage Weil * If CACHE is being revoked, and we have no dirty buffers, 2413a8599bd8SSage Weil * try to invalidate (once). (If there are dirty buffers, we 2414a8599bd8SSage Weil * will invalidate _after_ writeback.) 2415a8599bd8SSage Weil */ 24163b454c49SSage Weil if (((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) && 24173b454c49SSage Weil (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 && 2418bcd2cbd1SYehuda Sadeh !ci->i_wrbuffer_ref) { 2419e9075743SLi Wang if (try_nonblocking_invalidate(inode)) { 2420a8599bd8SSage Weil /* there were locked pages.. invalidate later 2421a8599bd8SSage Weil in a separate thread. */ 2422a8599bd8SSage Weil if (ci->i_rdcache_revoking != ci->i_rdcache_gen) { 24233c6f6b79SSage Weil queue_invalidate = 1; 2424a8599bd8SSage Weil ci->i_rdcache_revoking = ci->i_rdcache_gen; 2425a8599bd8SSage Weil } 2426a8599bd8SSage Weil } 242799ccbd22SMilosz Tanski 242899ccbd22SMilosz Tanski ceph_fscache_invalidate(inode); 2429a8599bd8SSage Weil } 2430a8599bd8SSage Weil 2431a8599bd8SSage Weil /* side effects now are allowed */ 2432a8599bd8SSage Weil 2433a8599bd8SSage Weil issued = __ceph_caps_issued(ci, &implemented); 2434a8599bd8SSage Weil issued |= implemented | __ceph_caps_dirty(ci); 2435a8599bd8SSage Weil 2436685f9a5dSSage Weil cap->cap_gen = session->s_cap_gen; 2437a8599bd8SSage Weil 2438a8599bd8SSage Weil __check_cap_issue(ci, cap, newcaps); 2439a8599bd8SSage Weil 2440a8599bd8SSage Weil if ((issued & CEPH_CAP_AUTH_EXCL) == 0) { 2441a8599bd8SSage Weil inode->i_mode = le32_to_cpu(grant->mode); 244205cb11c1SEric W. Biederman inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(grant->uid)); 244305cb11c1SEric W. Biederman inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(grant->gid)); 2444a8599bd8SSage Weil dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode, 2445bd2bae6aSEric W. Biederman from_kuid(&init_user_ns, inode->i_uid), 2446bd2bae6aSEric W. Biederman from_kgid(&init_user_ns, inode->i_gid)); 2447a8599bd8SSage Weil } 2448a8599bd8SSage Weil 2449ca20c991SYan, Zheng if ((issued & CEPH_CAP_LINK_EXCL) == 0) { 2450bfe86848SMiklos Szeredi set_nlink(inode, le32_to_cpu(grant->nlink)); 2451ca20c991SYan, Zheng if (inode->i_nlink == 0 && 2452ca20c991SYan, Zheng (newcaps & (CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL))) 2453ca20c991SYan, Zheng deleted_inode = 1; 2454ca20c991SYan, Zheng } 2455a8599bd8SSage Weil 2456a8599bd8SSage Weil if ((issued & CEPH_CAP_XATTR_EXCL) == 0 && grant->xattr_len) { 2457a8599bd8SSage Weil int len = le32_to_cpu(grant->xattr_len); 2458a8599bd8SSage Weil u64 version = le64_to_cpu(grant->xattr_version); 2459a8599bd8SSage Weil 2460a8599bd8SSage Weil if (version > ci->i_xattrs.version) { 2461a8599bd8SSage Weil dout(" got new xattrs v%llu on %p len %d\n", 2462a8599bd8SSage Weil version, inode, len); 2463a8599bd8SSage Weil if (ci->i_xattrs.blob) 2464a8599bd8SSage Weil ceph_buffer_put(ci->i_xattrs.blob); 2465a8599bd8SSage Weil ci->i_xattrs.blob = ceph_buffer_get(xattr_buf); 2466a8599bd8SSage Weil ci->i_xattrs.version = version; 24677221fe4cSGuangliang Zhao ceph_forget_all_cached_acls(inode); 2468a8599bd8SSage Weil } 2469a8599bd8SSage Weil } 2470a8599bd8SSage Weil 247199ccbd22SMilosz Tanski /* Do we need to revalidate our fscache cookie. Don't bother on the 247299ccbd22SMilosz Tanski * first cache cap as we already validate at cookie creation time. */ 247399ccbd22SMilosz Tanski if ((issued & CEPH_CAP_FILE_CACHE) && ci->i_rdcache_gen > 1) 247499ccbd22SMilosz Tanski queue_revalidate = 1; 247599ccbd22SMilosz Tanski 2476a8599bd8SSage Weil /* size/ctime/mtime/atime? */ 2477a8599bd8SSage Weil ceph_fill_file_size(inode, issued, 2478a8599bd8SSage Weil le32_to_cpu(grant->truncate_seq), 2479a8599bd8SSage Weil le64_to_cpu(grant->truncate_size), size); 2480a8599bd8SSage Weil ceph_decode_timespec(&mtime, &grant->mtime); 2481a8599bd8SSage Weil ceph_decode_timespec(&atime, &grant->atime); 2482a8599bd8SSage Weil ceph_decode_timespec(&ctime, &grant->ctime); 2483a8599bd8SSage Weil ceph_fill_file_time(inode, issued, 2484a8599bd8SSage Weil le32_to_cpu(grant->time_warp_seq), &ctime, &mtime, 2485a8599bd8SSage Weil &atime); 2486a8599bd8SSage Weil 2487a8599bd8SSage Weil /* max size increase? */ 24885e62ad30SYan, Zheng if (ci->i_auth_cap == cap && max_size != ci->i_max_size) { 2489a8599bd8SSage Weil dout("max_size %lld -> %llu\n", ci->i_max_size, max_size); 2490a8599bd8SSage Weil ci->i_max_size = max_size; 2491a8599bd8SSage Weil if (max_size >= ci->i_wanted_max_size) { 2492a8599bd8SSage Weil ci->i_wanted_max_size = 0; /* reset */ 2493a8599bd8SSage Weil ci->i_requested_max_size = 0; 2494a8599bd8SSage Weil } 2495a8599bd8SSage Weil wake = 1; 2496a8599bd8SSage Weil } 2497a8599bd8SSage Weil 2498a8599bd8SSage Weil /* check cap bits */ 2499a8599bd8SSage Weil wanted = __ceph_caps_wanted(ci); 2500a8599bd8SSage Weil used = __ceph_caps_used(ci); 2501a8599bd8SSage Weil dirty = __ceph_caps_dirty(ci); 2502a8599bd8SSage Weil dout(" my wanted = %s, used = %s, dirty %s\n", 2503a8599bd8SSage Weil ceph_cap_string(wanted), 2504a8599bd8SSage Weil ceph_cap_string(used), 2505a8599bd8SSage Weil ceph_cap_string(dirty)); 2506a8599bd8SSage Weil if (wanted != le32_to_cpu(grant->wanted)) { 2507a8599bd8SSage Weil dout("mds wanted %s -> %s\n", 2508a8599bd8SSage Weil ceph_cap_string(le32_to_cpu(grant->wanted)), 2509a8599bd8SSage Weil ceph_cap_string(wanted)); 2510390306c3SYan, Zheng /* imported cap may not have correct mds_wanted */ 2511390306c3SYan, Zheng if (le32_to_cpu(grant->op) == CEPH_CAP_OP_IMPORT) 2512390306c3SYan, Zheng check_caps = 1; 2513a8599bd8SSage Weil } 2514a8599bd8SSage Weil 2515a8599bd8SSage Weil cap->seq = seq; 2516a8599bd8SSage Weil 2517a8599bd8SSage Weil /* file layout may have changed */ 2518a8599bd8SSage Weil ci->i_layout = grant->layout; 2519a8599bd8SSage Weil 2520a8599bd8SSage Weil /* revocation, grant, or no-op? */ 2521a8599bd8SSage Weil if (cap->issued & ~newcaps) { 25223b454c49SSage Weil int revoking = cap->issued & ~newcaps; 25233b454c49SSage Weil 25243b454c49SSage Weil dout("revocation: %s -> %s (revoking %s)\n", 25253b454c49SSage Weil ceph_cap_string(cap->issued), 25263b454c49SSage Weil ceph_cap_string(newcaps), 25273b454c49SSage Weil ceph_cap_string(revoking)); 25280eb6cd49SSage Weil if (revoking & used & CEPH_CAP_FILE_BUFFER) 25293b454c49SSage Weil writeback = 1; /* initiate writeback; will delay ack */ 25303b454c49SSage Weil else if (revoking == CEPH_CAP_FILE_CACHE && 25313b454c49SSage Weil (newcaps & CEPH_CAP_FILE_LAZYIO) == 0 && 25323b454c49SSage Weil queue_invalidate) 25333b454c49SSage Weil ; /* do nothing yet, invalidation will be queued */ 25343b454c49SSage Weil else if (cap == ci->i_auth_cap) 25353b454c49SSage Weil check_caps = 1; /* check auth cap only */ 25363b454c49SSage Weil else 25373b454c49SSage Weil check_caps = 2; /* check all caps */ 2538a8599bd8SSage Weil cap->issued = newcaps; 2539978097c9SSage Weil cap->implemented |= newcaps; 2540a8599bd8SSage Weil } else if (cap->issued == newcaps) { 2541a8599bd8SSage Weil dout("caps unchanged: %s -> %s\n", 2542a8599bd8SSage Weil ceph_cap_string(cap->issued), ceph_cap_string(newcaps)); 2543a8599bd8SSage Weil } else { 2544a8599bd8SSage Weil dout("grant: %s -> %s\n", ceph_cap_string(cap->issued), 2545a8599bd8SSage Weil ceph_cap_string(newcaps)); 25466ee6b953SYan, Zheng /* non-auth MDS is revoking the newly grant caps ? */ 25476ee6b953SYan, Zheng if (cap == ci->i_auth_cap && 25486ee6b953SYan, Zheng __ceph_caps_revoking_other(ci, cap, newcaps)) 25496ee6b953SYan, Zheng check_caps = 2; 25506ee6b953SYan, Zheng 2551a8599bd8SSage Weil cap->issued = newcaps; 2552a8599bd8SSage Weil cap->implemented |= newcaps; /* add bits only, to 2553a8599bd8SSage Weil * avoid stepping on a 2554a8599bd8SSage Weil * pending revocation */ 2555a8599bd8SSage Weil wake = 1; 2556a8599bd8SSage Weil } 2557978097c9SSage Weil BUG_ON(cap->issued & ~cap->implemented); 2558a8599bd8SSage Weil 2559be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 256099ccbd22SMilosz Tanski 25613c6f6b79SSage Weil if (writeback) 2562a8599bd8SSage Weil /* 2563a8599bd8SSage Weil * queue inode for writeback: we can't actually call 2564a8599bd8SSage Weil * filemap_write_and_wait, etc. from message handler 2565a8599bd8SSage Weil * context. 2566a8599bd8SSage Weil */ 25673c6f6b79SSage Weil ceph_queue_writeback(inode); 25683c6f6b79SSage Weil if (queue_invalidate) 25693c6f6b79SSage Weil ceph_queue_invalidate(inode); 2570ca20c991SYan, Zheng if (deleted_inode) 2571ca20c991SYan, Zheng invalidate_aliases(inode); 257299ccbd22SMilosz Tanski if (queue_revalidate) 257399ccbd22SMilosz Tanski ceph_queue_revalidate(inode); 2574a8599bd8SSage Weil if (wake) 257503066f23SYehuda Sadeh wake_up_all(&ci->i_cap_wq); 257615637c8bSSage Weil 257715637c8bSSage Weil if (check_caps == 1) 257815637c8bSSage Weil ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY, 257915637c8bSSage Weil session); 258015637c8bSSage Weil else if (check_caps == 2) 258115637c8bSSage Weil ceph_check_caps(ci, CHECK_CAPS_NODELAY, session); 258215637c8bSSage Weil else 258315637c8bSSage Weil mutex_unlock(&session->s_mutex); 2584a8599bd8SSage Weil } 2585a8599bd8SSage Weil 2586a8599bd8SSage Weil /* 2587a8599bd8SSage Weil * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the 2588a8599bd8SSage Weil * MDS has been safely committed. 2589a8599bd8SSage Weil */ 25906df058c0SSage Weil static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid, 2591a8599bd8SSage Weil struct ceph_mds_caps *m, 2592a8599bd8SSage Weil struct ceph_mds_session *session, 2593a8599bd8SSage Weil struct ceph_cap *cap) 2594be655596SSage Weil __releases(ci->i_ceph_lock) 2595a8599bd8SSage Weil { 2596a8599bd8SSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 25973d14c5d2SYehuda Sadeh struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc; 2598a8599bd8SSage Weil unsigned seq = le32_to_cpu(m->seq); 2599a8599bd8SSage Weil int dirty = le32_to_cpu(m->dirty); 2600a8599bd8SSage Weil int cleaned = 0; 2601afcdaea3SSage Weil int drop = 0; 2602a8599bd8SSage Weil int i; 2603a8599bd8SSage Weil 2604a8599bd8SSage Weil for (i = 0; i < CEPH_CAP_BITS; i++) 2605a8599bd8SSage Weil if ((dirty & (1 << i)) && 2606a8599bd8SSage Weil flush_tid == ci->i_cap_flush_tid[i]) 2607a8599bd8SSage Weil cleaned |= 1 << i; 2608a8599bd8SSage Weil 2609a8599bd8SSage Weil dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s," 2610a8599bd8SSage Weil " flushing %s -> %s\n", 2611a8599bd8SSage Weil inode, session->s_mds, seq, ceph_cap_string(dirty), 2612a8599bd8SSage Weil ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps), 2613a8599bd8SSage Weil ceph_cap_string(ci->i_flushing_caps & ~cleaned)); 2614a8599bd8SSage Weil 2615a8599bd8SSage Weil if (ci->i_flushing_caps == (ci->i_flushing_caps & ~cleaned)) 2616a8599bd8SSage Weil goto out; 2617a8599bd8SSage Weil 2618a8599bd8SSage Weil ci->i_flushing_caps &= ~cleaned; 2619a8599bd8SSage Weil 2620a8599bd8SSage Weil spin_lock(&mdsc->cap_dirty_lock); 2621a8599bd8SSage Weil if (ci->i_flushing_caps == 0) { 2622a8599bd8SSage Weil list_del_init(&ci->i_flushing_item); 2623a8599bd8SSage Weil if (!list_empty(&session->s_cap_flushing)) 2624a8599bd8SSage Weil dout(" mds%d still flushing cap on %p\n", 2625a8599bd8SSage Weil session->s_mds, 2626a8599bd8SSage Weil &list_entry(session->s_cap_flushing.next, 2627a8599bd8SSage Weil struct ceph_inode_info, 2628a8599bd8SSage Weil i_flushing_item)->vfs_inode); 2629a8599bd8SSage Weil mdsc->num_cap_flushing--; 263003066f23SYehuda Sadeh wake_up_all(&mdsc->cap_flushing_wq); 2631a8599bd8SSage Weil dout(" inode %p now !flushing\n", inode); 2632afcdaea3SSage Weil 2633afcdaea3SSage Weil if (ci->i_dirty_caps == 0) { 2634a8599bd8SSage Weil dout(" inode %p now clean\n", inode); 2635afcdaea3SSage Weil BUG_ON(!list_empty(&ci->i_dirty_item)); 2636afcdaea3SSage Weil drop = 1; 26377d8cb26dSSage Weil if (ci->i_wrbuffer_ref_head == 0) { 26387d8cb26dSSage Weil BUG_ON(!ci->i_head_snapc); 26397d8cb26dSSage Weil ceph_put_snap_context(ci->i_head_snapc); 26407d8cb26dSSage Weil ci->i_head_snapc = NULL; 26417d8cb26dSSage Weil } 264276e3b390SSage Weil } else { 264376e3b390SSage Weil BUG_ON(list_empty(&ci->i_dirty_item)); 2644afcdaea3SSage Weil } 2645a8599bd8SSage Weil } 2646a8599bd8SSage Weil spin_unlock(&mdsc->cap_dirty_lock); 264703066f23SYehuda Sadeh wake_up_all(&ci->i_cap_wq); 2648a8599bd8SSage Weil 2649a8599bd8SSage Weil out: 2650be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 2651afcdaea3SSage Weil if (drop) 2652a8599bd8SSage Weil iput(inode); 2653a8599bd8SSage Weil } 2654a8599bd8SSage Weil 2655a8599bd8SSage Weil /* 2656a8599bd8SSage Weil * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can 2657a8599bd8SSage Weil * throw away our cap_snap. 2658a8599bd8SSage Weil * 2659a8599bd8SSage Weil * Caller hold s_mutex. 2660a8599bd8SSage Weil */ 26616df058c0SSage Weil static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid, 2662a8599bd8SSage Weil struct ceph_mds_caps *m, 2663a8599bd8SSage Weil struct ceph_mds_session *session) 2664a8599bd8SSage Weil { 2665a8599bd8SSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 2666a8599bd8SSage Weil u64 follows = le64_to_cpu(m->snap_follows); 2667a8599bd8SSage Weil struct ceph_cap_snap *capsnap; 2668a8599bd8SSage Weil int drop = 0; 2669a8599bd8SSage Weil 2670a8599bd8SSage Weil dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n", 2671a8599bd8SSage Weil inode, ci, session->s_mds, follows); 2672a8599bd8SSage Weil 2673be655596SSage Weil spin_lock(&ci->i_ceph_lock); 2674a8599bd8SSage Weil list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { 2675a8599bd8SSage Weil if (capsnap->follows == follows) { 2676a8599bd8SSage Weil if (capsnap->flush_tid != flush_tid) { 2677a8599bd8SSage Weil dout(" cap_snap %p follows %lld tid %lld !=" 2678a8599bd8SSage Weil " %lld\n", capsnap, follows, 2679a8599bd8SSage Weil flush_tid, capsnap->flush_tid); 2680a8599bd8SSage Weil break; 2681a8599bd8SSage Weil } 2682a8599bd8SSage Weil WARN_ON(capsnap->dirty_pages || capsnap->writing); 2683819ccbfaSSage Weil dout(" removing %p cap_snap %p follows %lld\n", 2684819ccbfaSSage Weil inode, capsnap, follows); 2685a8599bd8SSage Weil ceph_put_snap_context(capsnap->context); 2686a8599bd8SSage Weil list_del(&capsnap->ci_item); 2687a8599bd8SSage Weil list_del(&capsnap->flushing_item); 2688a8599bd8SSage Weil ceph_put_cap_snap(capsnap); 2689a8599bd8SSage Weil drop = 1; 2690a8599bd8SSage Weil break; 2691a8599bd8SSage Weil } else { 2692a8599bd8SSage Weil dout(" skipping cap_snap %p follows %lld\n", 2693a8599bd8SSage Weil capsnap, capsnap->follows); 2694a8599bd8SSage Weil } 2695a8599bd8SSage Weil } 2696be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 2697a8599bd8SSage Weil if (drop) 2698a8599bd8SSage Weil iput(inode); 2699a8599bd8SSage Weil } 2700a8599bd8SSage Weil 2701a8599bd8SSage Weil /* 2702a8599bd8SSage Weil * Handle TRUNC from MDS, indicating file truncation. 2703a8599bd8SSage Weil * 2704a8599bd8SSage Weil * caller hold s_mutex. 2705a8599bd8SSage Weil */ 2706a8599bd8SSage Weil static void handle_cap_trunc(struct inode *inode, 2707a8599bd8SSage Weil struct ceph_mds_caps *trunc, 2708a8599bd8SSage Weil struct ceph_mds_session *session) 2709be655596SSage Weil __releases(ci->i_ceph_lock) 2710a8599bd8SSage Weil { 2711a8599bd8SSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 2712a8599bd8SSage Weil int mds = session->s_mds; 2713a8599bd8SSage Weil int seq = le32_to_cpu(trunc->seq); 2714a8599bd8SSage Weil u32 truncate_seq = le32_to_cpu(trunc->truncate_seq); 2715a8599bd8SSage Weil u64 truncate_size = le64_to_cpu(trunc->truncate_size); 2716a8599bd8SSage Weil u64 size = le64_to_cpu(trunc->size); 2717a8599bd8SSage Weil int implemented = 0; 2718a8599bd8SSage Weil int dirty = __ceph_caps_dirty(ci); 2719a8599bd8SSage Weil int issued = __ceph_caps_issued(ceph_inode(inode), &implemented); 2720a8599bd8SSage Weil int queue_trunc = 0; 2721a8599bd8SSage Weil 2722a8599bd8SSage Weil issued |= implemented | dirty; 2723a8599bd8SSage Weil 2724a8599bd8SSage Weil dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n", 2725a8599bd8SSage Weil inode, mds, seq, truncate_size, truncate_seq); 2726a8599bd8SSage Weil queue_trunc = ceph_fill_file_size(inode, issued, 2727a8599bd8SSage Weil truncate_seq, truncate_size, size); 2728be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 2729a8599bd8SSage Weil 273099ccbd22SMilosz Tanski if (queue_trunc) { 27313c6f6b79SSage Weil ceph_queue_vmtruncate(inode); 273299ccbd22SMilosz Tanski ceph_fscache_invalidate(inode); 273399ccbd22SMilosz Tanski } 2734a8599bd8SSage Weil } 2735a8599bd8SSage Weil 2736a8599bd8SSage Weil /* 2737a8599bd8SSage Weil * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a 2738a8599bd8SSage Weil * different one. If we are the most recent migration we've seen (as 2739a8599bd8SSage Weil * indicated by mseq), make note of the migrating cap bits for the 2740a8599bd8SSage Weil * duration (until we see the corresponding IMPORT). 2741a8599bd8SSage Weil * 2742a8599bd8SSage Weil * caller holds s_mutex 2743a8599bd8SSage Weil */ 2744a8599bd8SSage Weil static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex, 2745154f42c2SSage Weil struct ceph_mds_session *session, 2746154f42c2SSage Weil int *open_target_sessions) 2747a8599bd8SSage Weil { 2748db354052SSage Weil struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc; 2749a8599bd8SSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 2750a8599bd8SSage Weil int mds = session->s_mds; 2751a8599bd8SSage Weil unsigned mseq = le32_to_cpu(ex->migrate_seq); 2752a8599bd8SSage Weil struct ceph_cap *cap = NULL, *t; 2753a8599bd8SSage Weil struct rb_node *p; 2754a8599bd8SSage Weil int remember = 1; 2755a8599bd8SSage Weil 2756a8599bd8SSage Weil dout("handle_cap_export inode %p ci %p mds%d mseq %d\n", 2757a8599bd8SSage Weil inode, ci, mds, mseq); 2758a8599bd8SSage Weil 2759be655596SSage Weil spin_lock(&ci->i_ceph_lock); 2760a8599bd8SSage Weil 2761a8599bd8SSage Weil /* make sure we haven't seen a higher mseq */ 2762a8599bd8SSage Weil for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { 2763a8599bd8SSage Weil t = rb_entry(p, struct ceph_cap, ci_node); 2764a8599bd8SSage Weil if (ceph_seq_cmp(t->mseq, mseq) > 0) { 2765a8599bd8SSage Weil dout(" higher mseq on cap from mds%d\n", 2766a8599bd8SSage Weil t->session->s_mds); 2767a8599bd8SSage Weil remember = 0; 2768a8599bd8SSage Weil } 2769a8599bd8SSage Weil if (t->session->s_mds == mds) 2770a8599bd8SSage Weil cap = t; 2771a8599bd8SSage Weil } 2772a8599bd8SSage Weil 2773a8599bd8SSage Weil if (cap) { 2774a8599bd8SSage Weil if (remember) { 2775a8599bd8SSage Weil /* make note */ 2776a8599bd8SSage Weil ci->i_cap_exporting_mds = mds; 2777a8599bd8SSage Weil ci->i_cap_exporting_mseq = mseq; 2778a8599bd8SSage Weil ci->i_cap_exporting_issued = cap->issued; 2779154f42c2SSage Weil 2780154f42c2SSage Weil /* 2781154f42c2SSage Weil * make sure we have open sessions with all possible 2782154f42c2SSage Weil * export targets, so that we get the matching IMPORT 2783154f42c2SSage Weil */ 2784154f42c2SSage Weil *open_target_sessions = 1; 2785db354052SSage Weil 2786db354052SSage Weil /* 2787db354052SSage Weil * we can't flush dirty caps that we've seen the 2788db354052SSage Weil * EXPORT but no IMPORT for 2789db354052SSage Weil */ 2790db354052SSage Weil spin_lock(&mdsc->cap_dirty_lock); 2791db354052SSage Weil if (!list_empty(&ci->i_dirty_item)) { 2792db354052SSage Weil dout(" moving %p to cap_dirty_migrating\n", 2793db354052SSage Weil inode); 2794db354052SSage Weil list_move(&ci->i_dirty_item, 2795db354052SSage Weil &mdsc->cap_dirty_migrating); 2796db354052SSage Weil } 2797db354052SSage Weil spin_unlock(&mdsc->cap_dirty_lock); 2798a8599bd8SSage Weil } 2799a096b09aSYan, Zheng __ceph_remove_cap(cap, false); 2800a8599bd8SSage Weil } 28014ea0043aSSage Weil /* else, we already released it */ 2802a8599bd8SSage Weil 2803be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 2804a8599bd8SSage Weil } 2805a8599bd8SSage Weil 2806a8599bd8SSage Weil /* 2807a8599bd8SSage Weil * Handle cap IMPORT. If there are temp bits from an older EXPORT, 2808a8599bd8SSage Weil * clean them up. 2809a8599bd8SSage Weil * 2810a8599bd8SSage Weil * caller holds s_mutex. 2811a8599bd8SSage Weil */ 2812a8599bd8SSage Weil static void handle_cap_import(struct ceph_mds_client *mdsc, 2813a8599bd8SSage Weil struct inode *inode, struct ceph_mds_caps *im, 2814a8599bd8SSage Weil struct ceph_mds_session *session, 2815a8599bd8SSage Weil void *snaptrace, int snaptrace_len) 2816a8599bd8SSage Weil { 2817a8599bd8SSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 2818a8599bd8SSage Weil int mds = session->s_mds; 2819a8599bd8SSage Weil unsigned issued = le32_to_cpu(im->caps); 2820a8599bd8SSage Weil unsigned wanted = le32_to_cpu(im->wanted); 2821a8599bd8SSage Weil unsigned seq = le32_to_cpu(im->seq); 2822a8599bd8SSage Weil unsigned mseq = le32_to_cpu(im->migrate_seq); 2823a8599bd8SSage Weil u64 realmino = le64_to_cpu(im->realm); 2824a8599bd8SSage Weil u64 cap_id = le64_to_cpu(im->cap_id); 2825a8599bd8SSage Weil 2826a8599bd8SSage Weil if (ci->i_cap_exporting_mds >= 0 && 2827a8599bd8SSage Weil ceph_seq_cmp(ci->i_cap_exporting_mseq, mseq) < 0) { 2828a8599bd8SSage Weil dout("handle_cap_import inode %p ci %p mds%d mseq %d" 2829a8599bd8SSage Weil " - cleared exporting from mds%d\n", 2830a8599bd8SSage Weil inode, ci, mds, mseq, 2831a8599bd8SSage Weil ci->i_cap_exporting_mds); 2832a8599bd8SSage Weil ci->i_cap_exporting_issued = 0; 2833a8599bd8SSage Weil ci->i_cap_exporting_mseq = 0; 2834a8599bd8SSage Weil ci->i_cap_exporting_mds = -1; 2835db354052SSage Weil 2836db354052SSage Weil spin_lock(&mdsc->cap_dirty_lock); 2837db354052SSage Weil if (!list_empty(&ci->i_dirty_item)) { 2838db354052SSage Weil dout(" moving %p back to cap_dirty\n", inode); 2839db354052SSage Weil list_move(&ci->i_dirty_item, &mdsc->cap_dirty); 2840db354052SSage Weil } 2841db354052SSage Weil spin_unlock(&mdsc->cap_dirty_lock); 2842a8599bd8SSage Weil } else { 2843a8599bd8SSage Weil dout("handle_cap_import inode %p ci %p mds%d mseq %d\n", 2844a8599bd8SSage Weil inode, ci, mds, mseq); 2845a8599bd8SSage Weil } 2846a8599bd8SSage Weil 2847a8599bd8SSage Weil down_write(&mdsc->snap_rwsem); 2848a8599bd8SSage Weil ceph_update_snap_trace(mdsc, snaptrace, snaptrace+snaptrace_len, 2849a8599bd8SSage Weil false); 2850a8599bd8SSage Weil downgrade_write(&mdsc->snap_rwsem); 2851a8599bd8SSage Weil ceph_add_cap(inode, session, cap_id, -1, 2852a8599bd8SSage Weil issued, wanted, seq, mseq, realmino, CEPH_CAP_FLAG_AUTH, 2853a8599bd8SSage Weil NULL /* no caps context */); 2854088b3f5eSSage Weil kick_flushing_inode_caps(mdsc, session, inode); 2855a8599bd8SSage Weil up_read(&mdsc->snap_rwsem); 2856feb4cc9bSSage Weil 2857feb4cc9bSSage Weil /* make sure we re-request max_size, if necessary */ 2858be655596SSage Weil spin_lock(&ci->i_ceph_lock); 28590e5e1774SYan, Zheng ci->i_wanted_max_size = 0; /* reset */ 2860feb4cc9bSSage Weil ci->i_requested_max_size = 0; 2861be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 2862a8599bd8SSage Weil } 2863a8599bd8SSage Weil 2864a8599bd8SSage Weil /* 2865a8599bd8SSage Weil * Handle a caps message from the MDS. 2866a8599bd8SSage Weil * 2867a8599bd8SSage Weil * Identify the appropriate session, inode, and call the right handler 2868a8599bd8SSage Weil * based on the cap op. 2869a8599bd8SSage Weil */ 2870a8599bd8SSage Weil void ceph_handle_caps(struct ceph_mds_session *session, 2871a8599bd8SSage Weil struct ceph_msg *msg) 2872a8599bd8SSage Weil { 2873a8599bd8SSage Weil struct ceph_mds_client *mdsc = session->s_mdsc; 28743d14c5d2SYehuda Sadeh struct super_block *sb = mdsc->fsc->sb; 2875a8599bd8SSage Weil struct inode *inode; 2876be655596SSage Weil struct ceph_inode_info *ci; 2877a8599bd8SSage Weil struct ceph_cap *cap; 2878a8599bd8SSage Weil struct ceph_mds_caps *h; 28792600d2ddSSage Weil int mds = session->s_mds; 2880a8599bd8SSage Weil int op; 28813d7ded4dSSage Weil u32 seq, mseq; 2882a8599bd8SSage Weil struct ceph_vino vino; 2883a8599bd8SSage Weil u64 cap_id; 2884a8599bd8SSage Weil u64 size, max_size; 28856df058c0SSage Weil u64 tid; 288670edb55bSSage Weil void *snaptrace; 2887ce1fbc8dSSage Weil size_t snaptrace_len; 2888ce1fbc8dSSage Weil void *flock; 2889ce1fbc8dSSage Weil u32 flock_len; 2890154f42c2SSage Weil int open_target_sessions = 0; 2891a8599bd8SSage Weil 2892a8599bd8SSage Weil dout("handle_caps from mds%d\n", mds); 2893a8599bd8SSage Weil 2894a8599bd8SSage Weil /* decode */ 28956df058c0SSage Weil tid = le64_to_cpu(msg->hdr.tid); 2896a8599bd8SSage Weil if (msg->front.iov_len < sizeof(*h)) 2897a8599bd8SSage Weil goto bad; 2898a8599bd8SSage Weil h = msg->front.iov_base; 2899a8599bd8SSage Weil op = le32_to_cpu(h->op); 2900a8599bd8SSage Weil vino.ino = le64_to_cpu(h->ino); 2901a8599bd8SSage Weil vino.snap = CEPH_NOSNAP; 2902a8599bd8SSage Weil cap_id = le64_to_cpu(h->cap_id); 2903a8599bd8SSage Weil seq = le32_to_cpu(h->seq); 29043d7ded4dSSage Weil mseq = le32_to_cpu(h->migrate_seq); 2905a8599bd8SSage Weil size = le64_to_cpu(h->size); 2906a8599bd8SSage Weil max_size = le64_to_cpu(h->max_size); 2907a8599bd8SSage Weil 2908ce1fbc8dSSage Weil snaptrace = h + 1; 2909ce1fbc8dSSage Weil snaptrace_len = le32_to_cpu(h->snap_trace_len); 2910ce1fbc8dSSage Weil 2911ce1fbc8dSSage Weil if (le16_to_cpu(msg->hdr.version) >= 2) { 2912ce1fbc8dSSage Weil void *p, *end; 2913ce1fbc8dSSage Weil 2914ce1fbc8dSSage Weil p = snaptrace + snaptrace_len; 2915ce1fbc8dSSage Weil end = msg->front.iov_base + msg->front.iov_len; 2916ce1fbc8dSSage Weil ceph_decode_32_safe(&p, end, flock_len, bad); 2917ce1fbc8dSSage Weil flock = p; 2918ce1fbc8dSSage Weil } else { 2919ce1fbc8dSSage Weil flock = NULL; 2920ce1fbc8dSSage Weil flock_len = 0; 2921ce1fbc8dSSage Weil } 2922ce1fbc8dSSage Weil 2923a8599bd8SSage Weil mutex_lock(&session->s_mutex); 2924a8599bd8SSage Weil session->s_seq++; 2925a8599bd8SSage Weil dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq, 2926a8599bd8SSage Weil (unsigned)seq); 2927a8599bd8SSage Weil 292866f58691SYan, Zheng if (op == CEPH_CAP_OP_IMPORT) 292966f58691SYan, Zheng ceph_add_cap_releases(mdsc, session); 293066f58691SYan, Zheng 2931a8599bd8SSage Weil /* lookup ino */ 2932a8599bd8SSage Weil inode = ceph_find_inode(sb, vino); 2933be655596SSage Weil ci = ceph_inode(inode); 2934a8599bd8SSage Weil dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino, 2935a8599bd8SSage Weil vino.snap, inode); 2936a8599bd8SSage Weil if (!inode) { 2937a8599bd8SSage Weil dout(" i don't have ino %llx\n", vino.ino); 29383d7ded4dSSage Weil 2939a096b09aSYan, Zheng if (op == CEPH_CAP_OP_IMPORT) { 2940a096b09aSYan, Zheng spin_lock(&session->s_cap_lock); 29413d7ded4dSSage Weil __queue_cap_release(session, vino.ino, cap_id, 29423d7ded4dSSage Weil mseq, seq); 2943a096b09aSYan, Zheng spin_unlock(&session->s_cap_lock); 2944a096b09aSYan, Zheng } 294521b559deSGreg Farnum goto flush_cap_releases; 2946a8599bd8SSage Weil } 2947a8599bd8SSage Weil 2948a8599bd8SSage Weil /* these will work even if we don't have a cap yet */ 2949a8599bd8SSage Weil switch (op) { 2950a8599bd8SSage Weil case CEPH_CAP_OP_FLUSHSNAP_ACK: 29516df058c0SSage Weil handle_cap_flushsnap_ack(inode, tid, h, session); 2952a8599bd8SSage Weil goto done; 2953a8599bd8SSage Weil 2954a8599bd8SSage Weil case CEPH_CAP_OP_EXPORT: 2955154f42c2SSage Weil handle_cap_export(inode, h, session, &open_target_sessions); 2956a8599bd8SSage Weil goto done; 2957a8599bd8SSage Weil 2958a8599bd8SSage Weil case CEPH_CAP_OP_IMPORT: 2959a8599bd8SSage Weil handle_cap_import(mdsc, inode, h, session, 2960ce1fbc8dSSage Weil snaptrace, snaptrace_len); 2961a8599bd8SSage Weil } 2962a8599bd8SSage Weil 2963a8599bd8SSage Weil /* the rest require a cap */ 2964be655596SSage Weil spin_lock(&ci->i_ceph_lock); 2965a8599bd8SSage Weil cap = __get_cap_for_mds(ceph_inode(inode), mds); 2966a8599bd8SSage Weil if (!cap) { 29679dbd412fSSage Weil dout(" no cap on %p ino %llx.%llx from mds%d\n", 2968a8599bd8SSage Weil inode, ceph_ino(inode), ceph_snap(inode), mds); 2969be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 297021b559deSGreg Farnum goto flush_cap_releases; 2971a8599bd8SSage Weil } 2972a8599bd8SSage Weil 2973be655596SSage Weil /* note that each of these drops i_ceph_lock for us */ 2974a8599bd8SSage Weil switch (op) { 2975a8599bd8SSage Weil case CEPH_CAP_OP_REVOKE: 2976a8599bd8SSage Weil case CEPH_CAP_OP_GRANT: 29770e5e1774SYan, Zheng case CEPH_CAP_OP_IMPORT: 297815637c8bSSage Weil handle_cap_grant(inode, h, session, cap, msg->middle); 297915637c8bSSage Weil goto done_unlocked; 2980a8599bd8SSage Weil 2981a8599bd8SSage Weil case CEPH_CAP_OP_FLUSH_ACK: 29826df058c0SSage Weil handle_cap_flush_ack(inode, tid, h, session, cap); 2983a8599bd8SSage Weil break; 2984a8599bd8SSage Weil 2985a8599bd8SSage Weil case CEPH_CAP_OP_TRUNC: 2986a8599bd8SSage Weil handle_cap_trunc(inode, h, session); 2987a8599bd8SSage Weil break; 2988a8599bd8SSage Weil 2989a8599bd8SSage Weil default: 2990be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 2991a8599bd8SSage Weil pr_err("ceph_handle_caps: unknown cap op %d %s\n", op, 2992a8599bd8SSage Weil ceph_cap_op_name(op)); 2993a8599bd8SSage Weil } 2994a8599bd8SSage Weil 299521b559deSGreg Farnum goto done; 299621b559deSGreg Farnum 299721b559deSGreg Farnum flush_cap_releases: 299821b559deSGreg Farnum /* 299921b559deSGreg Farnum * send any full release message to try to move things 300021b559deSGreg Farnum * along for the mds (who clearly thinks we still have this 300121b559deSGreg Farnum * cap). 300221b559deSGreg Farnum */ 300321b559deSGreg Farnum ceph_add_cap_releases(mdsc, session); 300421b559deSGreg Farnum ceph_send_cap_releases(mdsc, session); 300521b559deSGreg Farnum 3006a8599bd8SSage Weil done: 3007a8599bd8SSage Weil mutex_unlock(&session->s_mutex); 300815637c8bSSage Weil done_unlocked: 3009a8599bd8SSage Weil if (inode) 3010a8599bd8SSage Weil iput(inode); 3011154f42c2SSage Weil if (open_target_sessions) 3012154f42c2SSage Weil ceph_mdsc_open_export_target_sessions(mdsc, session); 3013a8599bd8SSage Weil return; 3014a8599bd8SSage Weil 3015a8599bd8SSage Weil bad: 3016a8599bd8SSage Weil pr_err("ceph_handle_caps: corrupt message\n"); 30179ec7cab1SSage Weil ceph_msg_dump(msg); 3018a8599bd8SSage Weil return; 3019a8599bd8SSage Weil } 3020a8599bd8SSage Weil 3021a8599bd8SSage Weil /* 3022a8599bd8SSage Weil * Delayed work handler to process end of delayed cap release LRU list. 3023a8599bd8SSage Weil */ 3024afcdaea3SSage Weil void ceph_check_delayed_caps(struct ceph_mds_client *mdsc) 3025a8599bd8SSage Weil { 3026a8599bd8SSage Weil struct ceph_inode_info *ci; 3027a8599bd8SSage Weil int flags = CHECK_CAPS_NODELAY; 3028a8599bd8SSage Weil 3029a8599bd8SSage Weil dout("check_delayed_caps\n"); 3030a8599bd8SSage Weil while (1) { 3031a8599bd8SSage Weil spin_lock(&mdsc->cap_delay_lock); 3032a8599bd8SSage Weil if (list_empty(&mdsc->cap_delay_list)) 3033a8599bd8SSage Weil break; 3034a8599bd8SSage Weil ci = list_first_entry(&mdsc->cap_delay_list, 3035a8599bd8SSage Weil struct ceph_inode_info, 3036a8599bd8SSage Weil i_cap_delay_list); 3037a8599bd8SSage Weil if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 && 3038a8599bd8SSage Weil time_before(jiffies, ci->i_hold_caps_max)) 3039a8599bd8SSage Weil break; 3040a8599bd8SSage Weil list_del_init(&ci->i_cap_delay_list); 3041a8599bd8SSage Weil spin_unlock(&mdsc->cap_delay_lock); 3042a8599bd8SSage Weil dout("check_delayed_caps on %p\n", &ci->vfs_inode); 3043a8599bd8SSage Weil ceph_check_caps(ci, flags, NULL); 3044a8599bd8SSage Weil } 3045a8599bd8SSage Weil spin_unlock(&mdsc->cap_delay_lock); 3046a8599bd8SSage Weil } 3047a8599bd8SSage Weil 3048a8599bd8SSage Weil /* 3049afcdaea3SSage Weil * Flush all dirty caps to the mds 3050afcdaea3SSage Weil */ 3051afcdaea3SSage Weil void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc) 3052afcdaea3SSage Weil { 3053db354052SSage Weil struct ceph_inode_info *ci; 3054db354052SSage Weil struct inode *inode; 3055afcdaea3SSage Weil 3056afcdaea3SSage Weil dout("flush_dirty_caps\n"); 3057afcdaea3SSage Weil spin_lock(&mdsc->cap_dirty_lock); 3058db354052SSage Weil while (!list_empty(&mdsc->cap_dirty)) { 3059db354052SSage Weil ci = list_first_entry(&mdsc->cap_dirty, struct ceph_inode_info, 3060afcdaea3SSage Weil i_dirty_item); 306170b666c3SSage Weil inode = &ci->vfs_inode; 306270b666c3SSage Weil ihold(inode); 3063db354052SSage Weil dout("flush_dirty_caps %p\n", inode); 3064afcdaea3SSage Weil spin_unlock(&mdsc->cap_dirty_lock); 306570b666c3SSage Weil ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH, NULL); 3066afcdaea3SSage Weil iput(inode); 3067afcdaea3SSage Weil spin_lock(&mdsc->cap_dirty_lock); 3068afcdaea3SSage Weil } 3069afcdaea3SSage Weil spin_unlock(&mdsc->cap_dirty_lock); 3070db354052SSage Weil dout("flush_dirty_caps done\n"); 3071afcdaea3SSage Weil } 3072afcdaea3SSage Weil 3073afcdaea3SSage Weil /* 3074a8599bd8SSage Weil * Drop open file reference. If we were the last open file, 3075a8599bd8SSage Weil * we may need to release capabilities to the MDS (or schedule 3076a8599bd8SSage Weil * their delayed release). 3077a8599bd8SSage Weil */ 3078a8599bd8SSage Weil void ceph_put_fmode(struct ceph_inode_info *ci, int fmode) 3079a8599bd8SSage Weil { 3080a8599bd8SSage Weil struct inode *inode = &ci->vfs_inode; 3081a8599bd8SSage Weil int last = 0; 3082a8599bd8SSage Weil 3083be655596SSage Weil spin_lock(&ci->i_ceph_lock); 3084a8599bd8SSage Weil dout("put_fmode %p fmode %d %d -> %d\n", inode, fmode, 3085a8599bd8SSage Weil ci->i_nr_by_mode[fmode], ci->i_nr_by_mode[fmode]-1); 3086a8599bd8SSage Weil BUG_ON(ci->i_nr_by_mode[fmode] == 0); 3087a8599bd8SSage Weil if (--ci->i_nr_by_mode[fmode] == 0) 3088a8599bd8SSage Weil last++; 3089be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 3090a8599bd8SSage Weil 3091a8599bd8SSage Weil if (last && ci->i_vino.snap == CEPH_NOSNAP) 3092a8599bd8SSage Weil ceph_check_caps(ci, 0, NULL); 3093a8599bd8SSage Weil } 3094a8599bd8SSage Weil 3095a8599bd8SSage Weil /* 3096a8599bd8SSage Weil * Helpers for embedding cap and dentry lease releases into mds 3097a8599bd8SSage Weil * requests. 3098a8599bd8SSage Weil * 3099a8599bd8SSage Weil * @force is used by dentry_release (below) to force inclusion of a 3100a8599bd8SSage Weil * record for the directory inode, even when there aren't any caps to 3101a8599bd8SSage Weil * drop. 3102a8599bd8SSage Weil */ 3103a8599bd8SSage Weil int ceph_encode_inode_release(void **p, struct inode *inode, 3104a8599bd8SSage Weil int mds, int drop, int unless, int force) 3105a8599bd8SSage Weil { 3106a8599bd8SSage Weil struct ceph_inode_info *ci = ceph_inode(inode); 3107a8599bd8SSage Weil struct ceph_cap *cap; 3108a8599bd8SSage Weil struct ceph_mds_request_release *rel = *p; 3109ec97f88bSSage Weil int used, dirty; 3110a8599bd8SSage Weil int ret = 0; 3111a8599bd8SSage Weil 3112be655596SSage Weil spin_lock(&ci->i_ceph_lock); 3113916623daSSage Weil used = __ceph_caps_used(ci); 3114ec97f88bSSage Weil dirty = __ceph_caps_dirty(ci); 3115916623daSSage Weil 3116ec97f88bSSage Weil dout("encode_inode_release %p mds%d used|dirty %s drop %s unless %s\n", 3117ec97f88bSSage Weil inode, mds, ceph_cap_string(used|dirty), ceph_cap_string(drop), 3118916623daSSage Weil ceph_cap_string(unless)); 3119916623daSSage Weil 3120ec97f88bSSage Weil /* only drop unused, clean caps */ 3121ec97f88bSSage Weil drop &= ~(used | dirty); 3122916623daSSage Weil 3123a8599bd8SSage Weil cap = __get_cap_for_mds(ci, mds); 3124a8599bd8SSage Weil if (cap && __cap_is_valid(cap)) { 3125a8599bd8SSage Weil if (force || 3126a8599bd8SSage Weil ((cap->issued & drop) && 3127a8599bd8SSage Weil (cap->issued & unless) == 0)) { 3128a8599bd8SSage Weil if ((cap->issued & drop) && 3129a8599bd8SSage Weil (cap->issued & unless) == 0) { 3130bb137f84SYan, Zheng int wanted = __ceph_caps_wanted(ci); 3131bb137f84SYan, Zheng if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0) 3132bb137f84SYan, Zheng wanted |= cap->mds_wanted; 3133bb137f84SYan, Zheng dout("encode_inode_release %p cap %p " 3134bb137f84SYan, Zheng "%s -> %s, wanted %s -> %s\n", inode, cap, 3135a8599bd8SSage Weil ceph_cap_string(cap->issued), 3136bb137f84SYan, Zheng ceph_cap_string(cap->issued & ~drop), 3137bb137f84SYan, Zheng ceph_cap_string(cap->mds_wanted), 3138bb137f84SYan, Zheng ceph_cap_string(wanted)); 3139bb137f84SYan, Zheng 3140a8599bd8SSage Weil cap->issued &= ~drop; 3141a8599bd8SSage Weil cap->implemented &= ~drop; 3142bb137f84SYan, Zheng cap->mds_wanted = wanted; 3143a8599bd8SSage Weil } else { 3144a8599bd8SSage Weil dout("encode_inode_release %p cap %p %s" 3145a8599bd8SSage Weil " (force)\n", inode, cap, 3146a8599bd8SSage Weil ceph_cap_string(cap->issued)); 3147a8599bd8SSage Weil } 3148a8599bd8SSage Weil 3149a8599bd8SSage Weil rel->ino = cpu_to_le64(ceph_ino(inode)); 3150a8599bd8SSage Weil rel->cap_id = cpu_to_le64(cap->cap_id); 3151a8599bd8SSage Weil rel->seq = cpu_to_le32(cap->seq); 3152a8599bd8SSage Weil rel->issue_seq = cpu_to_le32(cap->issue_seq), 3153a8599bd8SSage Weil rel->mseq = cpu_to_le32(cap->mseq); 3154a8599bd8SSage Weil rel->caps = cpu_to_le32(cap->issued); 3155a8599bd8SSage Weil rel->wanted = cpu_to_le32(cap->mds_wanted); 3156a8599bd8SSage Weil rel->dname_len = 0; 3157a8599bd8SSage Weil rel->dname_seq = 0; 3158a8599bd8SSage Weil *p += sizeof(*rel); 3159a8599bd8SSage Weil ret = 1; 3160a8599bd8SSage Weil } else { 3161a8599bd8SSage Weil dout("encode_inode_release %p cap %p %s\n", 3162a8599bd8SSage Weil inode, cap, ceph_cap_string(cap->issued)); 3163a8599bd8SSage Weil } 3164a8599bd8SSage Weil } 3165be655596SSage Weil spin_unlock(&ci->i_ceph_lock); 3166a8599bd8SSage Weil return ret; 3167a8599bd8SSage Weil } 3168a8599bd8SSage Weil 3169a8599bd8SSage Weil int ceph_encode_dentry_release(void **p, struct dentry *dentry, 3170a8599bd8SSage Weil int mds, int drop, int unless) 3171a8599bd8SSage Weil { 3172a8599bd8SSage Weil struct inode *dir = dentry->d_parent->d_inode; 3173a8599bd8SSage Weil struct ceph_mds_request_release *rel = *p; 3174a8599bd8SSage Weil struct ceph_dentry_info *di = ceph_dentry(dentry); 3175a8599bd8SSage Weil int force = 0; 3176a8599bd8SSage Weil int ret; 3177a8599bd8SSage Weil 3178a8599bd8SSage Weil /* 3179a8599bd8SSage Weil * force an record for the directory caps if we have a dentry lease. 3180be655596SSage Weil * this is racy (can't take i_ceph_lock and d_lock together), but it 3181a8599bd8SSage Weil * doesn't have to be perfect; the mds will revoke anything we don't 3182a8599bd8SSage Weil * release. 3183a8599bd8SSage Weil */ 3184a8599bd8SSage Weil spin_lock(&dentry->d_lock); 3185a8599bd8SSage Weil if (di->lease_session && di->lease_session->s_mds == mds) 3186a8599bd8SSage Weil force = 1; 3187a8599bd8SSage Weil spin_unlock(&dentry->d_lock); 3188a8599bd8SSage Weil 3189a8599bd8SSage Weil ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force); 3190a8599bd8SSage Weil 3191a8599bd8SSage Weil spin_lock(&dentry->d_lock); 3192a8599bd8SSage Weil if (ret && di->lease_session && di->lease_session->s_mds == mds) { 3193a8599bd8SSage Weil dout("encode_dentry_release %p mds%d seq %d\n", 3194a8599bd8SSage Weil dentry, mds, (int)di->lease_seq); 3195a8599bd8SSage Weil rel->dname_len = cpu_to_le32(dentry->d_name.len); 3196a8599bd8SSage Weil memcpy(*p, dentry->d_name.name, dentry->d_name.len); 3197a8599bd8SSage Weil *p += dentry->d_name.len; 3198a8599bd8SSage Weil rel->dname_seq = cpu_to_le32(di->lease_seq); 31991dadcce3SSage Weil __ceph_mdsc_drop_dentry_lease(dentry); 3200a8599bd8SSage Weil } 3201a8599bd8SSage Weil spin_unlock(&dentry->d_lock); 3202a8599bd8SSage Weil return ret; 3203a8599bd8SSage Weil } 3204