Skip to content

Commit 9a8d03c

Browse files
jtlaytonidryomov
authored andcommitted
ceph: attempt to do async create when possible
With the Octopus release, the MDS will hand out directory create caps. If we have Fxc caps on the directory, and complete directory information or a known negative dentry, then we can return without waiting on the reply, allowing the open() call to return very quickly to userland. We use the normal ceph_fill_inode() routine to fill in the inode, so we have to gin up some reply inode information with what we'd expect the newly-created inode to have. The client assumes that it has a full set of caps on the new inode, and that the MDS will revoke them when there is conflicting access. This functionality is gated on the wsync/nowsync mount options. Signed-off-by: Jeff Layton <jlayton@kernel.org> Reviewed-by: "Yan, Zheng" <zyan@redhat.com> Signed-off-by: Ilya Dryomov <idryomov@gmail.com>
1 parent 785892f commit 9a8d03c

2 files changed

Lines changed: 243 additions & 7 deletions

File tree

‎fs/ceph/file.c‎

Lines changed: 240 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,216 @@ cache_file_layout(struct inode *dst, struct inode *src)
448448
spin_unlock(&cdst->i_ceph_lock);
449449
}
450450

451+
/*
452+
* Try to set up an async create. We need caps, a file layout, and inode number,
453+
* and either a lease on the dentry or complete dir info. If any of those
454+
* criteria are not satisfied, then return false and the caller can go
455+
* synchronous.
456+
*/
457+
static int try_prep_async_create(struct inode *dir, struct dentry *dentry,
458+
struct ceph_file_layout *lo, u64 *pino)
459+
{
460+
struct ceph_inode_info *ci = ceph_inode(dir);
461+
struct ceph_dentry_info *di = ceph_dentry(dentry);
462+
int got = 0, want = CEPH_CAP_FILE_EXCL | CEPH_CAP_DIR_CREATE;
463+
u64 ino;
464+
465+
spin_lock(&ci->i_ceph_lock);
466+
/* No auth cap means no chance for Dc caps */
467+
if (!ci->i_auth_cap)
468+
goto no_async;
469+
470+
/* Any delegated inos? */
471+
if (xa_empty(&ci->i_auth_cap->session->s_delegated_inos))
472+
goto no_async;
473+
474+
if (!ceph_file_layout_is_valid(&ci->i_cached_layout))
475+
goto no_async;
476+
477+
if ((__ceph_caps_issued(ci, NULL) & want) != want)
478+
goto no_async;
479+
480+
if (d_in_lookup(dentry)) {
481+
if (!__ceph_dir_is_complete(ci))
482+
goto no_async;
483+
} else if (atomic_read(&ci->i_shared_gen) !=
484+
READ_ONCE(di->lease_shared_gen)) {
485+
goto no_async;
486+
}
487+
488+
ino = ceph_get_deleg_ino(ci->i_auth_cap->session);
489+
if (!ino)
490+
goto no_async;
491+
492+
*pino = ino;
493+
ceph_take_cap_refs(ci, want, false);
494+
memcpy(lo, &ci->i_cached_layout, sizeof(*lo));
495+
rcu_assign_pointer(lo->pool_ns,
496+
ceph_try_get_string(ci->i_cached_layout.pool_ns));
497+
got = want;
498+
no_async:
499+
spin_unlock(&ci->i_ceph_lock);
500+
return got;
501+
}
502+
503+
static void restore_deleg_ino(struct inode *dir, u64 ino)
504+
{
505+
struct ceph_inode_info *ci = ceph_inode(dir);
506+
struct ceph_mds_session *s = NULL;
507+
508+
spin_lock(&ci->i_ceph_lock);
509+
if (ci->i_auth_cap)
510+
s = ceph_get_mds_session(ci->i_auth_cap->session);
511+
spin_unlock(&ci->i_ceph_lock);
512+
if (s) {
513+
int err = ceph_restore_deleg_ino(s, ino);
514+
if (err)
515+
pr_warn("ceph: unable to restore delegated ino 0x%llx to session: %d\n",
516+
ino, err);
517+
ceph_put_mds_session(s);
518+
}
519+
}
520+
521+
static void ceph_async_create_cb(struct ceph_mds_client *mdsc,
522+
struct ceph_mds_request *req)
523+
{
524+
int result = req->r_err ? req->r_err :
525+
le32_to_cpu(req->r_reply_info.head->result);
526+
527+
if (result == -EJUKEBOX)
528+
goto out;
529+
530+
mapping_set_error(req->r_parent->i_mapping, result);
531+
532+
if (result) {
533+
struct dentry *dentry = req->r_dentry;
534+
int pathlen;
535+
u64 base;
536+
char *path = ceph_mdsc_build_path(req->r_dentry, &pathlen,
537+
&base, 0);
538+
539+
ceph_dir_clear_complete(req->r_parent);
540+
if (!d_unhashed(dentry))
541+
d_drop(dentry);
542+
543+
/* FIXME: start returning I/O errors on all accesses? */
544+
pr_warn("ceph: async create failure path=(%llx)%s result=%d!\n",
545+
base, IS_ERR(path) ? "<<bad>>" : path, result);
546+
ceph_mdsc_free_path(path, pathlen);
547+
}
548+
549+
if (req->r_target_inode) {
550+
struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
551+
u64 ino = ceph_vino(req->r_target_inode).ino;
552+
553+
if (req->r_deleg_ino != ino)
554+
pr_warn("%s: inode number mismatch! err=%d deleg_ino=0x%llx target=0x%llx\n",
555+
__func__, req->r_err, req->r_deleg_ino, ino);
556+
mapping_set_error(req->r_target_inode->i_mapping, result);
557+
558+
spin_lock(&ci->i_ceph_lock);
559+
if (ci->i_ceph_flags & CEPH_I_ASYNC_CREATE) {
560+
ci->i_ceph_flags &= ~CEPH_I_ASYNC_CREATE;
561+
wake_up_bit(&ci->i_ceph_flags, CEPH_ASYNC_CREATE_BIT);
562+
}
563+
ceph_kick_flushing_inode_caps(req->r_session, ci);
564+
spin_unlock(&ci->i_ceph_lock);
565+
} else {
566+
pr_warn("%s: no req->r_target_inode for 0x%llx\n", __func__,
567+
req->r_deleg_ino);
568+
}
569+
out:
570+
ceph_mdsc_release_dir_caps(req);
571+
}
572+
573+
static int ceph_finish_async_create(struct inode *dir, struct dentry *dentry,
574+
struct file *file, umode_t mode,
575+
struct ceph_mds_request *req,
576+
struct ceph_acl_sec_ctx *as_ctx,
577+
struct ceph_file_layout *lo)
578+
{
579+
int ret;
580+
char xattr_buf[4];
581+
struct ceph_mds_reply_inode in = { };
582+
struct ceph_mds_reply_info_in iinfo = { .in = &in };
583+
struct ceph_inode_info *ci = ceph_inode(dir);
584+
struct inode *inode;
585+
struct timespec64 now;
586+
struct ceph_vino vino = { .ino = req->r_deleg_ino,
587+
.snap = CEPH_NOSNAP };
588+
589+
ktime_get_real_ts64(&now);
590+
591+
inode = ceph_get_inode(dentry->d_sb, vino);
592+
if (IS_ERR(inode))
593+
return PTR_ERR(inode);
594+
595+
iinfo.inline_version = CEPH_INLINE_NONE;
596+
iinfo.change_attr = 1;
597+
ceph_encode_timespec64(&iinfo.btime, &now);
598+
599+
iinfo.xattr_len = ARRAY_SIZE(xattr_buf);
600+
iinfo.xattr_data = xattr_buf;
601+
memset(iinfo.xattr_data, 0, iinfo.xattr_len);
602+
603+
in.ino = cpu_to_le64(vino.ino);
604+
in.snapid = cpu_to_le64(CEPH_NOSNAP);
605+
in.version = cpu_to_le64(1); // ???
606+
in.cap.caps = in.cap.wanted = cpu_to_le32(CEPH_CAP_ALL_FILE);
607+
in.cap.cap_id = cpu_to_le64(1);
608+
in.cap.realm = cpu_to_le64(ci->i_snap_realm->ino);
609+
in.cap.flags = CEPH_CAP_FLAG_AUTH;
610+
in.ctime = in.mtime = in.atime = iinfo.btime;
611+
in.mode = cpu_to_le32((u32)mode);
612+
in.truncate_seq = cpu_to_le32(1);
613+
in.truncate_size = cpu_to_le64(-1ULL);
614+
in.xattr_version = cpu_to_le64(1);
615+
in.uid = cpu_to_le32(from_kuid(&init_user_ns, current_fsuid()));
616+
in.gid = cpu_to_le32(from_kgid(&init_user_ns, dir->i_mode & S_ISGID ?
617+
dir->i_gid : current_fsgid()));
618+
in.nlink = cpu_to_le32(1);
619+
in.max_size = cpu_to_le64(lo->stripe_unit);
620+
621+
ceph_file_layout_to_legacy(lo, &in.layout);
622+
623+
ret = ceph_fill_inode(inode, NULL, &iinfo, NULL, req->r_session,
624+
req->r_fmode, NULL);
625+
if (ret) {
626+
dout("%s failed to fill inode: %d\n", __func__, ret);
627+
ceph_dir_clear_complete(dir);
628+
if (!d_unhashed(dentry))
629+
d_drop(dentry);
630+
if (inode->i_state & I_NEW)
631+
discard_new_inode(inode);
632+
} else {
633+
struct dentry *dn;
634+
635+
dout("%s d_adding new inode 0x%llx to 0x%lx/%s\n", __func__,
636+
vino.ino, dir->i_ino, dentry->d_name.name);
637+
ceph_dir_clear_ordered(dir);
638+
ceph_init_inode_acls(inode, as_ctx);
639+
if (inode->i_state & I_NEW) {
640+
/*
641+
* If it's not I_NEW, then someone created this before
642+
* we got here. Assume the server is aware of it at
643+
* that point and don't worry about setting
644+
* CEPH_I_ASYNC_CREATE.
645+
*/
646+
ceph_inode(inode)->i_ceph_flags = CEPH_I_ASYNC_CREATE;
647+
unlock_new_inode(inode);
648+
}
649+
if (d_in_lookup(dentry) || d_really_is_negative(dentry)) {
650+
if (!d_unhashed(dentry))
651+
d_drop(dentry);
652+
dn = d_splice_alias(inode, dentry);
653+
WARN_ON_ONCE(dn && dn != dentry);
654+
}
655+
file->f_mode |= FMODE_CREATED;
656+
ret = finish_open(file, dentry, ceph_open);
657+
}
658+
return ret;
659+
}
660+
451661
/*
452662
* Do a lookup + open with a single request. If we get a non-existent
453663
* file or symlink, return 1 so the VFS can retry.
@@ -460,6 +670,7 @@ int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
460670
struct ceph_mds_request *req;
461671
struct dentry *dn;
462672
struct ceph_acl_sec_ctx as_ctx = {};
673+
bool try_async = ceph_test_mount_opt(fsc, ASYNC_DIROPS);
463674
int mask;
464675
int err;
465676

@@ -483,7 +694,7 @@ int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
483694
/* If it's not being looked up, it's negative */
484695
return -ENOENT;
485696
}
486-
697+
retry:
487698
/* do the open */
488699
req = prepare_open_request(dir->i_sb, flags, mode);
489700
if (IS_ERR(req)) {
@@ -492,21 +703,43 @@ int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
492703
}
493704
req->r_dentry = dget(dentry);
494705
req->r_num_caps = 2;
706+
mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
707+
if (ceph_security_xattr_wanted(dir))
708+
mask |= CEPH_CAP_XATTR_SHARED;
709+
req->r_args.open.mask = cpu_to_le32(mask);
710+
req->r_parent = dir;
711+
495712
if (flags & O_CREAT) {
713+
struct ceph_file_layout lo;
714+
496715
req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL;
497716
req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
498717
if (as_ctx.pagelist) {
499718
req->r_pagelist = as_ctx.pagelist;
500719
as_ctx.pagelist = NULL;
501720
}
721+
if (try_async &&
722+
(req->r_dir_caps =
723+
try_prep_async_create(dir, dentry, &lo,
724+
&req->r_deleg_ino))) {
725+
set_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags);
726+
req->r_args.open.flags |= cpu_to_le32(CEPH_O_EXCL);
727+
req->r_callback = ceph_async_create_cb;
728+
err = ceph_mdsc_submit_request(mdsc, dir, req);
729+
if (!err) {
730+
err = ceph_finish_async_create(dir, dentry,
731+
file, mode, req,
732+
&as_ctx, &lo);
733+
} else if (err == -EJUKEBOX) {
734+
restore_deleg_ino(dir, req->r_deleg_ino);
735+
ceph_mdsc_put_request(req);
736+
try_async = false;
737+
goto retry;
738+
}
739+
goto out_req;
740+
}
502741
}
503742

