From 3bfd614a358dc664469f8d5f56e9248818495bb2 Mon Sep 17 00:00:00 2001 From: Patrick Donnelly Date: Tue, 11 Mar 2025 10:49:50 -0400 Subject: [PATCH] client: refactor / optimize chdir In particular: there's no reason to do a getcwd after chdir. Signed-off-by: Patrick Donnelly --- src/client/Client.cc | 24 ++++++++++-------------- src/client/Client.h | 7 +++---- src/client/SyntheticClient.cc | 3 +-- src/libcephfs.cc | 2 +- 4 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/client/Client.cc b/src/client/Client.cc index 5090e7182687e..e32d2d3360c31 100644 --- a/src/client/Client.cc +++ b/src/client/Client.cc @@ -12292,10 +12292,7 @@ int Client::statxat(int dirfd, const char *relpath, return r; } -// not written yet, but i want to link! - -int Client::chdir(const char *relpath, std::string &new_cwd, - const UserPerm& perms) +int Client::chdir(const char *relpath, const UserPerm& perms) { RWRef_t mref_reader(mount_state, CLIENT_MOUNTING); if (!mref_reader.is_state_satisfied()) @@ -12311,18 +12308,17 @@ int Client::chdir(const char *relpath, std::string &new_cwd, return rc; } - if (!(in.get()->is_dir())) + if (!in->is_dir()) return -ENOTDIR; - if (cwd != in) - cwd.swap(in); + cwd = std::move(in); + ldout(cct, 3) << "chdir(" << relpath << ") cwd now " << cwd->ino << dendl; - _getcwd(new_cwd, perms); return 0; } -void Client::_getcwd(string& dir, const UserPerm& perms) +int Client::_getcwd(string& dir, const UserPerm& perms) { filepath path; ldout(cct, 10) << __func__ << " " << *cwd << dendl; @@ -12333,7 +12329,7 @@ void Client::_getcwd(string& dir, const UserPerm& perms) // A cwd or ancester is unlinked if (in->dentries.empty()) { - return; + return -ENOENT; } Dentry *dn = in->get_first_parent(); @@ -12360,17 +12356,17 @@ void Client::_getcwd(string& dir, const UserPerm& perms) } dir = "/"; dir += path.get_path(); + return 0; } -void Client::getcwd(string& dir, const UserPerm& perms) +int Client::getcwd(string& dir, const UserPerm& perms) { RWRef_t mref_reader(mount_state, CLIENT_MOUNTING); if (!mref_reader.is_state_satisfied()) - return; + return -ENOTCONN; std::scoped_lock l(client_lock); - - _getcwd(dir, perms); + return _getcwd(dir, perms); } int Client::statfs(const char *path, struct statvfs *stbuf, diff --git a/src/client/Client.h b/src/client/Client.h index 23376052f9efc..e3612e75cc6c0 100644 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -343,10 +343,9 @@ public: // these should (more or less) mirror the actual system calls. int statfs(const char *path, struct statvfs *stbuf, const UserPerm& perms); - // crap - int chdir(const char *s, std::string &new_cwd, const UserPerm& perms); - void _getcwd(std::string& cwd, const UserPerm& perms); - void getcwd(std::string& cwd, const UserPerm& perms); + int chdir(const char *path, const UserPerm& perms); + int _getcwd(std::string& cwd, const UserPerm& perms); + int getcwd(std::string& cwd, const UserPerm& perms); // namespace ops int opendir(const char *name, dir_result_t **dirpp, const UserPerm& perms); diff --git a/src/client/SyntheticClient.cc b/src/client/SyntheticClient.cc index 1160b2dd7dea5..7d12e2f097684 100644 --- a/src/client/SyntheticClient.cc +++ b/src/client/SyntheticClient.cc @@ -1219,8 +1219,7 @@ int SyntheticClient::play_trace(Trace& t, string& prefix, bool metadata_only) const char *a = t.get_string(buf, p); // Client users should remember their path, but since this // is just a synthetic client we ignore it. - std::string ignore; - client->chdir(a, ignore, perms); + client->chdir(a, perms); } else if (strcmp(op, "statfs") == 0) { struct statvfs stbuf; client->statfs("/", &stbuf, perms); diff --git a/src/libcephfs.cc b/src/libcephfs.cc index ce06ff7eef04e..9fc20f731c523 100644 --- a/src/libcephfs.cc +++ b/src/libcephfs.cc @@ -322,7 +322,7 @@ public: int chdir(const char *to, const UserPerm& perms) { - return client->chdir(to, cwd, perms); + return client->chdir(to, perms); } CephContext *get_ceph_context() const { -- 2.39.5