From a32add15b8ec7866c1d33a34a2b309b19270c0ba Mon Sep 17 00:00:00 2001 From: Gui Hecheng Date: Fri, 14 Apr 2017 17:28:19 +0800 Subject: [PATCH] rgw_file: add locks to fix potential race Signed-off-by: Gui Hecheng --- src/rgw/rgw_file.cc | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/rgw/rgw_file.cc b/src/rgw/rgw_file.cc index eb24db6d95a6f..297c2561a8930 100644 --- a/src/rgw/rgw_file.cc +++ b/src/rgw/rgw_file.cc @@ -80,6 +80,7 @@ namespace rgw { RGWFileHandle::FLAG_BUCKET); if (get<0>(fhr)) { RGWFileHandle* rgw_fh = get<0>(fhr); + lock_guard guard(rgw_fh->mtx); rgw_fh->set_times(req.get_ctime()); /* restore attributes */ auto ux_key = req.get_attr(RGW_ATTR_UNIX_KEY1); @@ -127,6 +128,7 @@ namespace rgw { fhr = lookup_fh(parent, path, RGWFileHandle::FLAG_CREATE); if (get<0>(fhr)) { RGWFileHandle* rgw_fh = get<0>(fhr); + lock_guard guard(rgw_fh->mtx); rgw_fh->set_size(req.get_size()); rgw_fh->set_times(req.get_mtime()); /* restore attributes */ @@ -153,6 +155,7 @@ namespace rgw { fhr = lookup_fh(parent, path, RGWFileHandle::FLAG_DIRECTORY); if (get<0>(fhr)) { RGWFileHandle* rgw_fh = get<0>(fhr); + lock_guard guard(rgw_fh->mtx); rgw_fh->set_size(req.get_size()); rgw_fh->set_times(req.get_mtime()); /* restore attributes */ @@ -195,6 +198,7 @@ namespace rgw { if (get<0>(fhr)) { /* for now use the parent object's mtime */ RGWFileHandle* rgw_fh = get<0>(fhr); + lock_guard guard(rgw_fh->mtx); rgw_fh->set_mtime(parent->get_mtime()); } } @@ -225,6 +229,7 @@ namespace rgw { int rc = rgwlib.get_fe()->execute_req(&req); if ((rc == 0) && (req.get_ret() == 0)) { + lock_guard(rgw_fh->mtx); rgw_fh->set_atime(real_clock::to_timespec(real_clock::now())); *bytes_read = req.nread; } @@ -587,7 +592,8 @@ namespace rgw { if ((rc == 0) && (rc2 == 0)) { /* XXX atomicity */ - LookupFHResult fhr = lookup_fh(parent, name, RGWFileHandle::FLAG_CREATE); + LookupFHResult fhr = lookup_fh(parent, name, RGWFileHandle::FLAG_CREATE | + RGWFileHandle::FLAG_LOCK); RGWFileHandle* rgw_fh = get<0>(fhr); if (rgw_fh) { if (get<1>(fhr) & RGWFileHandle::FLAG_CREATE) { @@ -595,7 +601,6 @@ namespace rgw { real_time t = real_clock::now(); rgw_fh->create_stat(st, mask); rgw_fh->set_times(t); - rgw_fh->open_for_create(); // XXX needed? parent->set_mtime(real_clock::to_timespec(t)); parent->set_ctime(real_clock::to_timespec(t)); @@ -603,6 +608,7 @@ namespace rgw { if (st) (void) rgw_fh->stat(st); get<0>(mkr) = rgw_fh; + rgw_fh->mtx.unlock(); } else rc = -EIO; } @@ -634,6 +640,8 @@ namespace rgw { int rc, rc2; buffer::list ux_key, ux_attrs; + lock_guard guard(rgw_fh->mtx); + switch(rgw_fh->fh.fh_type) { case RGW_FS_TYPE_FILE: { -- 2.39.5