xref: /illumos-gate/usr/src/lib/smbclnt/libfksmbfs/common/fake_rename.c (revision 8329232e00f1048795bae53acb230316243aadb5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2017, Joyent, Inc.
25  * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
26  * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
27  */
28 
29 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
30 /*	  All Rights Reserved	*/
31 
32 /*
33  * University Copyright- Copyright (c) 1982, 1986, 1988
34  * The Regents of the University of California
35  * All Rights Reserved
36  *
37  * University Acknowledgment- Portions of this document are derived from
38  * software developed by the University of California, Berkeley, and its
39  * contributors.
40  */
41 
42 /*
43  * Portions of code from both of:
44  *	syscall/rename.c
45  *	fs/vnode.c
46  * heavily modified for this use.
47  */
48 
49 #include <sys/types.h>
50 #include <sys/param.h>
51 #include <sys/t_lock.h>
52 #include <sys/errno.h>
53 #include <sys/cred.h>
54 #include <sys/user.h>
55 #include <sys/uio.h>
56 #include <sys/file.h>
57 #include <sys/pathname.h>
58 #include <sys/vfs.h>
59 #include <sys/vfs_opreg.h>
60 #include <sys/vnode.h>
61 #include <sys/rwstlock.h>
62 #include <sys/fem.h>
63 #include <sys/stat.h>
64 #include <sys/mode.h>
65 #include <sys/conf.h>
66 #include <sys/sysmacros.h>
67 #include <sys/cmn_err.h>
68 #include <sys/systm.h>
69 #include <sys/kmem.h>
70 #include <sys/debug.h>
71 #include <sys/acl.h>
72 #include <sys/nbmlock.h>
73 #include <sys/fcntl.h>
74 #include <fs/fs_subr.h>
75 #include <sys/taskq.h>
76 #include <fs/fs_reparse.h>
77 #include <sys/time.h>
78 
79 #include <libfksmbfs.h>
80 
81 int
fake_rename(char * frompath,char * topath)82 fake_rename(char *frompath, char *topath)
83 {
84 	vnode_t *fdvp = NULL;
85 	vnode_t *tdvp = NULL;
86 	char *fname = NULL;
87 	char *tname = NULL;
88 	int error;
89 
90 	/*
91 	 * Lookup to and from directories.
92 	 */
93 	if ((error = fake_lookup_dir(frompath, &fdvp, &fname)) != 0)
94 		goto out;
95 	if ((error = fake_lookup_dir(topath, &tdvp, &tname)) != 0)
96 		goto out;
97 
98 	/*
99 	 * Make sure both the from vnode directory and the to directory
100 	 * are in the same vfs and the to directory is writable.
101 	 */
102 	if (fdvp != tdvp && fdvp->v_vfsp != tdvp->v_vfsp) {
103 		error = EXDEV;
104 		goto out;
105 	}
106 	if (tdvp->v_vfsp->vfs_flag & VFS_RDONLY) {
107 		error = EROFS;
108 		goto out;
109 	}
110 
111 	/*
112 	 * Do the rename.
113 	 */
114 	error = VOP_RENAME(fdvp, fname, tdvp, tname, CRED(), NULL, 0);
115 
116 out:
117 	if (fdvp)
118 		VN_RELE(fdvp);
119 	if (tdvp)
120 		VN_RELE(tdvp);
121 
122 	return (error);
123 }
124