504-
mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
505-
if (ceph_security_xattr_wanted(dir))
506-
mask |= CEPH_CAP_XATTR_SHARED;
507-
req->r_args.open.mask = cpu_to_le32(mask);
508-
509-
req->r_parent = dir;
510743
set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
511744
err = ceph_mdsc_do_request(mdsc,
512745
(flags & (O_CREAT|O_TRUNC)) ? dir : NULL,

‎include/linux/ceph/ceph_fs.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,9 @@ int ceph_flags_to_mode(int flags);
659659
#define CEPH_CAP_ANY (CEPH_CAP_ANY_RD | CEPH_CAP_ANY_EXCL | \
660660
CEPH_CAP_ANY_FILE_WR | CEPH_CAP_FILE_LAZYIO | \
661661
CEPH_CAP_PIN)
662+
#define CEPH_CAP_ALL_FILE (CEPH_CAP_PIN | CEPH_CAP_ANY_SHARED | \
663+
CEPH_CAP_AUTH_EXCL | CEPH_CAP_XATTR_EXCL | \
664+
CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR)
662665

663666
#define CEPH_CAP_LOCKS (CEPH_LOCK_IFILE | CEPH_LOCK_IAUTH | CEPH_LOCK_ILINK | \
664667
CEPH_LOCK_IXATTR)

0 commit comments

Comments
 (0)