ceph: don't use d_add in ceph_handle_snapdir
Subsystems (2)
PATCH v2 1/1 ceph: don't use d_add in ceph_handle_snapdir
It's possible ceph_get_snapdir could end up finding a (disconnected) inode that already exists in the cache. Change the prototype for ceph_handle_snapdir to return a dentry pointer and have it use d_splice_alias so we don't end up with an aliased dentry in the cache.
URL: https://tracker.ceph.com/issues/49843
--- fs/ceph/dir.c | 32 ++++++++++++++++++++------------ fs/ceph/file.c | 7 +++++-- fs/ceph/super.h | 2 +- 3 files changed, 26 insertions(+), 15 deletions(-)
v2:
zero out err var when ceph_handle_snapdir returns success
fs/ceph/dir.c +20 -12
@@ -667,8 +667,8 @@ static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int whence)/* * Handle lookups for the hidden .snap directory. */ -int ceph_handle_snapdir (struct ceph_mds_request *req, -struct dentry *dentry,int err) +struct dentry *ceph_handle_snapdir (struct ceph_mds_request *req, +struct dentry *dentry,int err) {struct ceph_fs_client *fsc =ceph_sb_to_client (dentry->d_sb);struct inode *parent =d_inode (dentry->d_parent);/* we hold i_mutex */
@@ -676,18 +676,19 @@ int ceph_handle_snapdir(struct ceph_mds_request *req,/* .snap dir? */ if (err == -ENOENT &&ceph_snap (parent) ==CEPH_NOSNAP && -strcmp (dentry->d_name.name, - fsc->mount_options->snapdir_name) ==0 ) { +strcmp (dentry->d_name.name, fsc->mount_options->snapdir_name) ==0 ) { +struct dentry *res;struct inode *inode =ceph_get_snapdir (parent); +if (IS_ERR (inode)) -return PTR_ERR (inode); -dout ("ENOENT on snapdir %p '%pd', linking to snapdir %p\n" , - dentry, dentry, inode); -BUG_ON (!d_unhashed (dentry)); -d_add (dentry, inode); - err =0 ; +return ERR_CAST (inode); + res =d_splice_alias (inode, dentry); +dout ("ENOENT on snapdir %p '%pd', linking to snapdir %p. Spliced dentry %p\n" , + dentry, dentry, inode, res); +if (res) + dentry = res; } -return err; +return dentry; }/*
@@ -743,6 +744,7 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,struct ceph_fs_client *fsc =ceph_sb_to_client (dir->i_sb);struct ceph_mds_client *mdsc =ceph_sb_to_mdsc (dir->i_sb);struct ceph_mds_request *req; +struct dentry *res;int op;int mask;int err;
@@ -793,7 +795,13 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry, req->r_parent = dir;set_bit (CEPH_MDS_R_PARENT_LOCKED , &req->r_req_flags); err =ceph_mdsc_do_request (mdsc,NULL , req); - err =ceph_handle_snapdir (req, dentry, err); + res =ceph_handle_snapdir (req, dentry, err); +if (IS_ERR (res)) { + err =PTR_ERR (res); + }else { + dentry = res; + err =0 ; + } dentry =ceph_finish_lookup (req, dentry, err);ceph_mdsc_put_request (req);/* will dput(dentry) */ dout ("lookup result=%p\n" , dentry);
fs/ceph/file.c +5 -2
@@ -739,9 +739,12 @@ int ceph_atomic_open(struct inode *dir, struct dentry *dentry, err =ceph_mdsc_do_request (mdsc, (flags & (O_CREAT |O_TRUNC )) ? dir :NULL , req); - err =ceph_handle_snapdir (req, dentry, err); -if (err) + dentry =ceph_handle_snapdir (req, dentry, err); +if (IS_ERR (dentry)) { + err =PTR_ERR (dentry); goto out_req; + } + err =0 ;if ((flags &O_CREAT ) && !req->r_reply_info.head->is_dentry) err =ceph_handle_notrace_create (dir, dentry);
fs/ceph/super.h +1 -1
@@ -1193,7 +1193,7 @@ extern const struct dentry_operations ceph_dentry_ops;extern loff_t ceph_make_fpos (unsigned high,unsigned off,bool hash_order);extern int ceph_handle_notrace_create (struct inode *dir,struct dentry *dentry); -extern int ceph_handle_snapdir (struct ceph_mds_request *req, +extern struct dentry *ceph_handle_snapdir (struct ceph_mds_request *req,struct dentry *dentry,int err);extern struct dentry *ceph_finish_lookup (struct ceph_mds_request *req,struct dentry *dentry,int err);
Signature · 1 line
2.30.2
Quoted 108 lines · Jeff Layton
It's possible
ceph_get_snapdircould end up finding a (disconnected) inode that already exists in the cache. Change the prototype forceph_handle_snapdirto return a dentry pointer and have it used_splice_aliasso we don't end up with an aliased dentry in the cache.URL: https://tracker.ceph.com/issues/49843
Reported-by Al Viro <[email protected]>Signed-off-by Jeff Layton <[email protected]>--- fs/ceph/dir.c | 32 ++++++++++++++++++++------------ fs/ceph/file.c | 7 +++++-- fs/ceph/super.h | 2 +- 3 files changed, 26 insertions(+), 15 deletions(-)v2:
zero out err var whenceph_handle_snapdirreturns successfs/ceph/dir.c +20
-12@@ -667,8 +667,8 @@ static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int whence)/* * Handle lookups for the hidden .snap directory. */ -int ceph_handle_snapdir (struct ceph_mds_request *req, -struct dentry *dentry,int err) +struct dentry *ceph_handle_snapdir (struct ceph_mds_request *req, +struct dentry *dentry,int err) {struct ceph_fs_client *fsc =ceph_sb_to_client (dentry->d_sb);struct inode *parent =d_inode (dentry->d_parent);/* we hold i_mutex */ @@ -676,18 +676,19 @@ int ceph_handle_snapdir(struct ceph_mds_request *req,/* .snap dir? */ if (err == -ENOENT &&ceph_snap (parent) ==CEPH_NOSNAP && -strcmp (dentry->d_name.name, - fsc->mount_options->snapdir_name) ==0 ) { +strcmp (dentry->d_name.name, fsc->mount_options->snapdir_name) ==0 ) { +struct dentry *res;struct inode *inode =ceph_get_snapdir (parent); +if (IS_ERR (inode)) -return PTR_ERR (inode); -dout ("ENOENT on snapdir %p '%pd', linking to snapdir %p\n" , - dentry, dentry, inode); -BUG_ON (!d_unhashed (dentry)); -d_add (dentry, inode); - err =0 ; +return ERR_CAST (inode); + res =d_splice_alias (inode, dentry); +dout ("ENOENT on snapdir %p '%pd', linking to snapdir %p. Spliced dentry %p\n" , + dentry, dentry, inode, res); +if (res) + dentry = res; } -return err; +return dentry; }/* @@ -743,6 +744,7 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,struct ceph_fs_client *fsc =ceph_sb_to_client (dir->i_sb);struct ceph_mds_client *mdsc =ceph_sb_to_mdsc (dir->i_sb);struct ceph_mds_request *req; +struct dentry *res;int op;int mask;int err;@@ -793,7 +795,13 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry, req->r_parent = dir;set_bit (CEPH_MDS_R_PARENT_LOCKED , &req->r_req_flags); err =ceph_mdsc_do_request (mdsc,NULL , req); - err =ceph_handle_snapdir (req, dentry, err); + res =ceph_handle_snapdir (req, dentry, err); +if (IS_ERR (res)) { + err =PTR_ERR (res); + }else { + dentry = res; + err =0 ; + } dentry =ceph_finish_lookup (req, dentry, err);ceph_mdsc_put_request (req);/* will dput(dentry) */ dout ("lookup result=%p\n" , dentry);fs/ceph/file.c +5
-2@@ -739,9 +739,12 @@ int ceph_atomic_open(struct inode *dir, struct dentry *dentry, err =ceph_mdsc_do_request (mdsc, (flags & (O_CREAT |O_TRUNC )) ? dir :NULL , req); - err =ceph_handle_snapdir (req, dentry, err); -if (err) + dentry =ceph_handle_snapdir (req, dentry, err); +if (IS_ERR (dentry)) { + err =PTR_ERR (dentry); goto out_req; + } + err =0 ;if ((flags &O_CREAT ) && !req->r_reply_info.head->is_dentry) err =ceph_handle_notrace_create (dir, dentry);fs/ceph/super.h +0
-0
fs/ceph/super.h extern const struct dentry_operations ceph_dentry_ops; · line 1199
Quoted 8 lines
fs/ceph/super.h +1
-1@@ -1193,7 +1193,7 @@ extern const struct dentry_operations ceph_dentry_ops;extern loff_t ceph_make_fpos (unsigned high,unsigned off,bool hash_order);extern int ceph_handle_notrace_create (struct inode *dir,struct dentry *dentry); -extern int ceph_handle_snapdir (struct ceph_mds_request *req, +extern struct dentry *ceph_handle_snapdir (struct ceph_mds_request *req,struct dentry *dentry,int err);extern struct dentry *ceph_finish_lookup (struct ceph_mds_request *req,struct dentry *dentry,int err);
LGTM.