xref: /titanic_44/usr/src/uts/common/fs/smbsrv/smb_close.c (revision a576ab5b6e08c47732b3dedca9eaa8a8cbb85720)
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/smb_incl.h>
29 
30 
31 /*
32  * Close a file by fid.  All locks or other resources held by the
33  * requesting process on the file should be released by the server.
34  * The requesting process can no longer use the fid for further
35  * file access requests.
36  *
37  * If LastWriteTime is non-zero, it should be used to set the file
38  * timestamp.  Otherwise, file system should set the timestamp.
39  * Failure to set the timestamp, even if requested by the client,
40  * should not result in an error response from the server.
41  */
42 smb_sdrc_t
43 smb_pre_close(smb_request_t *sr)
44 {
45 	int rc;
46 
47 	rc = smbsr_decode_vwv(sr, "wl", &sr->smb_fid, &sr->arg.timestamp);
48 
49 	DTRACE_SMB_1(op__Close__start, smb_request_t *, sr);
50 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
51 }
52 
53 void
54 smb_post_close(smb_request_t *sr)
55 {
56 	DTRACE_SMB_1(op__Close__done, smb_request_t *, sr);
57 }
58 
59 smb_sdrc_t
60 smb_com_close(smb_request_t *sr)
61 {
62 	int rc;
63 
64 	sr->fid_ofile = smb_ofile_lookup_by_fid(sr->tid_tree, sr->smb_fid);
65 	if (sr->fid_ofile == NULL) {
66 		smbsr_error(sr, NT_STATUS_INVALID_HANDLE, ERRDOS, ERRbadfid);
67 		return (SDRC_ERROR);
68 	}
69 
70 	rc = smb_common_close(sr, sr->arg.timestamp);
71 	if (rc) {
72 		smbsr_errno(sr, rc);
73 		return (SDRC_ERROR);
74 	}
75 
76 	rc = smbsr_encode_empty_result(sr);
77 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
78 }
79 
80 /*
81  * Close the file represented by fid and then disconnect the
82  * associated tree.
83  */
84 smb_sdrc_t
85 smb_pre_close_and_tree_disconnect(smb_request_t *sr)
86 {
87 	int rc;
88 
89 	rc = smbsr_decode_vwv(sr, "wl", &sr->smb_fid, &sr->arg.timestamp);
90 
91 	DTRACE_SMB_1(op__CloseAndTreeDisconnect__start, smb_request_t *, sr);
92 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
93 }
94 
95 void
96 smb_post_close_and_tree_disconnect(smb_request_t *sr)
97 {
98 	DTRACE_SMB_1(op__CloseAndTreeDisconnect__done, smb_request_t *, sr);
99 }
100 
101 smb_sdrc_t
102 smb_com_close_and_tree_disconnect(smb_request_t *sr)
103 {
104 	int rc;
105 
106 	sr->fid_ofile = smb_ofile_lookup_by_fid(sr->tid_tree, sr->smb_fid);
107 	if (sr->fid_ofile == NULL) {
108 		smbsr_error(sr, NT_STATUS_INVALID_HANDLE, ERRDOS, ERRbadfid);
109 		return (SDRC_ERROR);
110 	}
111 
112 	rc = smb_common_close(sr, sr->arg.timestamp);
113 	smbsr_rq_notify(sr, sr->session, sr->tid_tree);
114 	smb_tree_disconnect(sr->tid_tree);
115 
116 	if (rc) {
117 		smbsr_errno(sr, rc);
118 		return (SDRC_ERROR);
119 	}
120 
121 	rc = smbsr_encode_empty_result(sr);
122 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
123 }
124 
125 /*
126  * smb_common_close
127  *
128  * Common close function called by SmbClose, SmbWriteAndClose,
129  * and SMBCloseAndTreeDisconnect.
130  */
131 int
132 smb_common_close(smb_request_t *sr, uint32_t last_wtime)
133 {
134 	return (smb_ofile_close(sr->fid_ofile, last_wtime));
135 }
136 
137 /*
138  * smb_commit_delete_on_close()
139  *
140  * Check for the DeleteOnClose flag on the smb file and set it on the
141  * smb node if it is not already set. This will inhibit subsequent
142  * open requests. The delete-on-close credentials should be set to the
143  * user credentials of the current open file instance.
144  *
145  * When DeleteOnClose is set on an smb_node, the common open code will
146  * reject subsequent open requests for the file. Observation of Windows
147  * 2000 indicates that subsequent opens should be allowed (assuming
148  * there would be no sharing violation) until the file is closed using
149  * the fid on which the DeleteOnClose was requested.
150  *
151  * If there are multiple opens with delete-on-close create options,
152  * whichever the first file handle is closed will trigger the node to be
153  * marked as delete-on-close. The credentials of that ofile will be used
154  * as the delete-on-close credentials of the node.
155  */
156 void
157 smb_commit_delete_on_close(struct smb_ofile *ofile)
158 {
159 	struct smb_node *node = ofile->f_node;
160 
161 	if (!(node->flags & NODE_FLAGS_DELETE_ON_CLOSE) &&
162 	    (ofile->f_flags & SMB_OFLAGS_SET_DELETE_ON_CLOSE))	{
163 		node->flags |= NODE_FLAGS_DELETE_ON_CLOSE;
164 		crhold(node->delete_on_close_cred = ofile->f_cr);
165 	}
166 }
167