xref: /titanic_51/usr/src/uts/common/fs/smbsrv/smb_rename.c (revision b6917abefc343244b784f0cc34bc65b01469c3bf)
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  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <smbsrv/nterror.h>
29 #include <sys/synch.h>
30 #include <smbsrv/smb_incl.h>
31 #include <smbsrv/smb_fsops.h>
32 #include <sys/nbmlock.h>
33 
34 static int smb_do_rename(smb_request_t *, struct smb_fqi *, struct smb_fqi *);
35 
36 /*
37  * smb_com_rename
38  *
39  * Rename a file. Files OldFileName must exist and NewFileName must not.
40  * Both pathnames must be relative to the Tid specified in the request.
41  * Open files may be renamed.
42  *
43  * Multiple files may be renamed in response to a single request as Rename
44  * File supports wildcards in the file name (last component of the path).
45  * NOTE: we don't support rename with wildcards.
46  *
47  * SearchAttributes indicates the attributes that the target file(s) must
48  * have. If SearchAttributes is zero then only normal files are renamed.
49  * If the system file or hidden attributes are specified then the rename
50  * is inclusive - both the specified type(s) of files and normal files are
51  * renamed. The encoding of SearchAttributes is described in section 3.10
52  * - File Attribute Encoding.
53  */
54 smb_sdrc_t
55 smb_pre_rename(smb_request_t *sr)
56 {
57 	struct smb_fqi *src_fqi = &sr->arg.dirop.fqi;
58 	struct smb_fqi *dst_fqi = &sr->arg.dirop.dst_fqi;
59 	int rc;
60 
61 	if ((rc = smbsr_decode_vwv(sr, "w", &src_fqi->srch_attr)) == 0) {
62 		rc = smbsr_decode_data(sr, "%SS", sr, &src_fqi->path,
63 		    &dst_fqi->path);
64 
65 		dst_fqi->srch_attr = 0;
66 	}
67 
68 	DTRACE_SMB_2(op__Rename__start, smb_request_t *, sr,
69 	    struct dirop *, &sr->arg.dirop);
70 
71 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
72 }
73 
74 void
75 smb_post_rename(smb_request_t *sr)
76 {
77 	DTRACE_SMB_1(op__Rename__done, smb_request_t *, sr);
78 }
79 
80 smb_sdrc_t
81 smb_com_rename(smb_request_t *sr)
82 {
83 	static kmutex_t mutex;
84 	struct smb_fqi *src_fqi = &sr->arg.dirop.fqi;
85 	struct smb_fqi *dst_fqi = &sr->arg.dirop.dst_fqi;
86 	struct smb_node *dst_node;
87 	int rc;
88 
89 	if (!STYPE_ISDSK(sr->tid_tree->t_res_type)) {
90 		smbsr_error(sr, NT_STATUS_ACCESS_DENIED,
91 		    ERRDOS, ERROR_ACCESS_DENIED);
92 		return (SDRC_ERROR);
93 	}
94 
95 	mutex_enter(&mutex);
96 	rc = smb_do_rename(sr, src_fqi, dst_fqi);
97 	mutex_exit(&mutex);
98 
99 	if (rc != 0) {
100 		/*
101 		 * The following values are based on observed WFWG,
102 		 * Windows 9x, NT and Windows 2000 behaviour.
103 		 * ERROR_FILE_EXISTS doesn't work for Windows 98 clients.
104 		 * Windows 95 clients don't see the problem because the
105 		 * target is deleted before the rename request.
106 		 */
107 		switch (rc) {
108 		case EEXIST:
109 			smbsr_error(sr, NT_STATUS_OBJECT_NAME_COLLISION,
110 			    ERRDOS, ERROR_ALREADY_EXISTS);
111 			break;
112 		case EPIPE:
113 			smbsr_error(sr, NT_STATUS_SHARING_VIOLATION,
114 			    ERRDOS, ERROR_SHARING_VIOLATION);
115 			break;
116 		case ENOENT:
117 			smbsr_error(sr, NT_STATUS_OBJECT_NAME_NOT_FOUND,
118 			    ERRDOS, ERROR_FILE_NOT_FOUND);
119 			break;
120 		default:
121 			smbsr_errno(sr, rc);
122 			break;
123 		}
124 
125 		return (SDRC_ERROR);
126 	}
127 
128 	if (src_fqi->dir_snode)
129 		smb_node_release(src_fqi->dir_snode);
130 
131 	dst_node = dst_fqi->dir_snode;
132 	if (dst_node) {
133 		if (dst_node->flags & NODE_FLAGS_NOTIFY_CHANGE) {
134 			dst_node->flags |= NODE_FLAGS_CHANGED;
135 			smb_process_node_notify_change_queue(dst_node);
136 		}
137 		smb_node_release(dst_node);
138 	}
139 
140 	SMB_NULL_FQI_NODES(*src_fqi);
141 	SMB_NULL_FQI_NODES(*dst_fqi);
142 
143 	rc = smbsr_encode_empty_result(sr);
144 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
145 }
146 
147 /*
148  * smb_do_rename
149  *
150  * Backend to smb_com_rename to ensure that the rename operation is atomic.
151  * This function should be called within a mutual exclusion region. If the
152  * source and destination are identical, we don't actually do a rename, we
153  * just check that the conditions are right. If the source and destination
154  * files differ only in case, we a case-sensitive rename. Otherwise, we do
155  * a full case-insensitive rename.
156  *
157  * This function should always return errno values.
158  *
159  * Upon success, the last_snode's and dir_snode's of both src_fqi and dst_fqi
160  * are not released in this routine but in smb_com_rename().
161  */
162 static int
163 smb_do_rename(
164     smb_request_t *sr,
165     struct smb_fqi *src_fqi,
166     struct smb_fqi *dst_fqi)
167 {
168 	struct smb_node *src_node;
169 	char *dstname;
170 	DWORD status;
171 	int rc;
172 	int count;
173 
174 	if ((rc = smbd_fs_query(sr, src_fqi, FQM_PATH_MUST_EXIST)) != 0) {
175 		return (rc);
176 	}
177 
178 	src_node = src_fqi->last_snode;
179 
180 	/*
181 	 * Break the oplock before access checks. If a client
182 	 * has a file open, this will force a flush or close,
183 	 * which may affect the outcome of any share checking.
184 	 */
185 
186 	smb_oplock_break(src_node);
187 
188 	for (count = 0; count <= 3; count++) {
189 		if (count) {
190 			smb_node_end_crit(src_node);
191 			delay(MSEC_TO_TICK(400));
192 		}
193 
194 		smb_node_start_crit(src_node, RW_READER);
195 
196 		status = smb_node_rename_check(src_node);
197 
198 		if (status != NT_STATUS_SHARING_VIOLATION)
199 			break;
200 	}
201 
202 	if (status == NT_STATUS_SHARING_VIOLATION) {
203 		smb_node_end_crit(src_node);
204 
205 		smb_node_release(src_node);
206 		smb_node_release(src_fqi->dir_snode);
207 
208 		SMB_NULL_FQI_NODES(*src_fqi);
209 		SMB_NULL_FQI_NODES(*dst_fqi);
210 		return (EPIPE); /* = ERRbadshare */
211 	}
212 
213 	status = smb_range_check(sr, sr->user_cr, src_node, 0, UINT64_MAX,
214 	    B_TRUE);
215 
216 	if (status != NT_STATUS_SUCCESS) {
217 		smb_node_end_crit(src_node);
218 
219 		smb_node_release(src_node);
220 		smb_node_release(src_fqi->dir_snode);
221 
222 		SMB_NULL_FQI_NODES(*src_fqi);
223 		SMB_NULL_FQI_NODES(*dst_fqi);
224 		return (EACCES);
225 	}
226 
227 	if (utf8_strcasecmp(src_fqi->path, dst_fqi->path) == 0) {
228 		if ((rc = smbd_fs_query(sr, dst_fqi, 0)) != 0) {
229 			smb_node_end_crit(src_node);
230 
231 			smb_node_release(src_node);
232 			smb_node_release(src_fqi->dir_snode);
233 
234 			SMB_NULL_FQI_NODES(*src_fqi);
235 			SMB_NULL_FQI_NODES(*dst_fqi);
236 			return (rc);
237 		}
238 
239 		/*
240 		 * Because the fqm parameter to smbd_fs_query() was 0,
241 		 * a successful return value means that dst_fqi->last_snode
242 		 * may be NULL.
243 		 */
244 		if (dst_fqi->last_snode)
245 			smb_node_release(dst_fqi->last_snode);
246 
247 		rc = strcmp(src_fqi->last_comp_od, dst_fqi->last_comp);
248 		if (rc == 0) {
249 			smb_node_end_crit(src_node);
250 
251 			smb_node_release(src_node);
252 			smb_node_release(src_fqi->dir_snode);
253 			smb_node_release(dst_fqi->dir_snode);
254 
255 			SMB_NULL_FQI_NODES(*src_fqi);
256 			SMB_NULL_FQI_NODES(*dst_fqi);
257 			return (0);
258 		}
259 
260 		rc = smb_fsop_rename(sr, sr->user_cr,
261 		    src_fqi->dir_snode,
262 		    src_fqi->last_comp_od,
263 		    dst_fqi->dir_snode,
264 		    dst_fqi->last_comp);
265 
266 		if (rc != 0) {
267 			smb_node_release(src_fqi->dir_snode);
268 			smb_node_release(dst_fqi->dir_snode);
269 
270 			SMB_NULL_FQI_NODES(*src_fqi);
271 			SMB_NULL_FQI_NODES(*dst_fqi);
272 		}
273 
274 		smb_node_end_crit(src_node);
275 
276 		smb_node_release(src_node);
277 		return (rc);
278 	}
279 
280 	rc = smbd_fs_query(sr, dst_fqi, FQM_PATH_MUST_NOT_EXIST);
281 	if (rc != 0) {
282 		smb_node_end_crit(src_node);
283 
284 		smb_node_release(src_node);
285 		smb_node_release(src_fqi->dir_snode);
286 
287 		SMB_NULL_FQI_NODES(*src_fqi);
288 		SMB_NULL_FQI_NODES(*dst_fqi);
289 		return (rc);
290 	}
291 
292 	/*
293 	 * Because of FQM_PATH_MUST_NOT_EXIST and the successful return
294 	 * value, only dst_fqi->dir_snode is valid (dst_fqi->last_snode
295 	 * is NULL).
296 	 */
297 
298 	/*
299 	 * Use the unmangled form of the destination name if the
300 	 * source and destination names are the same and the source
301 	 * name is mangled.  (We are taking a chance here, assuming
302 	 * that this is what the user wants.)
303 	 */
304 
305 	if ((smb_maybe_mangled_name(src_fqi->last_comp)) &&
306 	    (strcmp(src_fqi->last_comp, dst_fqi->last_comp) == 0)) {
307 		dstname = src_fqi->last_comp_od;
308 	} else {
309 		dstname = dst_fqi->last_comp;
310 	}
311 
312 	rc = smb_fsop_rename(sr, sr->user_cr,
313 	    src_fqi->dir_snode,
314 	    src_fqi->last_comp_od,
315 	    dst_fqi->dir_snode,
316 	    dstname);
317 
318 	if (rc != 0) {
319 		smb_node_release(src_fqi->dir_snode);
320 		smb_node_release(dst_fqi->dir_snode);
321 
322 		SMB_NULL_FQI_NODES(*src_fqi);
323 		SMB_NULL_FQI_NODES(*dst_fqi);
324 	}
325 
326 	smb_node_end_crit(src_node);
327 
328 	smb_node_release(src_node);
329 
330 	return (rc);
331 }
332