xref: /onnv-gate/usr/src/uts/common/fs/smbsrv/smb_flush.c (revision 6139:5c743b207bf9)
15331Samw /*
25331Samw  * CDDL HEADER START
35331Samw  *
45331Samw  * The contents of this file are subject to the terms of the
55331Samw  * Common Development and Distribution License (the "License").
65331Samw  * You may not use this file except in compliance with the License.
75331Samw  *
85331Samw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95331Samw  * or http://www.opensolaris.org/os/licensing.
105331Samw  * See the License for the specific language governing permissions
115331Samw  * and limitations under the License.
125331Samw  *
135331Samw  * When distributing Covered Code, include this CDDL HEADER in each
145331Samw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155331Samw  * If applicable, add the following below this CDDL HEADER, with the
165331Samw  * fields enclosed by brackets "[]" replaced with your own identifying
175331Samw  * information: Portions Copyright [yyyy] [name of copyright owner]
185331Samw  *
195331Samw  * CDDL HEADER END
205331Samw  */
215331Samw /*
225772Sas200622  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
235331Samw  * Use is subject to license terms.
245331Samw  */
255331Samw 
265331Samw #pragma ident	"%Z%%M%	%I%	%E% SMI"
275331Samw 
285331Samw /*
295331Samw  * The flush SMB is sent to ensure all data and allocation information
305331Samw  * for the corresponding file has been written to stable storage. This
315331Samw  * is a synchronous request. The response should not be sent until the
325331Samw  * writes are complete.
335331Samw  *
345331Samw  * The SmbFlush request is described in CIFS/1.0 1996 Section 3.9.14.
355331Samw  *
365331Samw  * CIFS/1.0 June 13, 1996
375331Samw  * Heizer, et al
385331Samw  * draft-heizer-cifs-v1-spec-00.txt
395331Samw  */
405331Samw 
415331Samw #include <smbsrv/smb_incl.h>
425331Samw #include <smbsrv/smb_fsops.h>
435331Samw 
445331Samw 
455331Samw static void smb_flush_file(struct smb_request *sr, struct smb_ofile *ofile);
465331Samw 
475331Samw /*
485331Samw  * smb_com_flush
495331Samw  *
505331Samw  * Flush any cached data for a specified file, or for all files that
515331Samw  * this client has open, to stable storage. If the fid is valid (i.e.
525331Samw  * not 0xFFFF), we flush only that file. Otherwise we flush all files
535331Samw  * associated with this client.
545331Samw  *
555331Samw  * We need to protect the list because there's a good chance we'll
565331Samw  * block during the flush operation.
575331Samw  */
586030Sjb150015 smb_sdrc_t
59*6139Sjb150015 smb_pre_flush(smb_request_t *sr)
60*6139Sjb150015 {
61*6139Sjb150015 	int rc;
62*6139Sjb150015 
63*6139Sjb150015 	rc = smbsr_decode_vwv(sr, "w", &sr->smb_fid);
64*6139Sjb150015 
65*6139Sjb150015 	DTRACE_SMB_1(op__Flush__start, smb_request_t *, sr);
66*6139Sjb150015 
67*6139Sjb150015 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
68*6139Sjb150015 }
69*6139Sjb150015 
70*6139Sjb150015 void
71*6139Sjb150015 smb_post_flush(smb_request_t *sr)
72*6139Sjb150015 {
73*6139Sjb150015 	DTRACE_SMB_1(op__Flush__done, smb_request_t *, sr);
74*6139Sjb150015 }
75*6139Sjb150015 
76*6139Sjb150015 smb_sdrc_t
775331Samw smb_com_flush(smb_request_t *sr)
785331Samw {
795331Samw 	smb_ofile_t	*file;
805331Samw 	smb_llist_t	*flist;
816030Sjb150015 	int		rc;
825331Samw 
835331Samw 	if (smb_flush_required == 0) {
846030Sjb150015 		rc = smbsr_encode_empty_result(sr);
85*6139Sjb150015 		return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
865331Samw 	}
875331Samw 
885331Samw 	if (sr->smb_fid != 0xffff) {
895331Samw 		sr->fid_ofile = smb_ofile_lookup_by_fid(sr->tid_tree,
905331Samw 		    sr->smb_fid);
915331Samw 		if (sr->fid_ofile == NULL) {
925772Sas200622 			smbsr_error(sr, NT_STATUS_INVALID_HANDLE,
935331Samw 			    ERRDOS, ERRbadfid);
94*6139Sjb150015 			return (SDRC_ERROR);
955331Samw 		}
965331Samw 
975331Samw 		smb_flush_file(sr, sr->fid_ofile);
985331Samw 	} else {
995331Samw 		flist = &sr->tid_tree->t_ofile_list;
1005331Samw 		smb_llist_enter(flist, RW_READER);
1015331Samw 		file = smb_llist_head(flist);
1025331Samw 		while (file) {
1035331Samw 			mutex_enter(&file->f_mutex);
1045331Samw 			smb_flush_file(sr, file);
1055331Samw 			mutex_exit(&file->f_mutex);
1065331Samw 			file = smb_llist_next(flist, file);
1075331Samw 		}
1085331Samw 		smb_llist_exit(flist);
1095331Samw 	}
1106030Sjb150015 
1116030Sjb150015 	rc = smbsr_encode_empty_result(sr);
112*6139Sjb150015 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
1135331Samw }
1145331Samw 
1155331Samw 
1165331Samw /*
1175331Samw  * smb_flush_file
1185331Samw  *
1195331Samw  * If writes on this file are not synchronous, flush it using the NFSv3
1205331Samw  * commit interface.
1215331Samw  */
1226030Sjb150015 static void
1236030Sjb150015 smb_flush_file(struct smb_request *sr, struct smb_ofile *ofile)
1245331Samw {
1255331Samw 	if ((ofile->f_node->flags & NODE_FLAGS_WRITE_THROUGH) == 0)
1265331Samw 		(void) smb_fsop_commit(sr, sr->user_cr, ofile->f_node);
1275331Samw }
128