xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb2_change_notify.c (revision 7f3d7c9289dee6488b3cd2848a68c0b8580d750c)
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 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates.
24  * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
25  * Copyright 2022-2025 RackTop Systems, Inc.
26  */
27 
28 /*
29  * Dispatch function for SMB2_CHANGE_NOTIFY
30  */
31 
32 #include <smbsrv/smb2_kproto.h>
33 
34 /* For the output DataOffset fields in here. */
35 #define	DATA_OFF	(SMB2_HDR_SIZE + 8)
36 
37 smb_sdrc_t
38 smb2_change_notify(smb_request_t *sr)
39 {
40 	uint16_t StructSize;
41 	uint16_t iFlags;
42 	uint32_t oBufLength;
43 	smb2fid_t smb2fid;
44 	uint32_t CompletionFilter;
45 	uint32_t reserved;
46 	uint32_t status;
47 	int rc = 0;
48 
49 	/*
50 	 * SMB2 Change Notify request
51 	 */
52 	rc = smb_mbc_decodef(
53 	    &sr->smb_data,		"wwlqqll",
54 	    &StructSize,		/* w */
55 	    &iFlags,			/* w */
56 	    &oBufLength,		/* l */
57 	    &smb2fid.persistent,	/* q */
58 	    &smb2fid.temporal,		/* q */
59 	    &CompletionFilter,		/* l */
60 	    &reserved);			/* l */
61 	if (rc || StructSize != 32)
62 		return (SDRC_ERROR);
63 
64 	status = smb2sr_lookup_fid(sr, &smb2fid);
65 	DTRACE_SMB2_START(op__ChangeNotify, smb_request_t *, sr);
66 
67 	if (status != 0)
68 		goto errout; /* Bad FID */
69 
70 	/*
71 	 * Only deal with change notify last in a compound,
72 	 * because it blocks indefinitely.  This status gets
73 	 * "sticky" handling in smb2sr_work().
74 	 */
75 	if (sr->smb2_next_command != 0) {
76 		status = NT_STATUS_INSUFFICIENT_RESOURCES;
77 		goto errout;
78 	}
79 
80 	CompletionFilter &= FILE_NOTIFY_VALID_MASK;
81 	if (iFlags & SMB2_WATCH_TREE)
82 		CompletionFilter |= FILE_NOTIFY_CHANGE_EV_SUBDIR;
83 
84 	if (oBufLength > smb2_max_trans) {
85 		status = NT_STATUS_INVALID_PARAMETER;
86 		goto errout;
87 	}
88 
89 	/*
90 	 * Check for events and consume, non-blocking.
91 	 * Special return STATUS_PENDING means:
92 	 *   No events; caller must call "act2" next.
93 	 * SMB2 does that in "async mode".
94 	 */
95 	status = smb_notify_act1(sr, oBufLength, CompletionFilter);
96 	if (status == NT_STATUS_PENDING) {
97 		smb_disp_stats_t *sds;
98 		hrtime_t start_time = sr->sr_time_start;
99 
100 		ASSERT(sr->smb2_cmd_code == SMB2_CHANGE_NOTIFY);
101 		sds = &sr->sr_server->sv_disp_stats2[SMB2_CHANGE_NOTIFY];
102 
103 		status = smb2sr_go_async_indefinite(sr);
104 		if (status != 0)
105 			goto errout;
106 		status = smb_notify_act2(sr);
107 		if (status == NT_STATUS_PENDING) {
108 			/*
109 			 * NOTE: at this point, the sr can no longer be
110 			 * referenced, as smb2_change_notify_finish() may have
111 			 * freed the sr.
112 			 *
113 			 * Change Notify is expected to block for a long time.
114 			 * Record a latency sample before we go async
115 			 * so as not to mislead users of SMB statistics.
116 			 */
117 			smb_latency_add_sample(&sds->sdt_lat,
118 			    gethrtime() - start_time);
119 
120 			/* See next: smb2_change_notify_finish */
121 			return (SDRC_SR_KEPT);
122 		}
123 	}
124 
125 errout:
126 	sr->smb2_status = status;
127 	DTRACE_SMB2_DONE(op__ChangeNotify, smb_request_t *, sr);
128 
129 	if (NT_SC_SEVERITY(status) == NT_STATUS_SEVERITY_SUCCESS) {
130 		oBufLength = sr->raw_data.chain_offset;
131 		(void) smb_mbc_encodef(
132 		    &sr->reply, "wwlC",
133 		    9,	/* StructSize */	/* w */
134 		    DATA_OFF,			/* w */
135 		    oBufLength,			/* l */
136 		    &sr->raw_data);		/* C */
137 	} else {
138 		smb2sr_put_error(sr, status);
139 	}
140 
141 	return (SDRC_SUCCESS);
142 }
143 
144 /*
145  * This is called via taskq_dispatch in smb_notify.c
146  * to finish up an NT transact notify change request.
147  * Build an SMB2 Change Notify reply and send it.
148  */
149 void
150 smb2_change_notify_finish(void *arg)
151 {
152 	smb_request_t *sr = arg;
153 	smb_disp_stats_t *sds;
154 	uint32_t status;
155 	uint32_t oBufLength;
156 
157 	SMB_REQ_VALID(sr);
158 
159 	/*
160 	 * Common part of notify, puts data in sr->raw_data
161 	 */
162 	status = smb_notify_act3(sr);
163 
164 	/*
165 	 * The prior thread returned SDRC_SR_KEPT and skiped
166 	 * the dtrace DONE probe, so fire that here.
167 	 */
168 	sr->smb2_status = status;
169 	DTRACE_SMB2_DONE(op__ChangeNotify, smb_request_t *, sr);
170 
171 	if (NT_SC_SEVERITY(status) == NT_STATUS_SEVERITY_SUCCESS) {
172 		oBufLength = sr->raw_data.chain_offset;
173 		(void) smb_mbc_encodef(
174 		    &sr->reply, "wwlC",
175 		    9,	/* StructSize */	/* w */
176 		    DATA_OFF,			/* w */
177 		    oBufLength,			/* l */
178 		    &sr->raw_data);		/* C */
179 	} else {
180 		smb2sr_put_error(sr, status);
181 	}
182 
183 	/*
184 	 * Record some statistics.
185 	 * We already took a latency sample before we went async.
186 	 */
187 	sds = &sr->session->s_server->sv_disp_stats2[SMB2_CHANGE_NOTIFY];
188 	smb2_record_stats(sr, sds, B_FALSE);
189 
190 	/*
191 	 * Put (overwrite) the final SMB2 header,
192 	 * sign, send.
193 	 */
194 	(void) smb2_encode_header(sr, B_TRUE);
195 	if (sr->smb2_hdr_flags & SMB2_FLAGS_SIGNED)
196 		smb2_sign_reply(sr);
197 	smb2_send_reply(sr);
198 
199 	mutex_enter(&sr->sr_mutex);
200 	sr->sr_state = SMB_REQ_STATE_COMPLETED;
201 	mutex_exit(&sr->sr_mutex);
202 
203 	smb_request_free(sr);
204 }
205