From 3c4a5ea385c7d2f2dfe88328e2c5f7778928d92d Mon Sep 17 00:00:00 2001 From: Jan Fajerski Date: Thu, 20 Apr 2017 18:38:43 +0200 Subject: [PATCH] fs: normalize file open flags internally used by cephfs The file open flags (O_foo) are platform specific. Normalize these flags before they are send to the MDS. For processing of client messages the MDS should only compare to these normalized flags. Otherwise this can lead to bogus flags getting transmitted on ppc64. Signed-off-by: Jan Fajerski (cherry picked from commit 88d2da5e93198e69435e288ce00d216d5fe27f80) Conflicts: src/client/Client.cc - trivial resolution, add ceph_flags_sys2wire when flags are logged --- src/client/Client.cc | 35 +++++++++++++++++++---------------- src/common/ceph_fs.cc | 38 ++++++++++++++++++++++++++++++++++---- src/include/ceph_fs.h | 14 ++++++++++++++ src/mds/Server.cc | 19 +++++++++---------- 4 files changed, 76 insertions(+), 30 deletions(-) diff --git a/src/client/Client.cc b/src/client/Client.cc index 2596920814d..d7576070329 100644 --- a/src/client/Client.cc +++ b/src/client/Client.cc @@ -7865,11 +7865,11 @@ int Client::open(const char *relpath, int flags, const UserPerm& perms, mode_t mode, int stripe_unit, int stripe_count, int object_size, const char *data_pool) { - ldout(cct, 3) << "open enter(" << relpath << ", " << flags << "," << mode << ") = " << dendl; + ldout(cct, 3) << "open enter(" << relpath << ", " << ceph_flags_sys2wire(flags) << "," << mode << ") = " << dendl; Mutex::Locker lock(client_lock); tout(cct) << "open" << std::endl; tout(cct) << relpath << std::endl; - tout(cct) << flags << std::endl; + tout(cct) << ceph_flags_sys2wire(flags) << std::endl; Fh *fh = NULL; @@ -7939,7 +7939,7 @@ int Client::open(const char *relpath, int flags, const UserPerm& perms, out: tout(cct) << r << std::endl; - ldout(cct, 3) << "open exit(" << path << ", " << flags << ") = " << r << dendl; + ldout(cct, 3) << "open exit(" << path << ", " << ceph_flags_sys2wire(flags) << ") = " << r << dendl; return r; } @@ -8146,7 +8146,8 @@ void Client::_put_fh(Fh *f) int Client::_open(Inode *in, int flags, mode_t mode, Fh **fhp, const UserPerm& perms) { - int cmode = ceph_flags_to_mode(flags); + // use normalized flags to generate cmode + int cmode = ceph_flags_to_mode(ceph_flags_sys2wire(flags)); if (cmode < 0) return -EINVAL; int want = ceph_caps_for_mode(cmode); @@ -8167,8 +8168,8 @@ int Client::_open(Inode *in, int flags, mode_t mode, Fh **fhp, MetaRequest *req = new MetaRequest(CEPH_MDS_OP_OPEN); filepath path; in->make_nosnap_relative_path(path); - req->set_filepath(path); - req->head.args.open.flags = flags & ~O_CREAT; + req->set_filepath(path); + req->head.args.open.flags = ceph_flags_sys2wire(flags & ~O_CREAT); req->head.args.open.mode = mode; req->head.args.open.pool = -1; if (cct->_conf->client_debug_getattr_caps) @@ -11096,7 +11097,8 @@ int Client::_create(Inode *dir, const char *name, int flags, mode_t mode, return -EDQUOT; } - int cmode = ceph_flags_to_mode(flags); + // use normalized flags to generate cmode + int cmode = ceph_flags_to_mode(ceph_flags_sys2wire(flags)); if (cmode < 0) return -EINVAL; @@ -11117,7 +11119,7 @@ int Client::_create(Inode *dir, const char *name, int flags, mode_t mode, path.push_dentry(name); req->set_filepath(path); req->set_inode(dir); - req->head.args.open.flags = flags | O_CREAT; + req->head.args.open.flags = ceph_flags_sys2wire(flags | O_CREAT); req->head.args.open.stripe_unit = stripe_unit; req->head.args.open.stripe_count = stripe_count; @@ -11889,10 +11891,10 @@ int Client::ll_open(Inode *in, int flags, Fh **fhp, const UserPerm& perms) vinodeno_t vino = _get_vino(in); - ldout(cct, 3) << "ll_open " << vino << " " << flags << dendl; + ldout(cct, 3) << "ll_open " << vino << " " << ceph_flags_sys2wire(flags) << dendl; tout(cct) << "ll_open" << std::endl; tout(cct) << vino.ino.val << std::endl; - tout(cct) << flags << std::endl; + tout(cct) << ceph_flags_sys2wire(flags) << std::endl; int r; if (!cct->_conf->fuse_default_permissions) { @@ -11909,8 +11911,8 @@ int Client::ll_open(Inode *in, int flags, Fh **fhp, const UserPerm& perms) ll_unclosed_fh_set.insert(fhptr); } tout(cct) << (unsigned long)fhptr << std::endl; - ldout(cct, 3) << "ll_open " << vino << " " << flags << " = " << r << " (" << - fhptr << ")" << dendl; + ldout(cct, 3) << "ll_open " << vino << " " << ceph_flags_sys2wire(flags) << + " = " << r << " (" << fhptr << ")" << dendl; return r; } @@ -11923,13 +11925,13 @@ int Client::_ll_create(Inode *parent, const char *name, mode_t mode, vinodeno_t vparent = _get_vino(parent); ldout(cct, 3) << "_ll_create " << vparent << " " << name << " 0" << oct << - mode << dec << " " << flags << ", uid " << perms.uid() + mode << dec << " " << ceph_flags_sys2wire(flags) << ", uid " << perms.uid() << ", gid " << perms.gid() << dendl; tout(cct) << "ll_create" << std::endl; tout(cct) << vparent.ino.val << std::endl; tout(cct) << name << std::endl; tout(cct) << mode << std::endl; - tout(cct) << flags << std::endl; + tout(cct) << ceph_flags_sys2wire(flags) << std::endl; bool created = false; int r = _lookup(parent, name, caps, in, perms); @@ -11990,8 +11992,8 @@ out: tout(cct) << (unsigned long)*fhp << std::endl; tout(cct) << ino << std::endl; ldout(cct, 3) << "_ll_create " << parent << " " << name << " 0" << oct << - mode << dec << " " << flags << " = " << r << " (" << *fhp << " " << - hex << ino << dec << ")" << dendl; + mode << dec << " " << ceph_flags_sys2wire(flags) << " = " << r << " (" << + *fhp << " " << hex << ino << dec << ")" << dendl; return r; } @@ -12030,6 +12032,7 @@ int Client::ll_createx(Inode *parent, const char *name, mode_t mode, Mutex::Locker lock(client_lock); InodeRef in; + int r = _ll_create(parent, name, mode, oflags, &in, caps, fhp, perms); if (r >= 0) { assert(in); diff --git a/src/common/ceph_fs.cc b/src/common/ceph_fs.cc index a4f71be943a..923e38b59d0 100644 --- a/src/common/ceph_fs.cc +++ b/src/common/ceph_fs.cc @@ -17,18 +17,18 @@ int ceph_flags_to_mode(int flags) int mode = -1; #ifdef O_DIRECTORY /* fixme */ - if ((flags & O_DIRECTORY) == O_DIRECTORY) + if ((flags & CEPH_O_DIRECTORY) == CEPH_O_DIRECTORY) return CEPH_FILE_MODE_PIN; #endif switch (flags & O_ACCMODE) { - case O_WRONLY: + case CEPH_O_WRONLY: mode = CEPH_FILE_MODE_WR; break; - case O_RDONLY: + case CEPH_O_RDONLY: mode = CEPH_FILE_MODE_RD; break; - case O_RDWR: + case CEPH_O_RDWR: case O_ACCMODE: /* this is what the VFS does */ mode = CEPH_FILE_MODE_RDWR; break; @@ -54,3 +54,33 @@ int ceph_caps_for_mode(int mode) return caps; } + +int ceph_flags_sys2wire(int flags) +{ + int wire_flags = 0; + + switch (flags & O_ACCMODE) { + case O_RDONLY: + wire_flags |= CEPH_O_RDONLY; + break; + case O_WRONLY: + wire_flags |= CEPH_O_WRONLY; + break; + case O_RDWR: + wire_flags |= CEPH_O_RDWR; + break; + } + flags &= ~O_ACCMODE; + +#define ceph_sys2wire(a) if (flags & a) { wire_flags |= CEPH_##a; flags &= ~a; } + + ceph_sys2wire(O_CREAT); + ceph_sys2wire(O_EXCL); + ceph_sys2wire(O_TRUNC); + ceph_sys2wire(O_DIRECTORY); + ceph_sys2wire(O_NOFOLLOW); + +#undef ceph_sys2wire + + return wire_flags; +} diff --git a/src/include/ceph_fs.h b/src/include/ceph_fs.h index ffc510813b9..abfb594d610 100644 --- a/src/include/ceph_fs.h +++ b/src/include/ceph_fs.h @@ -382,6 +382,20 @@ extern const char *ceph_mds_op_name(int op); #define CEPH_SETATTR_ATIME_NOW (1 << 8) #define CEPH_SETATTR_KILL_SGUID (1 << 10) +/* + * open request flags + */ +#define CEPH_O_RDONLY 00000000 +#define CEPH_O_WRONLY 00000001 +#define CEPH_O_RDWR 00000002 +#define CEPH_O_CREAT 00000100 +#define CEPH_O_EXCL 00000200 +#define CEPH_O_TRUNC 00001000 +#define CEPH_O_DIRECTORY 00200000 +#define CEPH_O_NOFOLLOW 00400000 + +int ceph_flags_sys2wire(int flags); + /* * Ceph setxattr request flags. */ diff --git a/src/mds/Server.cc b/src/mds/Server.cc index 007ee12802e..d2fa6b3ec92 100644 --- a/src/mds/Server.cc +++ b/src/mds/Server.cc @@ -58,7 +58,6 @@ #include "osd/OSDMap.h" #include -#include #include #include @@ -2903,7 +2902,7 @@ void Server::handle_client_open(MDRequestRef& mdr) return; } - bool need_auth = !file_mode_is_readonly(cmode) || (flags & O_TRUNC); + bool need_auth = !file_mode_is_readonly(cmode) || (flags & CEPH_O_TRUNC); if ((cmode & CEPH_FILE_MODE_WR) && mdcache->is_readonly()) { dout(7) << "read-only FS" << dendl; @@ -2928,8 +2927,8 @@ void Server::handle_client_open(MDRequestRef& mdr) // can only open non-regular inode with mode FILE_MODE_PIN, at least for now. cmode = CEPH_FILE_MODE_PIN; // the inode is symlink and client wants to follow it, ignore the O_TRUNC flag. - if (cur->inode.is_symlink() && !(flags & O_NOFOLLOW)) - flags &= ~O_TRUNC; + if (cur->inode.is_symlink() && !(flags & CEPH_O_NOFOLLOW)) + flags &= ~CEPH_O_TRUNC; } dout(10) << "open flags = " << flags @@ -2943,13 +2942,13 @@ void Server::handle_client_open(MDRequestRef& mdr) respond_to_request(mdr, -ENXIO); // FIXME what error do we want? return; }*/ - if ((flags & O_DIRECTORY) && !cur->inode.is_dir() && !cur->inode.is_symlink()) { + if ((flags & CEPH_O_DIRECTORY) && !cur->inode.is_dir() && !cur->inode.is_symlink()) { dout(7) << "specified O_DIRECTORY on non-directory " << *cur << dendl; respond_to_request(mdr, -EINVAL); return; } - if ((flags & O_TRUNC) && !cur->inode.is_file()) { + if ((flags & CEPH_O_TRUNC) && !cur->inode.is_file()) { dout(7) << "specified O_TRUNC on !(file|symlink) " << *cur << dendl; // we should return -EISDIR for directory, return -EINVAL for other non-regular respond_to_request(mdr, cur->inode.is_dir() ? -EISDIR : -EINVAL); @@ -2987,7 +2986,7 @@ void Server::handle_client_open(MDRequestRef& mdr) } // O_TRUNC - if ((flags & O_TRUNC) && !mdr->has_completed) { + if ((flags & CEPH_O_TRUNC) && !mdr->has_completed) { assert(cur->is_auth()); xlocks.insert(&cur->filelock); @@ -3125,7 +3124,7 @@ void Server::handle_client_openc(MDRequestRef& mdr) return; } - if (!(req->head.args.open.flags & O_EXCL)) { + if (!(req->head.args.open.flags & CEPH_O_EXCL)) { int r = mdcache->path_traverse(mdr, NULL, NULL, req->get_filepath(), &mdr->dn[0], NULL, MDS_TRAVERSE_FORWARD); if (r > 0) return; @@ -3148,7 +3147,7 @@ void Server::handle_client_openc(MDRequestRef& mdr) // r == -ENOENT } - bool excl = (req->head.args.open.flags & O_EXCL); + bool excl = (req->head.args.open.flags & CEPH_O_EXCL); set rdlocks, wrlocks, xlocks; file_layout_t *dir_layout = NULL; CDentry *dn = rdlock_path_xlock_dentry(mdr, 0, rdlocks, wrlocks, xlocks, @@ -3223,7 +3222,7 @@ void Server::handle_client_openc(MDRequestRef& mdr) if (!dnl->is_null()) { // it existed. - assert(req->head.args.open.flags & O_EXCL); + assert(req->head.args.open.flags & CEPH_O_EXCL); dout(10) << "O_EXCL, target exists, failing with -EEXIST" << dendl; mdr->tracei = dnl->get_inode(); mdr->tracedn = dn; -- 2.47.3