Skip to content

Commit cefd1b8

Browse files
author
Miklos Szeredi
committed
fuse: decrement nlink on overwriting rename
Rename didn't decrement/clear nlink on overwritten target inode. Create a common helper fuse_entry_unlinked() that handles this for unlink, rmdir and rename. Signed-off-by: Miklos Szeredi <[email protected]>
1 parent 84840ef commit cefd1b8

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

fs/fuse/dir.c

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,29 @@ void fuse_update_ctime(struct inode *inode)
755755
}
756756
}
757757

758+
static void fuse_entry_unlinked(struct dentry *entry)
759+
{
760+
struct inode *inode = d_inode(entry);
761+
struct fuse_conn *fc = get_fuse_conn(inode);
762+
struct fuse_inode *fi = get_fuse_inode(inode);
763+
764+
spin_lock(&fi->lock);
765+
fi->attr_version = atomic64_inc_return(&fc->attr_version);
766+
/*
767+
* If i_nlink == 0 then unlink doesn't make sense, yet this can
768+
* happen if userspace filesystem is careless. It would be
769+
* difficult to enforce correct nlink usage so just ignore this
770+
* condition here
771+
*/
772+
if (S_ISDIR(inode->i_mode))
773+
clear_nlink(inode);
774+
else if (inode->i_nlink > 0)
775+
drop_nlink(inode);
776+
spin_unlock(&fi->lock);
777+
fuse_invalidate_entry_cache(entry);
778+
fuse_update_ctime(inode);
779+
}
780+
758781
static int fuse_unlink(struct inode *dir, struct dentry *entry)
759782
{
760783
int err;
@@ -771,23 +794,8 @@ static int fuse_unlink(struct inode *dir, struct dentry *entry)
771794
args.in_args[0].value = entry->d_name.name;
772795
err = fuse_simple_request(fm, &args);
773796
if (!err) {
774-
struct inode *inode = d_inode(entry);
775-
struct fuse_inode *fi = get_fuse_inode(inode);
776-
777-
spin_lock(&fi->lock);
778-
fi->attr_version = atomic64_inc_return(&fm->fc->attr_version);
779-
/*
780-
* If i_nlink == 0 then unlink doesn't make sense, yet this can
781-
* happen if userspace filesystem is careless. It would be
782-
* difficult to enforce correct nlink usage so just ignore this
783-
* condition here
784-
*/
785-
if (inode->i_nlink > 0)
786-
drop_nlink(inode);
787-
spin_unlock(&fi->lock);
788797
fuse_dir_changed(dir);
789-
fuse_invalidate_entry_cache(entry);
790-
fuse_update_ctime(inode);
798+
fuse_entry_unlinked(entry);
791799
} else if (err == -EINTR)
792800
fuse_invalidate_entry(entry);
793801
return err;
@@ -809,9 +817,8 @@ static int fuse_rmdir(struct inode *dir, struct dentry *entry)
809817
args.in_args[0].value = entry->d_name.name;
810818
err = fuse_simple_request(fm, &args);
811819
if (!err) {
812-
clear_nlink(d_inode(entry));
813820
fuse_dir_changed(dir);
814-
fuse_invalidate_entry_cache(entry);
821+
fuse_entry_unlinked(entry);
815822
} else if (err == -EINTR)
816823
fuse_invalidate_entry(entry);
817824
return err;
@@ -851,10 +858,8 @@ static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
851858
fuse_dir_changed(newdir);
852859

853860
/* newent will end up negative */
854-
if (!(flags & RENAME_EXCHANGE) && d_really_is_positive(newent)) {
855-
fuse_invalidate_entry_cache(newent);
856-
fuse_update_ctime(d_inode(newent));
857-
}
861+
if (!(flags & RENAME_EXCHANGE) && d_really_is_positive(newent))
862+
fuse_entry_unlinked(newent);
858863
} else if (err == -EINTR) {
859864
/* If request was interrupted, DEITY only knows if the
860865
rename actually took place. If the invalidation

0 commit comments

Comments
 (0)