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