xref: /titanic_51/usr/src/uts/common/fs/smbsrv/smb_close.c (revision 1c2187e7a735b31a46941879f0bd124e0aa325a3)
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 2007 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 int
43 smb_com_close(struct smb_request *sr)
44 {
45 	uint32_t last_wtime;
46 	int rc = 0;
47 
48 	rc = smbsr_decode_vwv(sr, "wl", &sr->smb_fid, &last_wtime);
49 	if (rc != 0) {
50 		smbsr_decode_error(sr);
51 		/* NOTREACHED */
52 	}
53 
54 	sr->fid_ofile = smb_ofile_lookup_by_fid(sr->tid_tree, sr->smb_fid);
55 	if (sr->fid_ofile == NULL) {
56 		smbsr_raise_cifs_error(sr, NT_STATUS_INVALID_HANDLE,
57 		    ERRDOS, ERRbadfid);
58 		/* NOTREACHED */
59 	}
60 
61 	rc = smb_common_close(sr, last_wtime);
62 	if (rc) {
63 		smbsr_raise_errno(sr, rc);
64 		/* NOTREACHED */
65 	}
66 
67 	smbsr_encode_empty_result(sr);
68 	return (SDRC_NORMAL_REPLY);
69 }
70 
71 /*
72  * Close the file represented by fid and then disconnect the
73  * associated tree.
74  */
75 int
76 smb_com_close_and_tree_disconnect(struct smb_request *sr)
77 {
78 	uint32_t last_wtime;
79 	int rc = 0;
80 
81 	rc = smbsr_decode_vwv(sr, "wl", &sr->smb_fid, &last_wtime);
82 	if (rc != 0) {
83 		smbsr_decode_error(sr);
84 		/* NOTREACHED */
85 	}
86 
87 	sr->fid_ofile = smb_ofile_lookup_by_fid(sr->tid_tree, sr->smb_fid);
88 	if (sr->fid_ofile == NULL) {
89 		smbsr_raise_cifs_error(sr, NT_STATUS_INVALID_HANDLE,
90 		    ERRDOS, ERRbadfid);
91 		/* NOTREACHED */
92 	}
93 
94 	rc = smb_common_close(sr, last_wtime);
95 	smbsr_rq_notify(sr, sr->session, sr->tid_tree);
96 	smb_tree_disconnect(sr->tid_tree);
97 
98 	if (rc) {
99 		smbsr_raise_errno(sr, rc);
100 		/* NOTREACHED */
101 	}
102 
103 	smbsr_encode_empty_result(sr);
104 	return (SDRC_NORMAL_REPLY);
105 }
106 
107 /*
108  * smb_common_close
109  *
110  * Common close function called by SmbClose, SmbWriteAndClose,
111  * and SMBCloseAndTreeDisconnect.
112  */
113 int
114 smb_common_close(struct smb_request *sr, uint32_t last_wtime)
115 {
116 	return (smb_ofile_close(sr->fid_ofile, last_wtime));
117 }
118 
119 /*
120  * smb_commit_delete_on_close()
121  *
122  * Check for the DeleteOnClose flag on the smb file and set it on the
123  * smb node if it is not already set. This will inhibit subsequent
124  * open requests. The delete-on-close credentials should be set to the
125  * user credentials of the current open file instance.
126  *
127  * When DeleteOnClose is set on an smb_node, the common open code will
128  * reject subsequent open requests for the file. Observation of Windows
129  * 2000 indicates that subsequent opens should be allowed (assuming
130  * there would be no sharing violation) until the file is closed using
131  * the fid on which the DeleteOnClose was requested.
132  *
133  * If there are multiple opens with delete-on-close create options,
134  * whichever the first file handle is closed will trigger the node to be
135  * marked as delete-on-close. The credentials of that ofile will be used
136  * as the delete-on-close credentials of the node.
137  */
138 void
139 smb_commit_delete_on_close(struct smb_ofile *ofile)
140 {
141 	struct smb_node *node = ofile->f_node;
142 
143 	if (!(node->flags & NODE_FLAGS_DELETE_ON_CLOSE) &&
144 	    (ofile->f_flags & SMB_OFLAGS_SET_DELETE_ON_CLOSE))	{
145 		node->flags |= NODE_FLAGS_DELETE_ON_CLOSE;
146 		crhold(node->delete_on_close_cred = ofile->f_cr);
147 	}
148 }
149