xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_flush.c (revision 2cf6b79f28bc1e3f5631e49f3995194cc93991d4)
1da6c28aaSamw /*
2da6c28aaSamw  * CDDL HEADER START
3da6c28aaSamw  *
4da6c28aaSamw  * The contents of this file are subject to the terms of the
5da6c28aaSamw  * Common Development and Distribution License (the "License").
6da6c28aaSamw  * You may not use this file except in compliance with the License.
7da6c28aaSamw  *
8da6c28aaSamw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9da6c28aaSamw  * or http://www.opensolaris.org/os/licensing.
10da6c28aaSamw  * See the License for the specific language governing permissions
11da6c28aaSamw  * and limitations under the License.
12da6c28aaSamw  *
13da6c28aaSamw  * When distributing Covered Code, include this CDDL HEADER in each
14da6c28aaSamw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15da6c28aaSamw  * If applicable, add the following below this CDDL HEADER, with the
16da6c28aaSamw  * fields enclosed by brackets "[]" replaced with your own identifying
17da6c28aaSamw  * information: Portions Copyright [yyyy] [name of copyright owner]
18da6c28aaSamw  *
19da6c28aaSamw  * CDDL HEADER END
20da6c28aaSamw  */
21da6c28aaSamw /*
222c2961f8Sjose borrego  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23da6c28aaSamw  * Use is subject to license terms.
246d1c73b5SDan Vatca  *
256d1c73b5SDan Vatca  * Copyright 2016 Syneto S.R.L. All rights reserved.
2693bc28dbSGordon Ross  * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
27*2cf6b79fSGordon Ross  * Copyright 2022 RackTop Systems, Inc.
28da6c28aaSamw  */
29da6c28aaSamw 
30da6c28aaSamw /*
31da6c28aaSamw  * The flush SMB is sent to ensure all data and allocation information
32da6c28aaSamw  * for the corresponding file has been written to stable storage. This
33da6c28aaSamw  * is a synchronous request. The response should not be sent until the
34da6c28aaSamw  * writes are complete.
35da6c28aaSamw  *
36da6c28aaSamw  * The SmbFlush request is described in CIFS/1.0 1996 Section 3.9.14.
37da6c28aaSamw  *
38da6c28aaSamw  * CIFS/1.0 June 13, 1996
39da6c28aaSamw  * Heizer, et al
40da6c28aaSamw  * draft-heizer-cifs-v1-spec-00.txt
41da6c28aaSamw  */
42da6c28aaSamw 
43bbf6f00cSJordan Brown #include <smbsrv/smb_kproto.h>
44da6c28aaSamw #include <smbsrv/smb_fsops.h>
45da6c28aaSamw 
46da6c28aaSamw 
47da6c28aaSamw /*
48da6c28aaSamw  * smb_com_flush
49da6c28aaSamw  *
50da6c28aaSamw  * Flush any cached data for a specified file, or for all files that
51da6c28aaSamw  * this client has open, to stable storage. If the fid is valid (i.e.
52da6c28aaSamw  * not 0xFFFF), we flush only that file. Otherwise we flush all files
53da6c28aaSamw  * associated with this client.
54da6c28aaSamw  *
55da6c28aaSamw  * We need to protect the list because there's a good chance we'll
56da6c28aaSamw  * block during the flush operation.
57da6c28aaSamw  */
587b59d02dSjb150015 smb_sdrc_t
smb_pre_flush(smb_request_t * sr)59faa1795aSjb150015 smb_pre_flush(smb_request_t *sr)
60faa1795aSjb150015 {
61faa1795aSjb150015 	int rc;
62faa1795aSjb150015 
63faa1795aSjb150015 	rc = smbsr_decode_vwv(sr, "w", &sr->smb_fid);
64faa1795aSjb150015 
6593bc28dbSGordon Ross 	DTRACE_SMB_START(op__Flush, smb_request_t *, sr);
66faa1795aSjb150015 
67faa1795aSjb150015 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
68faa1795aSjb150015 }
69faa1795aSjb150015 
70faa1795aSjb150015 void
smb_post_flush(smb_request_t * sr)71faa1795aSjb150015 smb_post_flush(smb_request_t *sr)
72faa1795aSjb150015 {
7393bc28dbSGordon Ross 	DTRACE_SMB_DONE(op__Flush, smb_request_t *, sr);
74faa1795aSjb150015 }
75faa1795aSjb150015 
76faa1795aSjb150015 smb_sdrc_t
smb_com_flush(smb_request_t * sr)77da6c28aaSamw smb_com_flush(smb_request_t *sr)
78da6c28aaSamw {
79da6c28aaSamw 	smb_ofile_t	*file;
80*2cf6b79fSGordon Ross 	smb_lavl_t	*lavl;
817b59d02dSjb150015 	int		rc;
82da6c28aaSamw 
83da6c28aaSamw 	if (smb_flush_required == 0) {
847b59d02dSjb150015 		rc = smbsr_encode_empty_result(sr);
85faa1795aSjb150015 		return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
86da6c28aaSamw 	}
87da6c28aaSamw 
88da6c28aaSamw 	if (sr->smb_fid != 0xffff) {
892c2961f8Sjose borrego 		smbsr_lookup_file(sr);
90da6c28aaSamw 		if (sr->fid_ofile == NULL) {
91dc20a302Sas200622 			smbsr_error(sr, NT_STATUS_INVALID_HANDLE,
92da6c28aaSamw 			    ERRDOS, ERRbadfid);
93faa1795aSjb150015 			return (SDRC_ERROR);
94da6c28aaSamw 		}
956d1c73b5SDan Vatca 		smb_ofile_flush(sr, sr->fid_ofile);
96da6c28aaSamw 	} else {
97*2cf6b79fSGordon Ross 		lavl = &sr->tid_tree->t_ofile_list;
98*2cf6b79fSGordon Ross 		smb_lavl_enter(lavl, RW_READER);
99*2cf6b79fSGordon Ross 		file = smb_lavl_first(lavl);
100da6c28aaSamw 		while (file) {
101da6c28aaSamw 			mutex_enter(&file->f_mutex);
1026d1c73b5SDan Vatca 			smb_ofile_flush(sr, file);
103da6c28aaSamw 			mutex_exit(&file->f_mutex);
104*2cf6b79fSGordon Ross 			file = smb_lavl_next(lavl, file);
105da6c28aaSamw 		}
106*2cf6b79fSGordon Ross 		smb_lavl_exit(lavl);
107da6c28aaSamw 	}
1087b59d02dSjb150015 
1097b59d02dSjb150015 	rc = smbsr_encode_empty_result(sr);
110faa1795aSjb150015 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
111da6c28aaSamw }
112