xref: /onnv-gate/usr/src/uts/common/fs/smbsrv/smb_flush.c (revision 10966:37e5dcdf36d3)
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 /*
228934SJose.Borrego@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
235331Samw  * Use is subject to license terms.
245331Samw  */
255331Samw 
265331Samw /*
275331Samw  * The flush SMB is sent to ensure all data and allocation information
285331Samw  * for the corresponding file has been written to stable storage. This
295331Samw  * is a synchronous request. The response should not be sent until the
305331Samw  * writes are complete.
315331Samw  *
325331Samw  * The SmbFlush request is described in CIFS/1.0 1996 Section 3.9.14.
335331Samw  *
345331Samw  * CIFS/1.0 June 13, 1996
355331Samw  * Heizer, et al
365331Samw  * draft-heizer-cifs-v1-spec-00.txt
375331Samw  */
385331Samw 
39*10966SJordan.Brown@Sun.COM #include <smbsrv/smb_kproto.h>
405331Samw #include <smbsrv/smb_fsops.h>
415331Samw 
425331Samw 
435331Samw static void smb_flush_file(struct smb_request *sr, struct smb_ofile *ofile);
445331Samw 
455331Samw /*
465331Samw  * smb_com_flush
475331Samw  *
485331Samw  * Flush any cached data for a specified file, or for all files that
495331Samw  * this client has open, to stable storage. If the fid is valid (i.e.
505331Samw  * not 0xFFFF), we flush only that file. Otherwise we flush all files
515331Samw  * associated with this client.
525331Samw  *
535331Samw  * We need to protect the list because there's a good chance we'll
545331Samw  * block during the flush operation.
555331Samw  */
566030Sjb150015 smb_sdrc_t
smb_pre_flush(smb_request_t * sr)576139Sjb150015 smb_pre_flush(smb_request_t *sr)
586139Sjb150015 {
596139Sjb150015 	int rc;
606139Sjb150015 
616139Sjb150015 	rc = smbsr_decode_vwv(sr, "w", &sr->smb_fid);
626139Sjb150015 
636139Sjb150015 	DTRACE_SMB_1(op__Flush__start, smb_request_t *, sr);
646139Sjb150015 
656139Sjb150015 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
666139Sjb150015 }
676139Sjb150015 
686139Sjb150015 void
smb_post_flush(smb_request_t * sr)696139Sjb150015 smb_post_flush(smb_request_t *sr)
706139Sjb150015 {
716139Sjb150015 	DTRACE_SMB_1(op__Flush__done, smb_request_t *, sr);
726139Sjb150015 }
736139Sjb150015 
746139Sjb150015 smb_sdrc_t
smb_com_flush(smb_request_t * sr)755331Samw smb_com_flush(smb_request_t *sr)
765331Samw {
775331Samw 	smb_ofile_t	*file;
785331Samw 	smb_llist_t	*flist;
796030Sjb150015 	int		rc;
805331Samw 
815331Samw 	if (smb_flush_required == 0) {
826030Sjb150015 		rc = smbsr_encode_empty_result(sr);
836139Sjb150015 		return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
845331Samw 	}
855331Samw 
865331Samw 	if (sr->smb_fid != 0xffff) {
878934SJose.Borrego@Sun.COM 		smbsr_lookup_file(sr);
885331Samw 		if (sr->fid_ofile == NULL) {
895772Sas200622 			smbsr_error(sr, NT_STATUS_INVALID_HANDLE,
905331Samw 			    ERRDOS, ERRbadfid);
916139Sjb150015 			return (SDRC_ERROR);
925331Samw 		}
935331Samw 
945331Samw 		smb_flush_file(sr, sr->fid_ofile);
955331Samw 	} else {
965331Samw 		flist = &sr->tid_tree->t_ofile_list;
975331Samw 		smb_llist_enter(flist, RW_READER);
985331Samw 		file = smb_llist_head(flist);
995331Samw 		while (file) {
1005331Samw 			mutex_enter(&file->f_mutex);
1015331Samw 			smb_flush_file(sr, file);
1025331Samw 			mutex_exit(&file->f_mutex);
1035331Samw 			file = smb_llist_next(flist, file);
1045331Samw 		}
1055331Samw 		smb_llist_exit(flist);
1065331Samw 	}
1076030Sjb150015 
1086030Sjb150015 	rc = smbsr_encode_empty_result(sr);
1096139Sjb150015 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
1105331Samw }
1115331Samw 
1125331Samw 
1135331Samw /*
1145331Samw  * smb_flush_file
1155331Samw  *
1165331Samw  * If writes on this file are not synchronous, flush it using the NFSv3
1175331Samw  * commit interface.
1185331Samw  */
1196030Sjb150015 static void
smb_flush_file(struct smb_request * sr,struct smb_ofile * ofile)1206030Sjb150015 smb_flush_file(struct smb_request *sr, struct smb_ofile *ofile)
1215331Samw {
1227961SNatalie.Li@Sun.COM 	sr->user_cr = smb_ofile_getcred(ofile);
1237961SNatalie.Li@Sun.COM 
1245331Samw 	if ((ofile->f_node->flags & NODE_FLAGS_WRITE_THROUGH) == 0)
1255331Samw 		(void) smb_fsop_commit(sr, sr->user_cr, ofile->f_node);
1265331Samw }
127