xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb2_change_notify.c (revision 77c0a660417a046bfab6c8ef58d00c181c0264b3)
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 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 		status = smb2sr_go_async(sr);
98 		if (status != 0)
99 			goto errout;
100 		status = smb_notify_act2(sr);
101 		if (status == NT_STATUS_PENDING) {
102 			/* See next: smb2_change_notify_finish */
103 			return (SDRC_SR_KEPT);
104 		}
105 	}
106 
107 errout:
108 	sr->smb2_status = status;
109 	DTRACE_SMB2_DONE(op__ChangeNotify, smb_request_t *, sr);
110 
111 	if (NT_SC_SEVERITY(status) == NT_STATUS_SEVERITY_SUCCESS) {
112 		oBufLength = sr->raw_data.chain_offset;
113 		(void) smb_mbc_encodef(
114 		    &sr->reply, "wwlC",
115 		    9,	/* StructSize */	/* w */
116 		    DATA_OFF,			/* w */
117 		    oBufLength,			/* l */
118 		    &sr->raw_data);		/* C */
119 	} else {
120 		smb2sr_put_error(sr, status);
121 	}
122 
123 	return (SDRC_SUCCESS);
124 }
125 
126 /*
127  * This is called via taskq_dispatch in smb_notify.c
128  * to finish up an NT transact notify change request.
129  * Build an SMB2 Change Notify reply and send it.
130  */
131 void
132 smb2_change_notify_finish(void *arg)
133 {
134 	smb_request_t *sr = arg;
135 	smb_disp_stats_t *sds;
136 	uint32_t status;
137 	uint32_t oBufLength;
138 
139 	SMB_REQ_VALID(sr);
140 
141 	/*
142 	 * Common part of notify, puts data in sr->raw_data
143 	 */
144 	status = smb_notify_act3(sr);
145 
146 	/*
147 	 * The prior thread returned SDRC_SR_KEPT and skiped
148 	 * the dtrace DONE probe, so fire that here.
149 	 */
150 	sr->smb2_status = status;
151 	DTRACE_SMB2_DONE(op__ChangeNotify, smb_request_t *, sr);
152 
153 	if (NT_SC_SEVERITY(status) == NT_STATUS_SEVERITY_SUCCESS) {
154 		oBufLength = sr->raw_data.chain_offset;
155 		(void) smb_mbc_encodef(
156 		    &sr->reply, "wwlC",
157 		    9,	/* StructSize */	/* w */
158 		    DATA_OFF,			/* w */
159 		    oBufLength,			/* l */
160 		    &sr->raw_data);		/* C */
161 	} else {
162 		smb2sr_put_error(sr, status);
163 	}
164 
165 	/*
166 	 * Record some statistics: (just tx bytes here)
167 	 */
168 	sds = &sr->session->s_server->sv_disp_stats2[SMB2_CHANGE_NOTIFY];
169 	atomic_add_64(&sds->sdt_txb, (int64_t)(sr->reply.chain_offset));
170 
171 	/*
172 	 * Put (overwrite) the final SMB2 header,
173 	 * sign, send.
174 	 */
175 	(void) smb2_encode_header(sr, B_TRUE);
176 	if (sr->smb2_hdr_flags & SMB2_FLAGS_SIGNED)
177 		smb2_sign_reply(sr);
178 	smb2_send_reply(sr);
179 
180 	mutex_enter(&sr->sr_mutex);
181 	sr->sr_state = SMB_REQ_STATE_COMPLETED;
182 	mutex_exit(&sr->sr_mutex);
183 
184 	smb_request_free(sr);
185 }
186