Skip to content

Commit

Permalink
ksmbd: vfs: pass additional flags parameter to ksmbd_vfs_fp_rename
Browse files Browse the repository at this point in the history
The flag is passed verbatim to vfs_rename(). This simplifies callers
as it's no longer necessary to handle the overwrite case.

Signed-off-by: Marios Makassikis <[email protected]>
  • Loading branch information
Marios Makassikis authored and namjaejeon committed Oct 25, 2023
1 parent 133fdd6 commit f26c977
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
46 changes: 21 additions & 25 deletions smb1pdu.c
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,6 @@ int smb_rename(struct ksmbd_work *work)
struct ksmbd_file *fp = NULL;
int oldname_len;
struct path path;
bool file_present = true;
int rc = 0;

if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Expand Down Expand Up @@ -793,19 +792,6 @@ int smb_rename(struct ksmbd_work *work)
goto out;
}

rc = ksmbd_vfs_kern_path(work, newname, LOOKUP_NO_SYMLINKS, &path, 1);
if (rc)
file_present = false;
else
path_put(&path);

if (file_present && strncmp(oldname, newname, strlen(oldname))) {
rc = -EEXIST;
rsp->hdr.Status.CifsError = STATUS_OBJECT_NAME_COLLISION;
ksmbd_debug(SMB, "cannot rename already existing file\n");
goto out;
}

ksmbd_debug(SMB, "rename %s -> %s\n", oldname, newname);
rc = ksmbd_vfs_kern_path(work, oldname, LOOKUP_NO_SYMLINKS, &path, 1);
if (rc)
Expand All @@ -818,7 +804,7 @@ int smb_rename(struct ksmbd_work *work)
goto out;
}

rc = ksmbd_vfs_fp_rename(work, fp, newname);
rc = ksmbd_vfs_fp_rename(work, fp, newname, RENAME_NOREPLACE);
if (rc) {
rsp->hdr.Status.CifsError = STATUS_NO_MEMORY;
path_put(&path);
Expand All @@ -832,6 +818,23 @@ int smb_rename(struct ksmbd_work *work)
ksmbd_close_fd(work, fp->volatile_id);
kfree(oldname);
kfree(newname);

if (rc) {
switch (rc) {
case -EEXIST:
rsp->hdr.Status.CifsError =
STATUS_OBJECT_NAME_COLLISION;
break;
case -ENOENT:
rsp->hdr.Status.CifsError =
NT_STATUS_OBJECT_NAME_NOT_FOUND;
break;
case -ENOMEM:
rsp->hdr.Status.CifsError = STATUS_NO_MEMORY;
break;
}
}

return rc;
}

Expand Down Expand Up @@ -7285,7 +7288,7 @@ static int smb_fileinfo_rename(struct ksmbd_work *work)
struct set_file_rename *info;
struct ksmbd_file *fp;
char *newname;
int rc = 0;
int rc = 0, flags;

req = (struct smb_com_trans2_sfi_req *)work->request_buf;
rsp = (struct smb_com_trans2_sfi_rsp *)work->response_buf;
Expand All @@ -7306,14 +7309,7 @@ static int smb_fileinfo_rename(struct ksmbd_work *work)
return -ENOENT;
}

if (info->overwrite) {
rc = ksmbd_vfs_truncate(work, fp, 0);
if (rc) {
rsp->hdr.Status.CifsError = STATUS_INVALID_PARAMETER;
ksmbd_fd_put(work, fp);
return rc;
}
}
flags = info->overwrite ? 0 : RENAME_NOREPLACE;

newname = smb_get_name(share, info->target_name, PATH_MAX, work, 0);
if (IS_ERR(newname)) {
Expand All @@ -7323,7 +7319,7 @@ static int smb_fileinfo_rename(struct ksmbd_work *work)
}

ksmbd_debug(SMB, "new name(%s)\n", newname);
rc = ksmbd_vfs_fp_rename(work, fp, newname);
rc = ksmbd_vfs_fp_rename(work, fp, newname, flags);
if (rc) {
rsp->hdr.Status.CifsError = STATUS_UNEXPECTED_IO_ERROR;
goto out;
Expand Down
18 changes: 12 additions & 6 deletions vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1487,7 +1487,8 @@ static int __ksmbd_vfs_rename(struct ksmbd_work *work,
struct mnt_idmap *dst_idmap,
struct dentry *dst_dent_parent,
struct dentry *trap_dent,
char *dst_name)
char *dst_name,
int flags)
#else
static int __ksmbd_vfs_rename(struct ksmbd_work *work,
struct user_namespace *src_user_ns,
Expand All @@ -1496,7 +1497,8 @@ static int __ksmbd_vfs_rename(struct ksmbd_work *work,
struct user_namespace *dst_user_ns,
struct dentry *dst_dent_parent,
struct dentry *trap_dent,
char *dst_name)
char *dst_name,
int flags)
#endif
{
struct dentry *dst_dent;
Expand Down Expand Up @@ -1549,6 +1551,7 @@ static int __ksmbd_vfs_rename(struct ksmbd_work *work,
.new_mnt_idmap = dst_idmap,
.new_dir = d_inode(dst_dent_parent),
.new_dentry = dst_dent,
.flags = flags,
};
#else
struct renamedata rd = {
Expand All @@ -1558,6 +1561,7 @@ static int __ksmbd_vfs_rename(struct ksmbd_work *work,
.new_mnt_userns = dst_user_ns,
.new_dir = d_inode(dst_dent_parent),
.new_dentry = dst_dent,
.flags = flags,
};
#endif
err = vfs_rename(&rd);
Expand All @@ -1567,7 +1571,7 @@ static int __ksmbd_vfs_rename(struct ksmbd_work *work,
d_inode(dst_dent_parent),
dst_dent,
NULL,
0);
flags);
#endif
}
if (err)
Expand All @@ -1580,7 +1584,7 @@ static int __ksmbd_vfs_rename(struct ksmbd_work *work,
}

int ksmbd_vfs_fp_rename(struct ksmbd_work *work, struct ksmbd_file *fp,
char *newname)
char *newname, int flags)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
struct mnt_idmap *idmap;
Expand Down Expand Up @@ -1650,7 +1654,8 @@ int ksmbd_vfs_fp_rename(struct ksmbd_work *work, struct ksmbd_file *fp,
mnt_idmap(dst_path.mnt),
dst_dent_parent,
trap_dent,
dst_name);
dst_name,
flags);
#else
err = __ksmbd_vfs_rename(work,
user_ns,
Expand All @@ -1659,7 +1664,8 @@ int ksmbd_vfs_fp_rename(struct ksmbd_work *work, struct ksmbd_file *fp,
mnt_user_ns(dst_path.mnt),
dst_dent_parent,
trap_dent,
dst_name);
dst_name,
flags);
#endif
out_lock:
dput(src_dent);
Expand Down
2 changes: 1 addition & 1 deletion vfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ int ksmbd_vfs_rename(struct ksmbd_work *work, const struct path *old_path,
char *newname, int flags);
#else
int ksmbd_vfs_fp_rename(struct ksmbd_work *work, struct ksmbd_file *fp,
char *newname);
char *newname, int flags);
#endif
int ksmbd_vfs_truncate(struct ksmbd_work *work,
struct ksmbd_file *fp, loff_t size);
Expand Down

0 comments on commit f26c977

Please sign in to comment.