xref: /onnv-gate/usr/src/uts/common/fs/smbsrv/smb_close.c (revision 5331:3047ad28a67b)
1*5331Samw /*
2*5331Samw  * CDDL HEADER START
3*5331Samw  *
4*5331Samw  * The contents of this file are subject to the terms of the
5*5331Samw  * Common Development and Distribution License (the "License").
6*5331Samw  * You may not use this file except in compliance with the License.
7*5331Samw  *
8*5331Samw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*5331Samw  * or http://www.opensolaris.org/os/licensing.
10*5331Samw  * See the License for the specific language governing permissions
11*5331Samw  * and limitations under the License.
12*5331Samw  *
13*5331Samw  * When distributing Covered Code, include this CDDL HEADER in each
14*5331Samw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*5331Samw  * If applicable, add the following below this CDDL HEADER, with the
16*5331Samw  * fields enclosed by brackets "[]" replaced with your own identifying
17*5331Samw  * information: Portions Copyright [yyyy] [name of copyright owner]
18*5331Samw  *
19*5331Samw  * CDDL HEADER END
20*5331Samw  */
21*5331Samw /*
22*5331Samw  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23*5331Samw  * Use is subject to license terms.
24*5331Samw  */
25*5331Samw 
26*5331Samw #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*5331Samw 
28*5331Samw #include <smbsrv/smb_incl.h>
29*5331Samw 
30*5331Samw 
31*5331Samw /*
32*5331Samw  * Close a file by fid.  All locks or other resources held by the
33*5331Samw  * requesting process on the file should be released by the server.
34*5331Samw  * The requesting process can no longer use the fid for further
35*5331Samw  * file access requests.
36*5331Samw  *
37*5331Samw  * If LastWriteTime is non-zero, it should be used to set the file
38*5331Samw  * timestamp.  Otherwise, file system should set the timestamp.
39*5331Samw  * Failure to set the timestamp, even if requested by the client,
40*5331Samw  * should not result in an error response from the server.
41*5331Samw  */
42*5331Samw int
43*5331Samw smb_com_close(struct smb_request *sr)
44*5331Samw {
45*5331Samw 	uint32_t last_wtime;
46*5331Samw 	int rc = 0;
47*5331Samw 
48*5331Samw 	rc = smbsr_decode_vwv(sr, "wl", &sr->smb_fid, &last_wtime);
49*5331Samw 	if (rc != 0) {
50*5331Samw 		smbsr_decode_error(sr);
51*5331Samw 		/* NOTREACHED */
52*5331Samw 	}
53*5331Samw 
54*5331Samw 	sr->fid_ofile = smb_ofile_lookup_by_fid(sr->tid_tree, sr->smb_fid);
55*5331Samw 	if (sr->fid_ofile == NULL) {
56*5331Samw 		smbsr_raise_cifs_error(sr, NT_STATUS_INVALID_HANDLE,
57*5331Samw 		    ERRDOS, ERRbadfid);
58*5331Samw 		/* NOTREACHED */
59*5331Samw 	}
60*5331Samw 
61*5331Samw 	rc = smb_common_close(sr, last_wtime);
62*5331Samw 	if (rc) {
63*5331Samw 		smbsr_raise_errno(sr, rc);
64*5331Samw 		/* NOTREACHED */
65*5331Samw 	}
66*5331Samw 
67*5331Samw 	smbsr_encode_empty_result(sr);
68*5331Samw 	return (SDRC_NORMAL_REPLY);
69*5331Samw }
70*5331Samw 
71*5331Samw /*
72*5331Samw  * Close the file represented by fid and then disconnect the
73*5331Samw  * associated tree.
74*5331Samw  */
75*5331Samw int
76*5331Samw smb_com_close_and_tree_disconnect(struct smb_request *sr)
77*5331Samw {
78*5331Samw 	uint32_t last_wtime;
79*5331Samw 	int rc = 0;
80*5331Samw 
81*5331Samw 	rc = smbsr_decode_vwv(sr, "wl", &sr->smb_fid, &last_wtime);
82*5331Samw 	if (rc != 0) {
83*5331Samw 		smbsr_decode_error(sr);
84*5331Samw 		/* NOTREACHED */
85*5331Samw 	}
86*5331Samw 
87*5331Samw 	sr->fid_ofile = smb_ofile_lookup_by_fid(sr->tid_tree, sr->smb_fid);
88*5331Samw 	if (sr->fid_ofile == NULL) {
89*5331Samw 		smbsr_raise_cifs_error(sr, NT_STATUS_INVALID_HANDLE,
90*5331Samw 		    ERRDOS, ERRbadfid);
91*5331Samw 		/* NOTREACHED */
92*5331Samw 	}
93*5331Samw 
94*5331Samw 	rc = smb_common_close(sr, last_wtime);
95*5331Samw 	smbsr_rq_notify(sr, sr->session, sr->tid_tree);
96*5331Samw 	smb_tree_disconnect(sr->tid_tree);
97*5331Samw 
98*5331Samw 	if (rc) {
99*5331Samw 		smbsr_raise_errno(sr, rc);
100*5331Samw 		/* NOTREACHED */
101*5331Samw 	}
102*5331Samw 
103*5331Samw 	smbsr_encode_empty_result(sr);
104*5331Samw 	return (SDRC_NORMAL_REPLY);
105*5331Samw }
106*5331Samw 
107*5331Samw /*
108*5331Samw  * smb_common_close
109*5331Samw  *
110*5331Samw  * Common close function called by SmbClose, SmbWriteAndClose,
111*5331Samw  * and SMBCloseAndTreeDisconnect.
112*5331Samw  */
113*5331Samw int
114*5331Samw smb_common_close(struct smb_request *sr, uint32_t last_wtime)
115*5331Samw {
116*5331Samw 	return (smb_ofile_close(sr->fid_ofile, last_wtime));
117*5331Samw }
118*5331Samw 
119*5331Samw /*
120*5331Samw  * smb_commit_delete_on_close()
121*5331Samw  *
122*5331Samw  * Check for the DeleteOnClose flag on the smb file and set it on the
123*5331Samw  * smb node if it is not already set. This will inhibit subsequent
124*5331Samw  * open requests. The delete-on-close credentials should be set to the
125*5331Samw  * user credentials of the current open file instance.
126*5331Samw  *
127*5331Samw  * When DeleteOnClose is set on an smb_node, the common open code will
128*5331Samw  * reject subsequent open requests for the file. Observation of Windows
129*5331Samw  * 2000 indicates that subsequent opens should be allowed (assuming
130*5331Samw  * there would be no sharing violation) until the file is closed using
131*5331Samw  * the fid on which the DeleteOnClose was requested.
132*5331Samw  *
133*5331Samw  * If there are multiple opens with delete-on-close create options,
134*5331Samw  * whichever the first file handle is closed will trigger the node to be
135*5331Samw  * marked as delete-on-close. The credentials of that ofile will be used
136*5331Samw  * as the delete-on-close credentials of the node.
137*5331Samw  */
138*5331Samw void
139*5331Samw smb_commit_delete_on_close(struct smb_ofile *ofile)
140*5331Samw {
141*5331Samw 	struct smb_node *node = ofile->f_node;
142*5331Samw 
143*5331Samw 	if (!(node->flags & NODE_FLAGS_DELETE_ON_CLOSE) &&
144*5331Samw 	    (ofile->f_flags & SMB_OFLAGS_SET_DELETE_ON_CLOSE))	{
145*5331Samw 		node->flags |= NODE_FLAGS_DELETE_ON_CLOSE;
146*5331Samw 		crhold(node->delete_on_close_cred = ofile->f_cr);
147*5331Samw 	}
148*5331Samw }
149