xref: /titanic_50/usr/src/uts/common/fs/smbsrv/smb_flush.c (revision 7b2cbac6bdd0f33171865b3ab8e08e8528c60d2c)
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 /*
29  * The flush SMB is sent to ensure all data and allocation information
30  * for the corresponding file has been written to stable storage. This
31  * is a synchronous request. The response should not be sent until the
32  * writes are complete.
33  *
34  * The SmbFlush request is described in CIFS/1.0 1996 Section 3.9.14.
35  *
36  * CIFS/1.0 June 13, 1996
37  * Heizer, et al
38  * draft-heizer-cifs-v1-spec-00.txt
39  */
40 
41 #include <smbsrv/smb_incl.h>
42 #include <smbsrv/smb_fsops.h>
43 
44 
45 static void smb_flush_file(struct smb_request *sr, struct smb_ofile *ofile);
46 
47 
48 int smb_flush_required = 1;
49 
50 
51 /*
52  * smb_commit_required
53  *
54  * Specify whether or not SmbFlush should send commit requests to the
55  * file system. If state is non-zero, commit requests will be sent to
56  * the file system. If state is zero, SmbFlush is a no-op.
57  */
58 void
59 smb_commit_required(int state)
60 {
61 	smb_flush_required = state;
62 }
63 
64 
65 /*
66  * smb_com_flush
67  *
68  * Flush any cached data for a specified file, or for all files that
69  * this client has open, to stable storage. If the fid is valid (i.e.
70  * not 0xFFFF), we flush only that file. Otherwise we flush all files
71  * associated with this client.
72  *
73  * We need to protect the list because there's a good chance we'll
74  * block during the flush operation.
75  */
76 smb_sdrc_t
77 smb_com_flush(smb_request_t *sr)
78 {
79 	smb_ofile_t	*file;
80 	smb_llist_t	*flist;
81 	int		rc;
82 
83 	if (smbsr_decode_vwv(sr, "w", &sr->smb_fid) != 0)
84 		return (SDRC_ERROR_REPLY);
85 
86 	if (smb_flush_required == 0) {
87 		rc = smbsr_encode_empty_result(sr);
88 		return ((rc == 0) ? SDRC_NORMAL_REPLY : SDRC_ERROR_REPLY);
89 	}
90 
91 	if (sr->smb_fid != 0xffff) {
92 		sr->fid_ofile = smb_ofile_lookup_by_fid(sr->tid_tree,
93 		    sr->smb_fid);
94 		if (sr->fid_ofile == NULL) {
95 			smbsr_error(sr, NT_STATUS_INVALID_HANDLE,
96 			    ERRDOS, ERRbadfid);
97 			return (SDRC_ERROR_REPLY);
98 		}
99 
100 		smb_flush_file(sr, sr->fid_ofile);
101 	} else {
102 		flist = &sr->tid_tree->t_ofile_list;
103 		smb_llist_enter(flist, RW_READER);
104 		file = smb_llist_head(flist);
105 		while (file) {
106 			mutex_enter(&file->f_mutex);
107 			smb_flush_file(sr, file);
108 			mutex_exit(&file->f_mutex);
109 			file = smb_llist_next(flist, file);
110 		}
111 		smb_llist_exit(flist);
112 	}
113 
114 	rc = smbsr_encode_empty_result(sr);
115 	return ((rc == 0) ? SDRC_NORMAL_REPLY : SDRC_ERROR_REPLY);
116 }
117 
118 
119 /*
120  * smb_flush_file
121  *
122  * If writes on this file are not synchronous, flush it using the NFSv3
123  * commit interface.
124  */
125 static void
126 smb_flush_file(struct smb_request *sr, struct smb_ofile *ofile)
127 {
128 	if ((ofile->f_node->flags & NODE_FLAGS_WRITE_THROUGH) == 0)
129 		(void) smb_fsop_commit(sr, sr->user_cr, ofile->f_node);
130 }
131